简体   繁体   English

我是否可以始终确定通过FindWindowEx找到的控件的层次顺序?

[英]Can I always be sure of the hierarchical order of controls found via FindWindowEx?

What am I doing? 我在做什么?

I am automating a 3rd party application. 我正在自动化第三方应用程序。 The login window looks like this 登录窗口如下所示

在此输入图像描述

Note: there is a background image embed within the form and both the USERNAME and PASSWORD are actually part of an image. 注意:表单中嵌入了背景图像, USERNAMEPASSWORD实际上都是图像的一部分。 Just in case anyone wonders why the below classes treeview from Window Detective doesn't have, possibly » Label controls. 万一有人想知道为什么来自Window Detective的以下类树视图没有,可能是» Label控件。

在此输入图像描述

Where am I at? 我在哪里?

I don't have any problems getting handles of components. 获取组件句柄时没有任何问题。 I mean, I can generally access each window/control using FindWindowEx function . 我的意思是,我通常可以使用FindWindowEx函数访问每个窗口/控件

I can implement a GetWindowTextLength and/or GetWindowText functions for buttons to distinguish which one I have got a handle of. 我可以为按钮实现GetWindowTextLength和/或GetWindowText函数,以区分我有哪个句柄。 While this approach works fine with Button s ( .Caption ) it would not benefit me when working with Edit controls. 虽然这种方法适用于Button s( .Caption ),但在使用Edit控件时这对我没有好处。 It seems that the Edit controls do not have any unique properties to distinguish which one is which. 似乎Edit控件没有任何唯一的属性来区分哪一个是哪个。 ( correct me if I am wrong )... 纠正我,如果我错了 )......

The problem 问题

Assume that I need to know at the compile stage which Edit control I am going to be handling, so I don't send a password to the username and other way around . 假设我需要在编译阶段知道我将要处理哪个Edit控件, 所以我不会向用户名和其他方式发送密码 There are only two but like I've said I'm not sure how to get the right one. 只有两个,但我说我不知道​​如何得到正确的

I wouldn't mind figuring it out at run-time but I am unsure of how to distinguish the difference without sending a message and actually visually identify which one is which... 我不介意在运行时搞清楚但是我不确定如何在不发送消息的情况下区分差异并且实际上可视地识别哪个是哪个...

Current solution 当前解决方案

I wouldn't really call this a solution but for now I am relying only on the fact that each time I run my code I always get a handle of the second ( password ) Edit control as the first handle returned. 我不会真的称之为解决方案但是现在我只依赖于这样一个事实:每次运行我的代码时,我总是得到第二个( 密码Edit控件的句柄作为第一个句柄返回。

Question

Can I be 100% sure the second Edit control will always be returned first in the hierarchy returned by the FindWindowEx function? 我可以100%确定第二个Edit控件将始终在FindWindowEx函数返回的层次结构中首先返回吗?

If someone can confirm my idea than I'd already have had a solution BUT if I can't always expect the second Edit control to be returned then I would like to hear some insight on how to handle this situation. 如果有人可以确认我的想法,而不是我已经有了解决方案但是如果我不能总是期望返回第二个Edit控件那么我想听听一些关于如何处理这种情况的见解。

Note: I didn't think any code is actually required for my question but if anyone would like to see the code then please leave a comment and I will add it to the question. 注意:我认为我的问题实际上并不需要任何代码,但如果有人想查看代码,请发表评论,我会将其添加到问题中。

Thanks a lot for your time. 非常感谢你的时间。

Each child HWND has a property called ID . 每个子HWND都有一个名为ID的属性。 Use GetWindowLongPtr( hWnd, GWLP_ID ) to get it. 使用GetWindowLongPtr( hWnd, GWLP_ID )来获取它。 Depending on how the target application is coded, that ID may be a way to distinguish between child control. 根据目标应用程序的编码方式,该ID可能是区分子控件的一种方式。 Knowing the ID you can get the child HWND with the GetDlgItem API (works fine when the parent is not a DialogBox, the API should have been called GetChildByID ). 知道ID您可以使用GetDlgItem API获取子HWND (当父级不是DialogBox时,工作正常,API应该被称为GetChildByID )。

Beware: some target applications really use random/unreliable values for IDs. 注意:某些目标应用程序确实使用ID的随机/不可靠值。

It seems that there is a general consensus: Enumeration APIs rely on the Z-Order. 似乎有一个普遍的共识:枚举API依赖于Z-Order。 See for example that SO answer (maybe making your question some sort of 'duplicate') 例如,参见SO 答案 (可能会使您的问题变得某种'重复')

While some applications may "play" with the Z-Order of their child windows, in the standard case they won't, which means that the first created child window is at the top of the Z-order, and the last created one is at the bottom. 虽然某些应用程序可以“播放”其子窗口的Z顺序,但在标准情况下它们不会,这意味着第一个创建的子窗口位于Z顺序的顶部,最后创建的子窗口是在底部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM