PowerShellでChromeの2ウィンドウ位置を制御する

フルスクリーンで開く

# Chromeをモニタ1(0,0)でフルスクリーン
Start-Process "chrome.exe" -ArgumentList "--new-window https://example.com --start-fullscreen"
Start-Sleep -Milliseconds 500
Add-Type @"
    using System;
    using System.Runtime.InteropServices;
    public class Win32 {
        [DllImport("user32.dll")]
        public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
    }
"@
$hwnd = (Get-Process -Name "chrome" | Select-Object -First 1).MainWindowHandle
[Win32]::SetWindowPos($hwnd, 0, 0, 0, 1920, 1080, 0x0040)

# Chromeをモニタ2(1920,0)でフルスクリーン
Start-Process "chrome.exe" -ArgumentList "--new-window https://example.org --start-fullscreen"
Start-Sleep -Milliseconds 500
$hwnd2 = (Get-Process -Name "chrome" | Where-Object { $_.MainWindowHandle -ne $hwnd } | Select-Object -First 1).MainWindowHandle
[Win32]::SetWindowPos($hwnd2, 0, 1920, 0, 1920, 1080, 0x0040)

動作

--start-fullscreenでChromeが各モニタでフルスクリーン表示。
SetWindowPosでモニタ1(0,0)とモニタ2(1920,0)にウィンドウを配置。
各モニタの解像度(例: 1920x1080)に合わせてcx、cyを設定。

注意

モニタ解像度: 1920x1080を仮定。異なる場合、Get-CimInstance Win32_VideoControllerで確認し、座標・サイズを調整。

タイミング: フルスクリーン切り替えに時間がかかる場合、Start-Sleepのミリ秒を増やす(例: 1000)。

ウィンドウ特定: 複数ChromeプロセスがあるとMainWindowHandleが競合する可能性。URLやタイトルでさらに絞り込む必要がある場合も。

結果

各モニタでChromeがフルスクリーン表示され、指定したURLがそれぞれ開く。


位置指定で開く

PowerShell
# Chromeをモニタ1(0,0)に起動
Start-Process "chrome.exe" -ArgumentList "--new-window https://example.com"
Start-Sleep -Milliseconds 500
Add-Type @"
    using System;
    using System.Runtime.InteropServices;
    public class Win32 {
        [DllImport("user32.dll")]
        public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
    }
"@
$hwnd = (Get-Process -Name "chrome" | Select-Object -First 1).MainWindowHandle
[Win32]::SetWindowPos($hwnd, 0, 0, 0, 800, 600, 0x0040)

# Chromeをモニタ2(1920,0)に起動
Start-Process "chrome.exe" -ArgumentList "--new-window https://example.org"
Start-Sleep -Milliseconds 500
$hwnd2 = (Get-Process -Name "chrome" | Where-Object { $_.MainWindowHandle -ne $hwnd } | Select-Object -First 1).MainWindowHandle
[Win32]::SetWindowPos($hwnd2, 0, 1920, 0, 800, 600, 0x0040)

説明: モニタ1(0,0)とモニタ2(1920,0)にChromeウィンドウを配置。座標は環境に応じて調整。

注意: モニタ解像度や配置(例: 1920x1080の2枚)を確認(Get-CimInstance Win32_VideoControllerで解像度取得可)。


備考

Windowsでは座標(0,0)はプライマリモニタの左上を基準にします。マルチモニタ環境では、以下のルールが適用されます

プライマリモニタ: Windowsのディスプレイ設定で「これをメインディスプレイにする」に設定されたモニタが(0,0)の原点。

他のモニタ: プライマリモニタに対する相対座標で配置。例: 右に2番目のモニタがある場合、(1920,0)がその左上(プライマリが1920x1080の場合)。

確認方法: 設定→システム→ディスプレイでモニタ配置を確認。プライマリモニタは「1」と表示。

モニタ1(プライマリ、1920x1080): (0,0)が左上。
モニタ2(右隣、1920x1080): (1920,0)が左上。

PowerShellコードで(0,0)はプライマリモニタの左上を指し、フルスクリーンならそのモニタ全体を占有。

コメント

  1. 結局今は・・・Chrome・Edge・Firefoxの位置記憶に頼ってるって落ちwww

    返信削除

コメントを投稿

人気の投稿

Bloggerでは“コピー用アイコン”が出せないの?

Googleドライブの共有で浮気がバレるかもしれない・・・www

ブックマークレット