Reputation: 124790
So, I thought this would be simple and, well, I was wrong. Here is a simplified description of the problem:
I am writing a small application for our manufacturing folks that will grab a screenshot of the entire desktop as well as the foreground window when they click the application's icon in the system tray. Currently, I am using the Win32 method "GetforegroundWindow" in the MouseMove event of the NotifyIcon to save the foreground window handle and take the screenshot in the Click event.
This does work sometimes, but if I click the icon very quickly I actually capture the task bar instead of the foreground window. I am not sure why this is happening (I do understand that the task bar is a window, I don't understand why sometimes it seems to have focus in MouseMove before I have clicked), and I have had little luck using the EnumWindows method as well, likely because I do not completely understand how it works.
It would seem that, if I were able to get the z position of each window using only the window handle, this would be an easy problem to solve using EnumWindows. I have not found a method to do that however.
So, I ask you guys; how would you write a method to locate the foreground window reliably, given that it may not have focus at the time? Either my google-fu is failing me or the information on this is sparse. Thanks in advance.
Upvotes: 7
Views: 2962
Reputation: 69282
The taskbar is as valid a foreground window as any. When you click it, it will temporarily be the foreground window. And if you click Start and press Escape for example, it will be the foreground window until you click off of it.
You can probably use GetWindow with HWND_NEXT passing in the window handle of the taskbar.
Nevermind, since the taskbar is a topmost window, GetWindow (or GetNextWindow, etc) will operate differently. I would suggest revisiting the EnumWindows solution which is probably your best bet.
Upvotes: 1
Reputation: 1509
If the form you want the snapshot of is the same as the form linked to the task bar, you really do not need to use GetforegroundWindow. Just use the Form.hWnd and pass that into the function getting the snapshot. You may need to make it the top window by calling
[DllImport( "user32.dll" )]
public static extern IntPtr SetForegroundWindow( IntPtr hWnd );
or
[DllImport( "user32.dll" )]
public static extern bool BringWindowToTop( HandleRef hWnd );
If you want the whole desktop, then you probably just need to put in a Thread.Sleep to make sure the foreground window has had enough to come to the top before getting the desktop snapshot.
putting the src from my comment here for better printing
[DllImport( "user32.dll" )]
public static extern IntPtr GetForegroundWindow();
[DllImport( "user32.dll" )]
public static extern IntPtr GetActiveWindow();
// this or the next line not both
IntPtr curWindow = GetActiveWindow();
IntPtr curWindow = GetForegroundWindow();
BringWindowToTop( window );
System.Threading.Thread.Sleep( 500 );
Where the thread spleep gives the window enough time to come to the top of the Z order.
Upvotes: 0