jakaw
jakaw

Reputation: 19

SetWindowsHookEx with WH_MOUSE and TrackMouseEvent not capture WM_MOUSELEAVE

I use SetWindowsHookEx() to capture WM_MOUSEMOVE and than use TrackMouseEvent to capture the WM_MOUSELEAVE, but the WM_MOUSELEAVE is not captured by my MouseHook.

Using Spy++ I can se that WM_MOUSELEAVE is fired, but my MouseHook not capture the message. Why?

LRESULT CALLBACK MouseHook(int nCode, WPARAM wp, LPARAM lp)
{
    MOUSEHOOKSTRUCT   *pmh = (MOUSEHOOKSTRUCT   *) lp;

    if (nCode >= 0) {
        if( wp == WM_MOUSEMOVE) {
            if(!tracking){
                TRACKMOUSEEVENT tme;
                tme.cbSize = sizeof(TRACKMOUSEEVENT);
                tme.dwFlags = TME_LEAVE;
                tme.hwndTrack = pmh->hwnd;
                TrackMouseEvent(&tme);
                tracking = true;
            }
        }
    }

    if( wp == WM_MOUSELEAVE){
        if(tracking){
            TRACKMOUSEEVENT tme;
            tme.cbSize = sizeof(TRACKMOUSEEVENT);
            tme.dwFlags = TME_LEAVE & TME_CANCEL;
            tme.hwndTrack = pmh->hwnd;
            TrackMouseEvent(&tme);
            tracking = false;
        }
    }
    return CallNextHookEx(NULL, nCode, wp, lp);   
}

Upvotes: 0

Views: 922

Answers (1)

Jonathan Potter
Jonathan Potter

Reputation: 37192

WM_MOUSELEAVE isn't a true mouse message, it's generated by an internal timer proc that's established when you call TrackMouseEvent (that repeatedly checks if the mouse has left the window and sends a message to it when it does).

You could probably catch it with a WH_CALLWNDPROC hook.

Upvotes: 2

Related Questions