[ACCEPTED]-How do I get the window handle of the desktop?-desktop

Accepted answer
Score: 23

In light of a recent discussion on Meta complaining that questions like this one have "not been properly answered", I'm going to try and give answering this one a whirl. Not to imply that I think meklarian's answer is bad—in fact, far from it. But it's clearly been deemed unsatisfactory, so perhaps I can fill in some of the additional details.

Your problem results from a fairly widespread 82 confusion over what the desktop window actually 81 is. The GetDesktopWindow function does precisely what it's documented to do: it 80 returns a handle to the desktop window. This, however, is 79 not the same window that contains the desktop 78 icons. That's a completely different window that 77 appeared for the first time in Windows 95. It's 76 actually a ListView control set to the "Large 75 Icons" view, with the actual desktop 74 window as its parent.

Raymond Chen, a developer 73 on the Windows Shell team provides some 72 additional detail in the following Windows 71 Confidential article: Leftovers from Windows 3.0

[ . . . ]  While in 70 Windows 3.0, icons on the desktop represented 69 minimized windows, in Windows 95, the desktop 68 acted as an icon container.

The Windows 95 67 desktop was actually a window created by 66 Explorer that covered your screen (but sat 65 beneath all the other windows on your desktop). That 64 was the window that displayed your icons. There 63 was still a window manager desktop window 62 beneath that (the window you get if you 61 call Get­Desktop­Window), but you never 60 saw it because it was covered by the Windows 59 95 desktop—the same way that the wood paneling 58 in the basement of my colleague’s house 57 covered the original wall and the time capsule 56 behind the wall.

[ . . . ]

This desktop design has 55 remained largely unchanged since its introduction 54 in Windows 95. On a typical machine, the 53 original desktop is still there, but it’s 52 completely covered by the Explorer desktop.

In 51 summary, then, the window returned by the 50 GetDesktopWindow function is the actual desktop window, the only 49 one we had way back in Windows 3.0. The 48 Explorer desktop (the one that contains 47 all your icons) is merely another window 46 sitting on top of the desktop window (although 45 one that completely covers the original) that 44 wasn't added until Windows 95.

If you want 43 to get a handle to the Explorer desktop 42 window, you need to do some additional work 41 beyond simply calling the GetDesktopWindow function. In 40 particular, you need to traverse the child 39 windows of the actual desktop window to find the 38 one that Explorer uses to display icons. Do 37 this by calling the FindWindowEx function to get each window 36 in the hierarchy until you get to the one 35 that you want. It has a class name of SysListView32. You'll 34 also probably want to use the GetShellWindow function, which returns 33 a handle to the Shell's desktop window, to 32 help get you started.

The code might look 31 like this (warning: this code is untested, and 30 I don't recommend using it anyway!):

HWND hShellWnd = GetShellWindow();
HWND hDefView = FindWindowEx(hShellWnd, NULL, _T("SHELLDLL_DefView"), NULL);
HWND folderView = FindWindowEx(hDefView, NULL, _T("SysListView32"), NULL);
return folderView;

I noted 29 there that I don't actually recommend using 28 that code. Why not? Because in almost every case that you want to get a handle to the desktop window (either the 27 actual desktop window, or the Explorer desktop), you're doing something wrong.

This 26 isn't how you're supposed to interact with 25 the desktop window. In fact, you're not 24 really supposed to interact with it at all! Remember 23 how you learned when you were a child that 22 you're not supposed to play with things 21 that belong to other people without their 20 permission? Well, the desktop belongs to 19 Windows (more specifically, to the Shell), and 18 it hasn't given you permission to play with 17 its toys! And like any good child, the Shell 16 is subject to throwing a fit when you try 15 to play with its toys without asking.

The 14 same Raymond Chen has published another 13 article on his blog that details a very 12 specific case, entitled What's so special about the desktop window?

Beyond the example 11 he gives, this is fundamentally not the 10 way to do UI automation. It's simply too 9 fragile, too problematic, and too subject 8 to breaking on future versions of Windows. Instead, define 7 what it is that you're actually trying to 6 accomplish, and then search for the function 5 that enables you to do that.

If such a function 4 does not exist, the lesson to be learned 3 is not that Microsoft simply wants to make 2 life harder for developers. But rather that 1 you aren't supposed to be doing that in the first place.

Score: 8

If you want the Desktop window as defined 17 in GetDesktopWindow(), use that window handle. This 16 is the window handle you should use to look 15 for top-level windows and other related 14 activities.

What you're seeing in Spy++ is 13 just the content drawn as the desktop in 12 your session. If you use the auto-locate 11 in Spy++, you'll see that the SysListView32-declared 10 window is a child window of your explorer 9 shell. It is quite infrequent for someone 8 to need access to this window. Also, the 7 existence of this window may be subject 6 to changes between versions of windows.

Edit (additional info)

If 5 you are looking to interact or place things 4 on the actual shell desktop, you may be 3 better served by other APIs. Here are two 2 such APIs that can accomplish this, depending 1 on the target version of windows.

Windows Sidebar @ MSDN
This is available on Vista and Windows 7

Using the Active Desktop @ MSDN
This is available on Windows 2000 and XP, although frequently disabled by users and sysadmins.

More Related questions