如何从 WinForms 应用程序控制新流程窗口的大小和位置?

发布于 2024-09-11 12:18:42 字数 260 浏览 4 评论 0原文

我的 WinForms 应用程序使用 Process.Start() 在其本机应用程序中打开文件。我想将屏幕分成两半,在一半上显示我的 WinForms 应用程序,在另一半上显示新流程。我知道我可以使用 Process.MainWindowHandle 来获取窗口句柄,但是如何设置它的大小和位置?

我想我必须使用某种 Windows API,但是使用哪一种以及如何使用呢?由于这并不是真正的“在我的驾驶室”,我不确定是否(以及如何)需要在 64 位 Windows 上使用不同的 API。

My WinForms app uses Process.Start() to open files in their native app. I want to split the screen in half, showing my WinForms app on one half and the new process on the other. I know I can use Process.MainWindowHandle to get the window handle, but how can I set its size and position?

I imagine I have have to use some kind of Windows API, but which one and how? Since this is not really "in my wheelhouse", I am unsure of whether (and how) I need to use different APIs on 64bit Windows.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

濫情▎り 2024-09-18 12:18:42

所讨论的 Windows API 方法是 SetWindowPos。您可以像这样声明它:

[DllImport("user32.dll")]
private extern static bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);

并在这里阅读它:
http://msdn.microsoft.com/en-us/library/ms633545.aspx

添加

Process.MainWindowHandle 是您将使用的 hWnd 参数。
hWndInsertAfter 可能是您自己的 Form 句柄 (Form.Handle)。
您可以使用屏幕类型来访问有关桌面的信息:
http://msdn.microsoft.com/en-us /library/system.windows.forms.screen.aspx

添加了 Thomas 的评论

确保在调用 SetWindowPos 之前您 WaitForInputIdle。

Process process = Process.Start(...);
if (process.WaitForInputIdle(15000))
    SetWindowPos(process.MainWindowHandle, this.Handle, ...);

上面的 SetWindowPos 声明适用于 32 位和 64 位 Windows。

The Windows API method in question is SetWindowPos. You can declare it like so:

[DllImport("user32.dll")]
private extern static bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);

and read about it here:
http://msdn.microsoft.com/en-us/library/ms633545.aspx

Added

Process.MainWindowHandle is the hWnd parameter you will use.
hWndInsertAfter will probably be your own Form's handle (Form.Handle).
You can use the Screen type to access information about the desktop:
http://msdn.microsoft.com/en-us/library/system.windows.forms.screen.aspx

Added Thomas' comment

Make sure you WaitForInputIdle before calling SetWindowPos.

Process process = Process.Start(...);
if (process.WaitForInputIdle(15000))
    SetWindowPos(process.MainWindowHandle, this.Handle, ...);

The declaration for SetWindowPos above works for both 32- and 64-bit Windows.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文