1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

C# 外部のアプリケーションのウインドウを無理やり前面に持ってくる

Last updated at Posted at 2023-04-19
.cs

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern IntPtr FindWindow(
           string lpClassName, string lpWindowName);

        [DllImport("user32.dll")]
        private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
        [DllImport("user32.dll")]
        private static extern bool IsIconic(IntPtr hWnd);
        // ShowWindowAsync関数のパラメータに渡す定義値
        private const int SW_RESTORE = 9;  // 画面を元の大きさに戻す

 // 外部プロセスのメイン・ウィンドウを起動するためのWin32 API
        [DllImport("user32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);

.cs

 //ウインドウ名は、ウインドウの左上に表示される文字列
 //WindowsFormApplicationでのForm.Textのこと

 //ウインドウ名からウインドウのハンドルを取得する。
 IntPtr hWnd = FindWindow(null, "ウインドウ名");
 if (hWnd != IntPtr.Zero)
 {
       //最小化されていれば元に戻す
       if (IsIconic(hWnd))
       {
           ShowWindowAsync(hWnd, SW_RESTORE);
       }
       //最前面に表示する
       SetForegroundWindow(hWnd);
 }

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?