I'm running Windows 7 and I want my program to detect when the desktop is in focus, including when I press the desktop key (minimize all windows and show desktop).
I used:
if(GetForegroundWindow() == GetShellWindow())
This works only when another window is in focus and I click on the desktop (GetForegroundWindow() returned (int)524654).
When I click the desktop button, however, it doesn't work. I figured this would be the same as:
if(GetForegroundWindow() == NULL)
..but that's apparently not the case. After pressing the desktop button the function returns (int)328192.
I can't just use these values because they change. Is there a function that returns what GetForegroundWindow() returns after the desktop button is pressed?
Ah, I wasn't aware the taskbar could be in focus. I was, however, well aware that they're two different things, thanks. Mind if I ask how you check if focus is set to the taskbar, then?
After pressing the button focus only stays on the taskbar momentarily (GetForegroundWindow() returns a third value) then changes to the second value I mentioned in my original post.
My Windows 7 (ASUS Eee) and Windows 8 (ASUS X502C!) PCs both have helper apps running on the desktop, so they're what get focus if nothing else is about and I haven't click on the actual desktop. ("Eee Docking" in the first instance; Asus Instant on Config in the latter.)
But when I kill the helper apps it's the Systray (class "Shell_TrayWnd") that gets focus in the Windows 8 case (I haven't installed the optional Start button) but the "Start" button (class "Button) for Windows 7.
Andy
PS Basic program I used to monitor what was going on.
Thanks Andy. I used your program and it came in handy; it turns out the start button wasn't the culprit. I'll be able to do what I need by checking when the foreground hwnd's classname is "WorkerW" - the Windows 7 version of progman (program manager for the shell). I guess the taskbar/start button are child classes of the program manager.. I'm going to download Spy++ to see what's going on here.
Edit: have you got wallpaper enabled on your PC? If so, see linked stackoverflow.com post for more info.
When there is set the wallpaper mode to slideshow, you have to search for a window of class WorkerW and check the children, whether there is a SHELLDLL_DefView. If there is no slideshow, you can use the good old GetShellWindow().