title: Catching Minimize, Maximize, Resize, Keyboard focus changing, etc
body: <p>I'm currently coding an application that needs to catch every action done by the user on his window handle.</p>
  
  <p>I needed the following event to be raised:</p>
  
  <ul>
  <li>Window resized, maximized, minimized or moved</li>
  <li>User changed the active window</li>
  <li>User changed the window handle's keyboard focused</li>
  </ul>
  
  <p>To do that, I tried a lot of solutions, but in vain. First, I used a Timer that poll every 100ms the foreground window (using <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms633505%28v=vs.85%29.aspx" rel="nofollow">GetForegroundWindow()</a> ) and the keyboard focused handle using <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms681956%28v=vs.85%29.aspx" rel="nofollow">AttachThreadInput()</a> coupled with <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms646294%28v=vs.85%29.aspx" rel="nofollow">GetFocus()</a> function.</p>
  
  <p>But this solution was not very convenient and I prefered a cleaner solution using UIAutomation provided by .NET Framework. But I realized it used a lot of CPU and was too slow for my purpose, and the event was called sometimes 3 or 4 times when I switched to another window handle.</p>
  
  <p>Concerning window resize, maximize, etc. I did a timer as well (but not very truthful), and tried to get working some Hooking technics like CBT hook and Shell hook. Unfortunately, I found that this kind of hook (global hook) isn't supported by C#.</p>
  
  <p>I'm looking for a stable and reliable code for this part of my program. Thanks in advance.</p>