Check If Current Window Is Desktop?

I've tried googling for this but it seems this question remains unanswered. Is there anyway to check if the user's current active window is the desktop?
If you have the handle for the current active window then you can compare it with the handle of the desktop.
closed account (E0p9LyTq)
I don't believe the desktop is ever active as app windows are considered active.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <Windows.h>

int main ()
{
   HWND desktop = GetDesktopWindow();

   for (int i = 0; i < 20; i++)
   {
      HWND window = GetForegroundWindow();
      if (desktop == window)
      {
         std::cout << "Desktop Active!\n";
      }
      else
      {
         std::cout << "Desktop NOT Active!\n";
      }

      Sleep(1000);
   }
}


This always reports the desktop is inactive, even when all windows are minimized to the taskbar and only the desktop is visible. Doing a net search doesn't bring up any methods either.

Using GetActiveWindow() instead of GetForegroundWindow() gives the same output.

Maybe also there is some Win32 "trick" I am missing and it is quite easy to check if the Desktop is the active window.

Well, it would help if I actually checked each loop for what the active window is, code revised. Still won't detect the desktop as being the active window. Ooops! :|
Last edited on
closed account (E0p9LyTq)
Even setting the Desktop to the foreground before checking what the active window is won't show the Desktop as being the active window:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <Windows.h>

int main ()
{
   HWND desktop = GetDesktopWindow();

   SetForegroundWindow(desktop);

   for (int i = 0; i < 20; i++)
   {
      HWND window = GetForegroundWindow();
      if (desktop == window)
      {
         std::cout << "Desktop Active!\n";
      }
      else
      {
         std::cout << "Desktop NOT Active!\n";
      }

      Sleep(1000);
   }
}



Well, it would help if I actually checked each loop for what the active window is, code revised. Still won't detect the desktop as being the active window. Ooops! :|
Last edited on
Still won't detect the desktop as being the active window.

That's because it's actually a child of the desktop which gets the focus.

On my Windows 10 PC, it's a child window named "Program Manager"of window class "Progman", but if the system is showing wallpaper then different windows are involved.

This problem was discussed on this site a few years ago:

HWND of desktop
http://www.cplusplus.com/forum/windows/112197/

Andy
Last edited on
closed account (E0p9LyTq)
That's because it's actually a child of the desktop which gets the focus.


Something I mentioned in passing, but didn't remember the exact details. Your link provided that.
Topic archived. No new replies allowed.