Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi all,
I am writting a GUI library using win 32 api. My laguage is Nim, but that is not a matter here.
For mimicking vb.net style, i have designed an event driven method in this library. Every control class in my lib has some public members called events. And all of them are UINTs. like --
ButtonClass.click = BN_CLICKED
FormClass.click = WM_LBUTTONDOWN
Each class has an AddHandler function. This is the signature.
AddHandler(evt, pFn)
-evt = events
-pFn = function pointer
So this AddHandler will add all user functions and appropriate messages in a list. Then, in the WndProc function, i can loop through this list and call appropriate function. Everything seems to be ok so far.
But I want my control classes to handle mouse enter, hover and leave events too. This created a problem. I can't find a general control mouse enter message in win api. I know that button's use BCN_HOTITEMCHANGE and it worked for me. But i can't find a solution which is applicable for every controls. How to fix this problem.
I just tested with TrackMouseEvent. This was my testing scenario-
1. When mouse pointer hovered over any control, i get a WM_MOUSELEAVE message. And when pointer comes back to client area, i get a WM_MOUSEMOVE message.
2. So i started tracking mouse in WM_MOUSELEAVE and stopped tracking in WM_MOUSEMOVE. My intention was to get the mouse hover message on the top of every controls. But this was not worked as in intend. Any help ?
Some extra info--
1. For all control class, i have created a CTL_MOUSE_ENTER, CTL_MOUSE_HOVER, CTL_MOUSE_LEAVE messages.
2. Then i added function pointers for these events and keep it in a list.
3. In WndProc, i want to loop through this list and call the function.
4. To find which control is under mouse pointer, i use GetMousePos & ScreenToClient functions.
5. And then i tried with WindowFromPoint function but didn't worked.
6. Then i tried to find the control with the RECT of the control, but didn't worked.
Now, all my experiments were failed.

What I have tried:

proc TrackMouse*(handle : HWND) = 
    var lpTME : TTRACKMOUSEEVENT
    lpTME.cbSize = cast[DWORD](sizeof(TTRACKMOUSEEVENT))
    lpTME.dwFlags = TME_HOVER or TME_LEAVE 
    lpTME.dwHoverTime = 100 #HOVER_DEFAULT
    lpTME.hwndTrack = handle
    discard COMCTL32_TrackMouseEvent(lpTME)
Posted
Updated 2-May-19 23:27pm
Comments
Richard MacCutchan 8-Oct-18 9:45am    
The best way to proceed with this is get a message tracker such as Spy++. You can then run your code and see exactly what messages are generated at each point.
Vinod Kc 9-Oct-18 14:43pm    
@Richard MacCutchan,
Thanks for the reply. I subclassed this control and it worked.

1 solution

You should use the WM_KILLFOCUS event which is raised just before the mouse moves out of a control.

See: WM_KILLFOCUS message - Windows applications | Microsoft Docs[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900