windows 如何枚举进程中的所有窗口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2281429/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:57:11  来源:igfitidea点击:

How to enumerate all windows within a process?

c#windowsprocess

提问by st78

I need to capture particular windows of 3rd party process. I can find main window handle as Process.MainWindowHandle, but what I can use to list other windows?

我需要捕获 3rd 方进程的特定窗口。我可以找到作为 Process.MainWindowHandle 的主窗口句柄,但是我可以用什么来列出其他窗口?

I am using C# / .NET

我正在使用 C#/.NET

采纳答案by st78

3rd party aplication launched other windows not as child windows.

第 3 方应用程序启动了其他窗口而不是子窗口。

It is possible to find out what is structure using Spy++ tool which comes with Visual Studio.

可以使用 Visual Studio 附带的 Spy++ 工具找出什么是结构。

After this, I was able to find necessary window using FindWindowEx function using WindowClassName (taken from Spy++): lastWindows = FindWindowEx(IntPtr.Zero, lastWindows, m.WindowClassName, null);

在此之后,我能够使用 FindWindowEx 函数使用 WindowClassName(取自 Spy++)找到必要的窗口: lastWindows = FindWindowEx(IntPtr.Zero, lastWindows, m.WindowClassName, null);

回答by Cory Charlton

The EnumChildWindowsfunction might help you out. The child windows could also have children and so on.

EnumChildWindows功能可以帮助你。子窗口也可以有子窗口等等。

There is also GetWindowand EnumThreadWindows

还有GetWindowEnumThreadWindows

Another post here with some more details: Get handles to all windows of a process

此处的另一篇文章有​​更多详细信息:获取进程的所有窗口的句柄

回答by Brian R. Bondy

Use the Win32 API EnumWindows(and if you want EnumChildWindows)

使用 Win32 API EnumWindows(如果你想要EnumChildWindows

[DllImport("user32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public static extern bool EnumWindows(EnumThreadWindowsCallback callback, IntPtr extraData);

Then check which process each window belongs to by using the Win32 API GetWindowThreadProcessId

然后使用Win32 API GetWindowThreadProcessId检查每个窗口属于哪个进程

[DllImport("user32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public static extern int GetWindowThreadProcessId(HandleRef handle, out int processId);