In one of my programs, I need to test if the user is currently focusing the desktop/shell window. Currently, I’m using GetShellWindow() from user32.dll and compare the result to GetForegroundWindow().
This approach is working until someone changes the desktop wallpaper, but as soon as the wallpaper is changed the handle from GetShellWindow() doesn’t match the one from GetForegroundWindow() anymore and I don’t quite get why that is. (OS: Windows 7 32bit)
Is there a better approach to check if the desktop is focused? Preferably one that won’t be broken if the user changes the wallpaper?
EDIT: I designed a workaround: I’m testing the handle to have a child of class SHELLDLL_DefView. If it has, the desktop is on focus. Whilst, it’s working at my PC that doesn’t mean it will work all the time.
The thing changed a little bit since there are slideshows as wallpaper available in Windows 7.
You are right with WorkerW, but this works only with wallpaper is set to slideshow effect.
When there is set the wallpaper mode to slideshow, you have to search for a window of class
WorkerWand check the children, whether there is aSHELLDLL_DefView.If there is no slideshow, you can use the good old
GetShellWindow().I had the same problem some months ago and I wrote a function for getting the right window. Unfortunately I can’t find it. But the following should work. Only the Win32 Imports are missing:
In your case you would call
GetDesktopWindow(DesktopWindow.SHELLDLL_DefViewParent);to get the top-level window for checking whether it is the foreground window.