title: Get handle to previouly active window where my application may be a tray app or not in focus
body: <p>I need to get the handle to the current/previously active window on my desktop. Basically I am working on a c# Windows Forms application that can take screen shots.
  I am using the GetForegroundWindow PInvoke to get this handle, but if I call my code from a menu click, the active window seems to be set to that of the menu itself so the image produced is a small black rectangle.</p>
  
  <p>The application runs in the systray with a context menu. I have also implemented global hot keys and these work fine, but when I use the context menu with the mouse, the above scenario happens.  </p>
  
  <p>How to I keep track of the previously active window handle? Bear in mind my application runs as a tray icon so overriding wndproc never fires. I have tried a NativeForm and MessageOnly form  also, but these never fire unless the application has focus. </p>
  
  <p>Are we down to global hooks and external dll's?  Surely there has to be a simple way to solve this issue?  (any yes, I do need to implement menu items as well as keyboard shortcuts for a particular usability scenario.</p>
  
  <p>So, how do I keep track of the current / previous active window on my desktop from a c# app that has no window itself and has no focus?</p>
  
  <p>Many thanks for any help</p>