问题描述
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowRect(HandleRef hWnd, out RECT lpRect);
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left; // x position of upper-left corner
public int Top; // y position of upper-left corner
public int Right; // x position of lower-right corner
public int Bottom; // y position of lower-right corner
}
foreach (Process pr in Process.GetProcesses())
{
RECT rc;
GetWindowRect(???, out rc);
我应该把为???? 。它告诉我,我必须把一个HandleRef对象,但我不知道如何从工艺方法HandleRef对象。
What should I put for " ??? " ? . It tells me I have to put a HandleRef object but I do not know how to get HandleRef object from Process method.
推荐答案
如果你需要一个窗口,在过程窗口坐标已经有其他方式获得,不需要枚举进程的窗口句柄。
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.
的
有关WPF应用程序,使用 WindowInteropHelper
For WPF applications, use WindowInteropHelper
的
如果你是试图枚举你不能直接从.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:
的
补充:
看起来像要列举所有的窗户和放大器;相关进程。使用 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.
的
的p / Invoke签名,请访问:
The P/Invoke signature is available here:
最后,你就可以用静态方法 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.
- 我使用的IntPtr,不是HandleRef。正如其他人所指出的,这可能会产生混淆的东西给你。
- 我没有指定
收益
属性。如果这是必需的,你应该能够重新添加在 - 我以管理员身份运行。有些事情可能,如果你是用户级权限运行运行不同
- I use IntPtr, not HandleRef. As other people have pointed out, this may be confusing things for you.
- I didn't specify a
return
attribute. If this is required, you should be able to add it back in. - I'm running as administrator; some things may run differently for you if you are running with user-level privileges.
的
这篇关于C# - user32.dll中 - GetWindowRect问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!