0

对于我的项目,我需要将我的 Winform 绑定到另一个进程的窗口,在本例中是标题为“客户端”的浏览器页面,以便它只能在该窗口中移动它。最好和稳定的方法是什么?

我试过按标题获取窗口进程,它成功了。我通过使用 getWindowRect 方法获得了窗口矩形,但这似乎并没有真正起作用,因为表单无法正确绑定到表单。

IntPtr hWnd = FindWindow(null, this.windowTitle);
RECT rect1;
GetWindowRect(hWnd, out rect1);

RECT rect2;
GetWindowRect(this.Handle, out rect2);

if (!(rect2.Y >= rect1.Y && rect2.Y + rect2.Height <= rect1.Y + rect1.Height && rect2.X >= rect1.X && rect2.X + rect2.Width <= (rect1.X + rect1.Width) - (rect1.Width / 3)))
{
Console.WriteLine("You can't leave the window with this form! Naughty!");
}
4

1 回答 1

2
if (!(rect2.Y >= rect1.Y && rect2.Y + rect2.Height <= rect1.Y + rect1.Height ...

显然,您对 RECT 的声明是虚假的。它没有 Height 或 Width 属性,它的行为不像 .NET Rectangle 类型。始终使用 pinvoke.net 网站或 Pinvoke Interop Assistant 工具等良好来源仔细检查您的 pinvoke 声明。正确的声明是:

private struct RECT {
    public int Left, Top, Right, Bottom;
}

相应地调整您的 if() 语句。

于 2012-11-29T12:48:39.083 回答