Author Topic: Detect if mouse has left window using winapi  (Read 3434 times)

0 Members and 1 Guest are viewing this topic.

Offline ferris

  • Pentium
  • *****
  • Posts: 841
  • Karma: 84
    • View Profile
    • Youth Uprising Home
Detect if mouse has left window using winapi
« on: September 07, 2009 »
So uh, yeah. Anyone know how this is done?
http://iamferris.com/
http://youth-uprising.com/

Where the fun's at.
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Detect if mouse has left window using winapi
« Reply #1 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
Challenge Trophies Won:

Offline ferris

  • Pentium
  • *****
  • Posts: 841
  • Karma: 84
    • View Profile
    • Youth Uprising Home
Re: Detect if mouse has left window using winapi
« Reply #2 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;
http://iamferris.com/
http://youth-uprising.com/

Where the fun's at.
Challenge Trophies Won: