Why is there no WM_MOUSEENTER message?

Raymond Chen

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?

0 comments

Discussion is closed.

Feedback usabilla icon