title: Globally intercept window movement
body: <p>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 <code>HCBT_MOVESIZE</code> 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.</p>
  
  <p>The hooks are set from a DLL, and the callback function is within that DLL. This is what I've tried.</p>
  
  <ul>
  <li><code>WH_CALLWNDPROC</code>. It does alert me when a window is moved (<code>WM_MOVING</code> is received for windows from other applications), but I cannot change the contents of the message.</li>
  <li><code>WH_CALLWNDPROCRET</code> Same as <code>WH_CALLWNDPROC</code>.</li>
  <li>CBT hook <code>HCBT_MOVESIZE</code>. Event happens to late.</li>
  <li><code>WH_GETMESSAGE</code>. Never receive <code>WM_MOVE</code>, <code>WM_MOVING</code> or <code>WM_WINDOWPOSCHANGING</code>. This hook would allow me to change the messages.</li>
  </ul>
  
  <p><strong>Update</strong>: Windows event hooks seem to allow me to capture it:</p>
  
  <pre><code>hWinEventHook = SetWinEventHook(EVENT_SYSTEM_MOVESIZESTART,    
      EVENT_SYSTEM_MOVESIZEEND, NULL, WinEventProc, 
      0, 0, WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS);
  </code></pre>
  
  <p>However, this creates a different problem: changing the size of the window using <code>SetWindowPos()</code> does not work (it changes size alright, but immediately changes back to its previous size), even though I use <code>SWP_NOSENDCHANGING</code>. Ideas?</p>
  
  <p><strong>Update 2</strong>: 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?</p>
  
  <p>I have a CBT hook (it was there from earlier), and whenever <code>HCBT_ACTIVATE</code> is sent for a new window, I remove any previous subclassing using <code>SetWindowLongPtr()</code> (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.</p>