windows 检索任何窗口的所有控件及其类型和值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3211408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Retrieve all the controls of any window with their types and value
提问by Zee99
My application is something like the Spy++ application: i want to be able to automatically retreive all the different controls of the active window (any application) and their children, and for each control i want to know the type, the name, and the value (caption or text).
我的应用程序类似于 Spy++ 应用程序:我希望能够自动检索活动窗口(任何应用程序)及其子项的所有不同控件,并且对于每个控件,我想知道类型、名称和值(标题或文字)。
I am using a C# windows app.
我正在使用 C# windows 应用程序。
what is the solution to iterate all the controls of the foreground window and their children (and so on) and retrieve name, type and value?
迭代前台窗口及其子项(等等)的所有控件并检索名称、类型和值的解决方案是什么?
回答by Alex K.
To enumerate top level windows use EnumWindows()
, to get their child windows use EnumChildWindows()
.
枚举顶级窗口的使用EnumWindows()
,以获取其子窗口的使用EnumChildWindows()
。
Using theHWND
s from the enumeration, a top level window with a title bars value can be read via GetWindowText()
, for other windows you can use the WM_GETTEXT
message, or depending on exactly what you want, a message specific to the windows class such as LB_GETTEXT
for a listbox.
使用HWND
枚举中的s,可以通过 读取具有标题栏值的顶级窗口GetWindowText()
,对于其他窗口,您可以使用该WM_GETTEXT
消息,或者根据您的需要,特定于窗口类的消息,例如LB_GETTEXT
列表框.
RealGetWindowClass()
will give you the windows class.
RealGetWindowClass()
会给你窗户类。
Window API reference; http://msdn.microsoft.com/en-us/library/ff468919%28v=VS.85%29.aspx
窗口 API 参考;http://msdn.microsoft.com/en-us/library/ff468919%28v=VS.85%29.aspx
回答by Iain Ward
Yes you will have to use the windows API if its a window thats not part of your current application. This will get you the currently active window:
是的,如果它的窗口不是您当前应用程序的一部分,您将不得不使用 windows API。这将为您提供当前活动的窗口:
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Text;
public class MainClass
// Declare external functions.
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
public static void Main() {
int chars = 256;
StringBuilder buff = new StringBuilder(chars);
// Obtain the handle of the active window.
IntPtr handle = GetForegroundWindow();
// Update the controls.
if (GetWindowText(handle, buff, chars) > 0)
{
Console.WriteLine(buff.ToString());
Console.WriteLine(handle.ToString());
}
}
}
It uses the GetWindowText() function to find the name of the window, so I assume it shouldn't be a problem to find out other properties of the windows such as its controls etc.
它使用 GetWindowText() 函数来查找窗口的名称,因此我认为查找窗口的其他属性(例如其控件等)应该不成问题。
回答by Trevor Balcom
There are a number of Win32 API functions you can use to write your own Spy++ program. This linkexplains how to write a Spy++ clone in Visual Basic. I know, you probably don't use Visual Basic, but this document does show you how to duplicate Spy++ using the Win32 API. It should not require much effort to translate this to C#.
您可以使用许多 Win32 API 函数来编写自己的 Spy++ 程序。 此链接解释了如何在 Visual Basic 中编写 Spy++ 克隆。我知道,您可能不使用 Visual Basic,但本文档确实向您展示了如何使用 Win32 API 复制 Spy++。将其转换为 C# 应该不需要太多努力。