简体   繁体   English

将表单绑定到另一个进程的窗口

[英]Binding a form to another process's window

For my project, I require to bind my Winform to another process's window, in this case the browser's page that has a title "Client", so that it's only able to move it in that window. 对于我的项目,我需要将我的Winform绑定到另一个进程的窗口,在这种情况下是浏览器的页面,其标题为“Client”,因此它只能在该窗口中移动它。 What would be the best and steady way to do this? 这样做最好,最稳定的方法是什么?

I've tried getting the window process by title, which succeeded. 我已经尝试通过标题获取窗口过程,该过程成功。 I got the window rect by using getWindowRect method but this didn't really seem to work as the form would not correctly bind to the form. 我通过使用getWindowRect方法得到窗口rect,但这似乎不起作用,因为窗体不能正确绑定到窗体。

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!");
}
if (!(rect2.Y >= rect1.Y && rect2.Y + rect2.Height <= rect1.Y + rect1.Height ...

Clearly your declaration for RECT is bogus. 很明显,您对RECT的声明是虚假的。 It doesn't have a Height or Width property, it doesn't behave like the .NET Rectangle type. 它没有Height或Width属性,它的行为与.NET Rectangle类型不同。 Always double-check your pinvoke declarations with good sources like the pinvoke.net website or the Pinvoke Interop Assistant tool. 始终使用pinvoke.net网站或Pinvoke Interop Assistant工具等良好来源仔细检查您的pinvoke声明。 A proper declaration is: 适当的声明是:

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

Adjust your if() statement accordingly. 相应地调整if()语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM