3

私は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 を返します。

何かアドバイス?

4

1 に答える 1