×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Tracking the mouse

Tracking the mouse

Tracking the mouse

(OP)
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."

RE: Tracking the mouse

(OP)
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.

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."

RE: Tracking the mouse

(OP)
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."

RE: Tracking the mouse

(OP)
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:

#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.

"Information has a tendency to be free.  Which means someone will always tell you something you don't want to know."

RE: Tracking the mouse

(OP)
Typo!

MOUSEHOOKSTRUCT)lParam -pt.y

should be:

MOUSEHOOKSTRUCT)lParam ->pt.y


"Information has a tendency to be free.  Which means someone will always tell you something you don't want to know."

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close