-3

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?

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
falcon.guru
  • 33
  • 1
  • 4

1 Answers1

0

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:

Anders
  • 97,548
  • 12
  • 110
  • 164
  • Hey, Anders I have question with your answer. When you said "Have you tried calling CallNextHookEx before SetwindowPos", can you give me more detail? – falcon.guru Aug 29 '17 at 14:07
  • And one more thing to inject is that when I added the SetWindowPos method into WM_LBUTTONUP case, not in If case, it works well. Would you like to explain why this happens? – falcon.guru Aug 29 '17 at 14:10
  • A hook should normally call CallNextHookEx so other hooks also get the message. If another hook also did something similar you would only "win" if you performed your action after the other hooks. If is not possible to answer your other question because you have not posted all the code. – Anders Aug 29 '17 at 15:24
  • Hi, Anders I have one thing to ask. Can I use two hook procedure concurrently? Such as MouseProc and CBTProc? – falcon.guru Aug 30 '17 at 03:14
  • Hey, Anders Can you help me once again? I wanna know if there's a crash between system original mouse events and global mouse hooks like mouse move or left button up. – falcon.guru Aug 30 '17 at 20:43