Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Tracking the mouse

Status
Not open for further replies.

AmkG

Programmer
May 12, 2001
366
PH
Okay, so a window gets the WM_MOUSEMOVE message when the mouse pointer is in its client area. However, I need to know is whether or not the mouse is within a the client area; I don't know if a message is sent to the window when the mouse leaves it. How can I have a message sent when the mouse leaves or enters the window? By what other method do I need to track it? "Information has a tendency to be free. Which means someone will always tell you something you don't want to know."
 
Talk about answering your own questions...

Use TrackMouseEvent(). You can get more information on that and the TRACKMOUSEEVENT structure on MSDN. Basically, TRACKMOUSEEVENT structure is this:
typedef struct TRACKMOUSEEVENT {
DWORD cbSize;
DWORD dwFlags;
HWND hwndTrack;
DWORD dwHoverTime;
}
cbSize=sizeof(TRACKMOUSEEVENT)
dwFlags=TME_LEAVE;
hwndTrack=hWnd of window you are interested in.
dwHoverTime=0; no real need to use it.


UNFORTUNATELY, TrackMouseEvent() is Win98 or later. To be Win95 compatible, you can use the SetWindowsHookEx() function to hook into the mouse driver. Again for more info look up MSDN. To use SetWindowsHookEx(), you need to have a separate function that will track the mouse.
Code:
hHook=SetWindowsHookEx(WH_MOUSE,&MouseTrackingProc,hInstance,NULL);

Whenever the mouse is moved, SetWindowsHookEx will cause your function MouseTrackingProc() to be called with the absolute SCREEN coordinates. MouseTrackingProc() must now use the WindowFromPoint() function to check which window the mouse is currently on. You SHOULD know which window you are interested in...

When you are done with mouse tracking, use UnhookWindowsHookEx().
"Information has a tendency to be free. Which means someone will always tell you something you don't want to know."
 
Gee man, thanks! You're great! You told me everything I needed to know! I really really should vote for you! [FOOTNOTE 1]




















Footnote 1: Talking to yourself is a sure sign of impending mental breakdown. "Information has a tendency to be free. Which means someone will always tell you something you don't want to know."
 
A bit of an aside:
if you use SetWindowsHookEx() in your application's code, your MouseProc() procedure will be called if, AND ONLY IF, the mouse is in one of your windows. If it happens to leave your windows and reaches another window, your MouseProc() will NOT get called. If you need to track the mouse OUTSIDE your window (which you are most likely trying to do since you want to know if the mouse has left your window), you must put your SetWindowsHookEx() call and your MouseProc() calls in a DLL.

This is a shortish DLL that will track if the mouse has left one of your windows for you:
Code:
#include<windows.inc>

HANDLE hHook,hWnd;

DllEntry (HINSTANCE hInst, LONG reason, LONG reserved)
{
hInstance=hInst;
return(TRUE);
}

MouseProc (LONG nCode,LONG wParam,LONG lParam)
{
CallNextHookEx(hHook,nCode,wParam,lParam)
/*if hWnd is NULL, we just go out
since we're not tracking anything*/
if(hWnd!=NULL)
    if(WindowFromPoint((MOUSEHOOKSTRUCT)lParam ->pt.x,
        (MOUSEHOOKSTRUCT)lParam -pt.y)!=hWnd)
        {
        PostMessage(hWnd,WM_MOUSELEAVE,NULL,NULL);
        hWnd=NULL;
        }
return(0);
}

InstallHook()
{
hWnd=NULL;
hHook=SetWindowsHookEx(WH_MOUSE,&MouseProc,hInstance,NULL);
}


UninstallHook()
{
UnhookWindowsHookEx(hHook);
}

TrackMouseLeave(HANDLE hwnd)
{hWnd=hwnd;}

You need to export InstallHook,UninstallHook, and TrackMouseLeave. Your WinMain should call InstallHook first, and then call UninstallHook. TrackMouseLeave() will cause WM_MOUSELEAVE to be sent to the window hwnd you pass to it. &quot;Information has a tendency to be free. Which means someone will always tell you something you don't want to know.&quot;
 
Typo!
Code:
MOUSEHOOKSTRUCT)lParam -pt.y
should be:
Code:
MOUSEHOOKSTRUCT)lParam ->pt.y

&quot;Information has a tendency to be free. Which means someone will always tell you something you don't want to know.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top