So, I thought it would be simple and, well, I was wrong. Here is a simplified description of the problem:
I am writing a small application for our production specialists, which will capture a screenshot of the entire desktop, as well as the foreground window, when they click the application icon on the taskbar. I am currently using the Win32 method "GetforegroundWindow" in the MouseMove NotifyIcon event to save a handle to the foreground window and take a screenshot in the Click event.
This sometimes works, but if I quickly click on the icon, I will actually grab the taskbar instead of the foreground window. I'm not sure why this is happening (I understand that the taskbar is a window, I don’t understand why sometimes it seems like I have focus in MouseMove before I clicked), and I was not lucky using the EnumWindows method as well. probably because I do not quite understand how this works.
It would seem that if I could get the z position of each window using only the window handle, this would be an easy problem to solve with EnumWindows. However, I did not find a way to do this.
So, I ask you guys; how would you write a method for reliably defining a foreground window, given that it may not have focus at the time? Either my google-fu fails, or information about it is sparse. Thanks in advance.
c # interop
Ed S.
source share