How to get mouse events from outside of program window

瘦欲@ 提交于 2020-01-13 19:16:12

问题


I want to drag this character. Image :

so i use this method to get mouse position:

WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    ...
    switch (message){
    case WM_LBUTTONDOWN:
    case WM_MOUSEMOVE:
        GetCursorPos(&mousePosition);
        break;
    }
    ...
}

The program seems to be running well, but if the mouse is out of the characters location, it wouldn't follow the mouse cursor anymore.

How can I fix this problem?


回答1:


See SetCapture on MSDN:

Sets the mouse capture to the specified window belonging to the current thread. SetCapture captures mouse input either when the mouse is over the capturing window, or when the mouse button was pressed while the mouse was over the capturing window and the button is still down. Only one window at a time can capture the mouse.

SetCapture..ReleaseCapture lets you temporarily extend your mouse event handling to space outside the window [where the event originated from].

You have some sample/demo here:

switch (uMsg) 
{ 
   case WM_LBUTTONDOWN: 

        // Capture mouse input. 
        SetCapture(hwndMain); // <<--- Here we go


来源:https://stackoverflow.com/questions/23442725/how-to-get-mouse-events-from-outside-of-program-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!