C++: Global Mouse hook and reset the position of window

322 views Asked by At

Now, I'm developing the app to reset the position of window. One thing to notice is that I used the global mouse hook to listen the desktop mouse event. Global Mouse hook is working well. My issue is that SetWindowPos() method is working strangely. The following is my code:

case WM_LBUTTONUP:
    mouselbut_clicked = false;

    if (mousewnd_drag)
    {
        TRACE("mouse - lbutton release\n");

        if ( window_moved )
        {
            ::SetWindowPos(hWnd, 0, 0, 0, 500, 500, SWP_ASYNCWINDOWPOS | SWP_NOZORDER);
            window_moved = false;
        }
     }
  break;

When I build the app, the window goes into the top-left corner of the desktop, but suddenly, it comes back into original position.

Would you like to help me to resolve this issue?

1

There are 1 answers

5
Anders On BEST ANSWER

Moving/resizing a window causes DefWindowProc to execute a internal moving or sizing modal loop. Control does not return to the application until the operation is complete and the exact behavior and message handling of it is not documented.

Have you tried calling CallNextHookEx before SetWindowPos?

As a hack you can try to delay your call to SetWindowPos until after your hook has returned (post a message to yourself or use a worker thread).

You could also try using a different hook to catch the end of the move operation: