EnumWindows返回句柄的顺序有意义吗? [英] Is the order in which handles are returned by EnumWindows meaningful?

查看:99
本文介绍了EnumWindows返回句柄的顺序有意义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从一些初步测试看来, EnumWindows 总是以相反的实例化顺序返回窗口,即最近实例化的窗口在前。这是一个有效的观察结果吗?如果是这样,则在所有Windows版本中都适用吗?

From a couple of preliminary tests it seems that EnumWindows always returns windows in reverse instantiation order, i.e. most recently instantiated window first. Is that a valid observation? If so, is it true across all versions of Windows? And is this a reliable assumption, i.e. is that behaviour documented somewhere?

上下文:我正在处理我触发第三方应用程序打开多个非模式窗口的情况,一旦它们打开,我需要向这些窗口发送一些窗口消息,但是我无法确定将它们识别为它们的窗口的可靠方法类及其标题将有所不同,我也不知道它们的预期坐标。但是,如果我可以依靠 EnumWindows 的上述行为,我可以简单地使用 EnumWindows 返回的第一个句柄,该句柄的类和字幕符合我的期望。这仍然留下了一些假设的漏洞,但我认为这已经足够了。仍然欢迎其他建议。

Context: I'm dealing with a situation where I am triggering a third-party application to open several non-modal windows and I need to send some window messages to those windows once they're open, yet I have no sure-fire way of identifying them as neither their window classes nor their captions will differ and I also do not know their expected coordinates. However, if I could rely on the above behaviour of EnumWindows I could simply use the first handle returned by EnumWindows whose class and caption match my expectation. That still leaves some hypothetical loop holes but I think it will be good enough. Alternative suggestions welcome nevertheless.

推荐答案

它以Z顺序返回它们。首先是设置了 WS_EX_TOPMOST 的最上面的窗口,直到设置了 WS_EX_TOPMOST c的最底部的窗口,然后是顶部没有 WS_EX_TOPMOST 的大多数窗口,但是到没有 WS_EX_TOPMOST 的最底部窗口。请注意,可见性不是决定性因素,因此在Z轴顺序中比可见窗口高的不可见窗口仍会出现在它之前。

It returns them in Z order. First the top-most window with WS_EX_TOPMOST set, until the bottom-most window with WS_EX_TOPMOST set, then the top-most window without WS_EX_TOPMOST, though to the bottom-most window without WS_EX_TOPMOST. Note that visibility is not a determining factor, so an invisible window that's higher in the Z-order than a visible window will still appear before it.

EDIT

您极不可能根据需要使用它,只需从 EnumWindows 。您的新窗口不仅不大可能会成为第一个返回窗口,而且您在竞争中会同时打开其他窗口。但是,您可以保留该应用程序的所有已知窗口的列表,当需要查找新打开的窗口时,请调用 EnumWindows 并将窗口句柄与其中的窗口句柄进行比较。您的清单。当您找到一个具有正确的类和标题(甚至可以使用 GetWindowThreadProcessID 检查它是否属于正确的进程)的情况下,其中的不是您的列表,然后您便找到了新窗口。

It's highly unlikely that you could use this as you want, just taking the first return from EnumWindows. Not only is your new window unlikely to be the first return, but you'd have a race condition where other windows could be opened in the meantime. You could, however, keep a list of all known windows for the application, and when you need to find a newly opened window, call EnumWindows and compare the window handles to those in your list. When you find one that has the correct class and caption (you might even check that it belongs to the right process with GetWindowThreadProcessID) that is not in your list, then you've found the new window.

不过,出于您的目的,安装CBT挂钩并注意HCBT_CREATEWND通知可能会为您提供更好的服务。请参阅 SetWindowsHookEx()<上的MSDN帮助。 / code> CBTProc 回调以获取更多信息。

For your purposes, though, you may be even better served by installing a CBT hook and watching for the HCBT_CREATEWND notification. See MSDN help on SetWindowsHookEx() and the CBTProc callback for more information.

枚举顺序的确定性

此问题的许多评论和其他答案都提到了MSDN中缺少有关 EnumWindows 返回窗口句柄。实际上, EnumWindows EnumWindowsProc 回调都对此问题保持沉默。我提供以下证据:

A number of comments and other answers to this question have mentioned a lack of precise documentation in MSDN about the order in which EnumWindows returns window handles. And indeed, the pages on EnumWindows and the EnumWindowsProc callback are both quite silent on the issue. I offer as evidence the following:


  1. A C ++问题与解答在MSDN杂志上的文章确实指出:


EnumWindows以自上而下的Z顺序枚举窗口

EnumWindows enumerates the windows in top-down Z-order


  • EnumChildWindows 暗示了该命令在备注部分:

  • The page on EnumChildWindows alludes to the order in the remarks section:


    在枚举过程中以Z顺序移动或重新定位的子窗口将被正确枚举。

    A child window that is moved or repositioned in the Z order during the enumeration process will be properly enumerated.

    这意味着顺序是Z顺序相关的。而且,由于在 hWndParent 参数的说明中这样说:

    This implies that the order is Z-order dependent. And since, in the description of the hWndParent parameter, it says this:


    如果此参数为NULL,此功能等效于EnumWindows。

    If this parameter is NULL, this function is equivalent to EnumWindows.

    一个人可以假定,相同的逻辑和顺序适用于 EnumWindows

    one can assume that the same logic and ordering applies to EnumWindows.

    当然,这在所有方面都是非常学术性的,因为 EnumWindows 可能不是解决问题的最佳解决方案OP的问题-至少 EnumThreadWindows 可能更合适-但我认为这对其他可能遇到这篇文章的人来说值得一提。

    Of course, this is all very academic at this point, since EnumWindows is probably not the best solution for the OP's problem—at the very least EnumThreadWindows would probably be a better fit—but I thought it was worth mentioning for other people who might come across this post.

    这篇关于EnumWindows返回句柄的顺序有意义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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