Dark Bit Factory & Gravity

PROGRAMMING => C / C++ /C# => Topic started by: ferris on September 07, 2009

Title: Detect if mouse has left window using winapi
Post by: ferris on September 07, 2009
So uh, yeah. Anyone know how this is done?
Title: Re: Detect if mouse has left window using winapi
Post by: Jim on September 07, 2009
You get a WM_MOUSELEAVE event posted to the WindowProc.
I assume you also stop receiving WM_MOUSEMOVE messages, and start getting them again when the mouse re-enters the window, because there's no equivalent WM_MOUSEENTER.

Jim
Title: Re: Detect if mouse has left window using winapi
Post by: ferris on September 07, 2009
Thanks Jim, and I tried that; it was a bit more complicated as you needed to use the TrackMouseEvent API and it was awful to make work...

Though I found a better method eventually (using the SetCapture API); this one works perfectly and I love it :D Here's the code, taken from my WndProc(), and slightly modified to be plugged into anyone else's (with minor changes):
Code: [Select]
    // Variable definitions
    RECT rect;
    POINT point;
Code: [Select]
        case WM_MOUSEMOVE:
            GetClientRect(hWnd,&rect);
            point.x = LOWORD(lParam);
            point.y = HIWORD(lParam);
            if(!PtInRect(&rect,point)) {
                ReleaseCapture();
                // Store mouse-in-window state and turn off mouse clicking states:
                gui_setmouseinwindow(false);
                gui_setmouseleftclicking(false);
                gui_setmouserightclicking(false);
            } else {
                if(GetCapture() != hWnd) SetCapture(hWnd);
                // Store the mouse-in-window state and mouse x and y coords:
                gui_setmouseinwindow(true);
                gui_setmousepos(LOWORD(lParam),HIWORD(lParam));
            }
            return 0;