系统相关
首页 > 系统相关> > 我如何从WinForms应用程序控制新进程窗口的大小和位置?

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

作者:互联网

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

我想我必须使用某种Windows API,但是哪种以及如何使用?由于这并不是真正的“麻烦”,因此我不确定在64位Windows上是否需要(以及如何使用)不同的API.

解决方法:

有问题的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

添加了托马斯的评论

在调用SetWindowPos之前,请确保您已等待WaitForInputIdle.

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

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

标签:32bit-64bit,windows,c,winforms
来源: https://codeday.me/bug/20191023/1916716.html