如何获取桌面的窗口句柄? [英] How do I get the window handle of the desktop?

查看:170
本文介绍了如何获取桌面的窗口句柄?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Windows API提供API GetDesktopWindow(),它返回窗口句柄

The Windows API provides an API GetDesktopWindow( ) which returns the window handle

但我用Spy ++测试,我发现桌面的窗口句柄和Windows桌面的窗口句柄是不一样的。

But I tested with Spy++ and I find that the window handle of the desktop and the window handle of the "Windows Desktop" is not the same.

由于Windows桌面是列表视图,我需要执行以下操作:

As the "Windows Desktop" is a list view, do I need to do the following

1) HANDLE hWnd = GetDesktopWindow() ;
2) FindWindow(hWnd, ..... ) with the SyslistView32 as the Window class.

一旦我得到窗口句柄,我想使用SendMessage()获取选择的文件名,所选文件的数量等。

Once I get the Window handle, I want to use SendMessage() for operations like getting selected file name, the number of files selected , etc.

请提供您的意见。我这样做使用Windows SDk

Please give your opinions. I am doing this using the Windows SDk

推荐答案

鉴于抱怨像这样的问题没有得到正确回答,这个问题没有得到解决,我要试着回答这一个旋风。不是暗示我认为 meklarian的回答是坏事实,从它。但是它显然被认为不令人满意,所以也许我可以填写一些额外的细节。

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, from from it. But it's clearly been deemed unsatisfactory, so perhaps I can fill in some of the additional details.

您的问题是由于相当广泛的混乱,窗口实际上是。 GetDesktopWindow 函数 它记录要做的事情:它返回一个句柄到桌面窗口。但是,这不是包含桌面图标的同一窗口。这是一个完全不同的窗口,第一次出现在Windows 95.它实际上是一个 ListView 控制设置为大图标视图,实际桌面窗口作为其父。

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

Windows Shell团队的开发人员Raymond Chen在以下Windows机密文章中提供了一些其他详细信息: Windows防火墙

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


> [。 。 。] 在Windows 3.0中,桌面上的图标代表最小化的窗口,在Windows 95中,桌面作为图标容器。

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

Windows 95桌面实际上是一个由资源管理器覆盖你的屏幕(但坐在桌面上的所有其他窗口下)。这是显示您的图标的窗口。还有一个窗口管理器桌面窗口下面(你得到的窗口,如果你调用GetDesktopWindow),但你从来没有看到它,因为它覆盖了Windows 95桌面 - 相同的方式,在我的同事的房子的地下室覆盖了原来的墙壁和墙后面的时间胶囊。

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

[。 。 。]

这个桌面设计自从在Windows 95中引入以来基本没有改变。在一台典型的机器上, ,但它完全被Explorer桌面覆盖。

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

总之,然后, GetDesktopWindow 函数是实际的桌面窗口,这是我们在Windows 3.0中唯一的一个。 Explorer桌面(包含所有图标的桌面)只是位于桌面窗口顶部的一个窗口(尽管它完全覆盖了原始窗口),但在Windows 95之前未添加。

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

如果你想得到Explorer桌面窗口的句柄,你需要做一些额外的工作,而不仅仅是调用 GetDesktopWindow 函数。特别是,您需要遍历实际桌面窗口的子窗口,找到Explorer用来显示图标的窗口。通过调用 FindWindowEx function 来获取层次结构中的每个窗口,直到找到所需的窗口。它有一个类名 SysListView32 。您还可能需要使用 GetShellWindow

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

代码可能如下所示(警告:c $ c> function ,它返回Shell的桌面窗口的句柄,这个代码是未经测试的,我不建议使用它。):

The code might look like this (warning: this code is untested, and 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 there that I don't actually recommend using that code. Why not? Because in almost every case that you want to get a handle to the desktop window (either the actual desktop window, or the Explorer desktop), you're doing something wrong.

这不是你应该如何与桌面窗口进行交互。事实上,你真的不应该与它进行交互!记住你是怎么知道当你是一个孩子,你不应该与没有他人许可的属于他人的东西玩?好吧,该桌面属于Windows(更具体地来说,是命名为Shell),它没有授予您与其玩具一起玩的权限!和任何好孩子一样,当你尝试玩它的玩具而不问时,壳牌会被扔一个合适。

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

同一位Raymond Chen在他的博客上发表了另一篇文章,详细描述了一个非常具体的案例,题为桌面窗口有什么特别之处?

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

超越他给出的例子,这根本不是做UI自动化的方式。它太简单了,太有问题,并且在未来版本的Windows上也会受到影响。相反,定义什么是你实际尝试完成,然后搜索的功能,使您能够做到这一点。

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

如果这样的函数不存在,那么学习的教训不是微软只是想让开发人员更难。而是你不应该这样做。

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

这篇关于如何获取桌面的窗口句柄?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆