首先,你不需要
UpdateWindow
,呼叫
SetWindowPos
可能就够了。您只需要确保创建了窗口句柄
. 只需在调用之前添加以下行
设置窗口位置
application.WaitForInputIdle();
如果
WaitForInputIdle()
不适合你,你可以尝试以下方法:
while (application.MainWindowHandle == IntPtr.Zero)
{
await Task.Delay(100);
}
以下代码对我来说很好:
Process application = new Process();
application.StartInfo.UseShellExecute = false;
application.StartInfo.FileName = "notepad.exe";
if (application.Start())
{
application.WaitForInputIdle();
/* Optional
while (application.MainWindowHandle == IntPtr.Zero)
{
await Task.Delay(100);
} */
Rectangle monitor = Screen.AllScreens[1].Bounds; // for monitor no 2
SetWindowPos(
application.MainWindowHandle,
IntPtr.Zero,
monitor.Left,
monitor.Top,
monitor.Width,
monitor.Height,
SWP_NOZORDER | SWP_NOSIZE | SWP_SHOWWINDOW);
}
请注意,这只会设置窗口的位置,而不会设置其大小。如果你想改变大小,你需要删除
SWP_NOSIZE