1
2

More than 3 years have passed since last update.

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

Posted at

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

ポイント

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

スクリプト

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

ブラウザサイズを変更する.ps1
$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
}

動作確認環境

  • Windows 10
  • PowerShell 5.1

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

1
2
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
2