更新时间:2022-11-08 17:22:39
如果你需要一个窗口,在过程窗口坐标已经有其他方式获得,不需要枚举进程的窗口句柄。
If you need window coordinates for a window already in your process, there are other ways to get a window handle that don't require enumerating processes.
有关的WinForms窗口,使用处理
属性。
For WinForms windows, use the Handle
property.
的 System.Windows.Forms.Control的... handle属性@ MSDN
有关WPF应用程序,使用 WindowInteropHelper
For WPF applications, use WindowInteropHelper
的System.Windows.Interop ... WindowInteropHelper类@ MSDN
如果你是试图枚举你不能直接从.NET访问窗口;从创建一个***窗口出你的代码的范围第三方控制说,你可能希望通过在Win32 EnumWindows的
函数来枚举。
If you are trying to enumerate windows that you aren't able to access directly from .NET; say from a 3rd party control that creates a top-level window out of scope of your code, you may wish to enumerate via the win32 EnumWindows
function.
签名的p / Invoke的EnumWindows的都可以在这里:
Signatures for P/Invoke for EnumWindows are available here:
的的User32.dll EnumWindows的@ pinvoke.net
补充:
看起来像要列举所有的窗户和放大器;相关进程。使用 EnumWindows的
,然后调用 GetWindowThreadProcessId
来获得相关的过程和放大器;每个窗口非托管线程ID。
Looks like you want to enumerate all the windows & associated processes. Use EnumWindows
, then call GetWindowThreadProcessId
to get the associated Process & Unmanaged Thread ID for each window.
的 GetWindowThreadProcessId的(Win32)@ MSDN
的p / Invoke签名,请访问:
The P/Invoke signature is available here:
的User32.dll GetWindowThreadProcessId @ pinvoke.net
最后,你就可以用静态方法 GetProcessById
A Process对象。
Last, you can get a Process object via the static method GetProcessById
.
Process.GetProcessById @ MSDN
添加(#2):
下面是一个可以枚举窗口很短的控制台程序,过程和放大器;线程ID。有您的片段有一些区别。
Here's a short console program that can enumerate windows, process & thread ids. There are a few differences from your snippet.
收益
属性。如果这是必需的,你应该能够重新添加在return
attribute. If this is required, you should be able to add it back in.