Link to home
Start Free TrialLog in
Avatar of mpmagin
mpmagin

asked on

MouseMove or GotFocus on a menu?

Is there a way to determine if a menu has focus or the mouse moves over a menu?  The only event available for the VB Menu object is Click, but I want to know if the user moves over a menu item before they click it.
Avatar of Microsoft
Microsoft

this is an example of how to get a value when the mouse is over an object maybe this might help you
to get an idea.


Call this function, passing the list box's hwnd, x, and y, from the list box's mousemove event, and
the item the mouse is over will be selected

Option Explicit
Private Const LB_SETCURSEL = &H186
Private Const LB_GETCURSEL = &H188
Private Type POINTAPI
   X As Long
   Y As Long
End Type

Private Declare Function ClientToScreen Lib "user32" _
(ByVal hwnd As Long, lpPoint As POINTAPI) As Long

Private Declare Function LBItemFromPt Lib "COMCTL32.DLL" _
(ByVal hLB As Long, ByVal ptX As Long, ByVal ptY As Long, _
ByVal bAutoScroll As Long) As Long

Private Declare Function SendMessage Lib "user32" Alias _
"SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lParam As Long) As Long

Public Sub HighlightLBItem(ByVal LBHwnd As Long, _
ByVal X As Single, ByVal Y As Single)

Dim ItemIndex As Long
Dim AtThisPoint As POINTAPI
AtThisPoint.X = X \ Screen.TwipsPerPixelX
AtThisPoint.Y = Y \ Screen.TwipsPerPixelY
Call ClientToScreen(LBHwnd, AtThisPoint)
ItemIndex = LBItemFromPt(LBHwnd, AtThisPoint.X, _
  AtThisPoint.Y, False)
If ItemIndex <> SendMessage(LBHwnd, LB_GETCURSEL, 0, 0) Then
   Call SendMessage(LBHwnd, LB_SETCURSEL, ItemIndex, 0)
End If

End Sub

try that

cheers
andy

I could be just pissing in the wind, i don't know if any of what ive suggested but im trying to help
as best as i can

if its not what you wont then never mind im only Human

cheers
Andy  
Comment
From
ASKER CERTIFIED SOLUTION
Avatar of JonFish85
JonFish85

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
mpmagin,

Are you wanting to use this to be able to enable/disable certain menu items, or to show/do something as they move over the menu items?

If you want to disable/enable menu items you can do it in the parent menu click event since it will run before the menu items expand, so if you have a menu like:

File
--Menu 1
--Menu 2
--Menu 3

When the mouse moves over the file menu item it will fire the click event before the menu expands, you can set the other menus enabled/disabled at this time in the File menu click event.  Even though they might not have actually clicked it themselves, the event still runs.

If you want to show or do something for each individual menu you will need to subclass the form as JonFish85 has suggested.  The example he gave you will do what you want.

My two cents...
Shannon
YOU MEAN the example Microsoft has given, as Jon has stated his example may do the same as mine.

sorry to seem alittle upset but don't want to lose points

you know how that feels.

cheers

Andy
microsoft, your answer will just work with listboxes no (ie the LB constants) no?
Andy,

Sorry, but subclassing the menu system of the form is the best way if he wants mouse events for it.  What Jon may not know is that the example he suggested from VB World's website will do just that.  On the other hand your code deals with a listbox and will not notify him of a menu mouse event.  If I am missing something please let me know...I didn't mean to try to sway him into picking someone's answer over another:)

Regards,
Shannon
Avatar of mpmagin

ASKER

JonFish85

That was exactly what I needed.  Thanks!

mpmagin
glad I could help!