且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C# - user32.dll中 - GetWindowRect问题

更新时间: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.

EnumWindows的(Win32的)@ MSDN

签名的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.


  1. 我使用的IntPtr,不是HandleRef。正如其他人所指出的,这可能会产生混淆的东西给你。

  2. 我没有指定收益属性。如果这是必需的,你应该能够重新添加在

  3. 我以管理员身份运行。有些事情可能,如果你是用户级权限运行运行不同

  1. I use IntPtr, not HandleRef. As other people have pointed out, this may be confusing things for you.
  2. I didn't specify a return attribute. If this is required, you should be able to add it back in.
  3. I'm running as administrator; some things may run differently for you if you are running with user-level privileges.

C#源代码示例@ gist.github