October 13th, 2003

Why is there no WM_MOUSEENTER message?

There is a WM_MOUSELEAVE message. Why isn’t there a WM_MOUSEENTER message?

Because you can easily figure that out for yourself.

When you receive a WM_MOUSELEAVE message, set a flag that says, “The mouse is outside the window.” When you receive a WM_MOUSEMOVE message and the flag is set, then the mouse has entered the window. (And clear the flag while you’re there.)

Let’s illustrate this with our sample program, just to make sure you get the idea:

BOOL g_fMouseInClient;
void OnMouseMove(HWND hwnd, int x, int y, UINT keyFlags)
{
    if (!g_fMouseInClient) {
        g_fMouseInClient = TRUE;
        MessageBeep(0);
        TRACKMOUSEEVENT tme = { sizeof(tme) };
        tme.dwFlags = TME_LEAVE;
        tme.hwndTrack = hwnd;
        TrackMouseEvent(&tme);
    }
}
    case WM_MOUSELEAVE: g_fMouseInClient = FALSE; return 0;
    HANDLE_MSG(hwnd, WM_MOUSEMOVE, OnMouseMove);

This program beeps whenever the mouse enters the client area.

Exercise: If the program starts with the mouse already in the client area without moving, why do you get a beep?

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.