(Win98+,NT4.0+,Win2K,WinXP)

This is a simple implementation of the TrackMouseEvent function to detect when the cursor has hovered over a control for 1 second. It requires a global variable fHover dd FALSE. To enable hover detect pass the controls handle to SetHoverDetect, to stop monitoring the control pass the handle to EndHoverDetect. Once a hover is detected the proc sends a WM_USER+26 message to the parent of the control with the following parameters:

wParam = Handle of the control
lParam = HIWORD = yPos, LOWORD = xPos in client coordinates

This sniplet uses the GWL_USERDATA field.
SetHoverDetect proc hWin:DWORD

invoke SetWindowLong,hWin,GWL_WNDPROC,OFFSET DetectHover
invoke SetWindowLong,hWin,GWL_USERDATA,eax
ret
SetHoverDetect endp

EndHoverDetect proc hWin:DWORD
invoke GetWindowLong,hWin,GWL_WNDPROC
.IF eax == OFFSET DetectHover
invoke GetWindowLong,hWin,GWL_USERDATA
invoke SetWindowLong,hWin,GWL_WNDPROC,eax
mov eax,0
.ELSE
mov eax,-1
.ENDIF
ret
EndHoverDetect endp

DetectHover proc uses ebx esi edi hWin:HWND,uMsg:UINT,wParam:WPARAM,lParam:LPARAM
LOCAL tme :TRACKMOUSEEVENT
LOCAL pt :POINT

.IF uMsg == WM_MOUSEMOVE
.IF fHover == FALSE
mov eax,hWin
mov tme.hwndTrack,eax
mov tme.cbSize,SIZEOF TRACKMOUSEEVENT
mov tme.dwFlags,TME_HOVER or TME_LEAVE
mov tme.dwHoverTime,1000 ; hover time in milliseconds
invoke TrackMouseEvent,ADDR tme
mov fHover,TRUE
.ENDIF

.ELSEIF uMsg == WM_MOUSEHOVER
mov fHover,FALSE
invoke GetParent,hWin
invoke PostMessage,eax,WM_USER+26,hWin,lParam

.ELSEIF uMsg == WM_MOUSELEAVE
mov fHover,FALSE

.ENDIF

invoke GetWindowLong,hWin,GWL_USERDATA
invoke CallWindowProc,eax,hWin,uMsg,wParam,lParam

ret
DetectHover endp
Posted on 2003-07-28 12:49:02 by donkey