我一直在尝试用 C# 编写一个简单的程序,以启动不同的软件,并将其移动到特定的屏幕,以便能够在总共有 12 个显示器的机器上自动设置不同的窗口。
大多数这些窗口都是在 Chrome 或 Internet Explorer 中启动的。
我用来移动应用程序的代码如下:
[DllImport("User32.dll")]
static extern int SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);
this.process = Process.Start(this.startInfo);
process.WaitForInputIdle();
SetForegroundWindow(this.process.MainWindowHandle);
Console.WriteLine("Process ID: "+ this.process.Handle);
this.process.Refresh();
Console.WriteLine("Main window handle: " + this.process.MainWindowHandle);
Point screenlocation = Screen.AllScreens[MonitorNum].Bounds.Location;
SetWindowPos(this.process.MainWindowHandle, -1, screenlocation.X, screenlocation.Y, Screen.AllScreens[MonitorNum].Bounds.Width, Screen.AllScreens[MonitorNum].Bounds.Height, 1);
它似乎在记事本上工作得很好,但是当它是一个浏览器时,MainWindowHandle 总是返回 IntPtr.Zero,即使我刷新了进程。
有什么建议吗?