I am having trouble getting a global system hook to work. I want to be notified whenever a window is moving, as early as possible, and change the window size. This means the CBT hook HCBT_MOVESIZE
won't cut it, it only happens after the window has been moved. I want to hook the actual movement of the window, and be able to change the window size during the move.
The hooks are set from a DLL, and the callback function is within that DLL. This is what I've tried.
WH_CALLWNDPROC
. It does alert me when a window is moved (WM_MOVING
is received for windows from other applications), but I cannot change the contents of the message.WH_CALLWNDPROCRET
Same asWH_CALLWNDPROC
.- CBT hook
HCBT_MOVESIZE
. Event happens to late. WH_GETMESSAGE
. Never receiveWM_MOVE
,WM_MOVING
orWM_WINDOWPOSCHANGING
. This hook would allow me to change the messages.
Update: Windows event hooks seem to allow me to capture it:
hWinEventHook = SetWinEventHook(EVENT_SYSTEM_MOVESIZESTART,
EVENT_SYSTEM_MOVESIZEEND, NULL, WinEventProc,
0, 0, WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS);
However, this creates a different problem: changing the size of the window using SetWindowPos()
does not work (it changes size alright, but immediately changes back to its previous size), even though I use SWP_NOSENDCHANGING
. Ideas?
Update 2: Subclassing seems to work, however Visual Studio crashes after each program run (so does a lot of other windows). It works well if I place breakpoints and walk through the "unsubclassing", but not when I let the program run by itself. Ideas?
I have a CBT hook (it was there from earlier), and whenever HCBT_ACTIVATE
is sent for a new window, I remove any previous subclassing using SetWindowLongPtr()
(this has to run on 64-bit as well), and then subclass the new window. If I put a breakpoint anywhere, and immediately resume the session when it breaks, everything works fine. However, when I do not have any breakpoints, Visual Studio crashes when the program exits.