kurukuru-papaのブログ

主に、ソフトウェア開発に関連したメモを書き溜めたいと思います。

見た目もスッキリ! ウインドウの配置・サイズを変更する方法

ブラウザを操作していてキャプチャをとるとき、どうせならウインドウサイズを揃えて、きれいにキャプチャしたくなります。 マウス操作で、いい感じに変更してもよいのですが、少し手間です。

そんなときのため、PowerShellでサクッと解決する方法を調べてみました。

ポイント

  1. ウインドウサイズの変更は、user32.dll の bool MoveWindow() でできる。ただし、呼び出すためにお作法が必要。
  2. ウインドウの一覧は、Get-Process -Name $name | where { $_.MainWindowTitle -ne "" } で取得できる。ただし、ブラウザで複数ウインドウを表示している場合、アクティブなウインドウのみ取得可能な模様。

スクリプト

Edge, IE, Chrome, Firefox を対象に、1024x768サイズに変更するスクリプトです。 対象ブラウザやサイズ、位置は、お好みで変更してみてください。

$name = "msedge","iexplore","chrome","firefox"
$w = 1024
$h = 768
$x = 0
$y = 0
Add-Type @"
  using System;
  using System.Runtime.InteropServices;
  public class Win32Api {
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
  }
"@
Get-Process -Name $name | where { $_.MainWindowTitle -ne "" } | foreach {
  [Win32Api]::MoveWindow($_.MainWindowHandle, $x, $y, $w, $h, $true) | Out-Null
}

動作確認環境

参考にさせていただいたサイト