33
37

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 5 years have passed since last update.

PowerShell でウインドウの位置と大きさを指定

Last updated at Posted at 2015-11-19

アプリケーションのウインドウの位置と大きさをコマンドで指定してみたかったので、やってみた。

今回は下記のCodeを参考に実装。コードはCodeの節参照。

使ってみる

位置と大きさの取得

下記の通り、Get-WindowRectで左上の位置と大きさを取得できる。

PS> Get-WindowRect * | ft

Name            X      Y Width Height
----            -      - ----- ------
EXCEL         650     92   702    525
powershell     38    131   589    615

001.png

位置と大きさの指定

下記の通り、Move-WindowRectで左上の位置と大きさを指定できる。

PS> Move-WindowRect powershell 650 92 702 525
PS> Move-WindowRect Excel 38 131 589 615
PS> Get-WindowRect * | ft
Name            X      Y Width Height
----            -      - ----- ------
EXCEL          38    131   589    615
powershell    650     92   702    525

002.png

位置と大きさの保存、復元

Export-WindowRectで、各ウィンドウの現在の位置と大きさをcsvファイルに吐き出し、それをImport-WindowRectで読みこめば各ウィンドウの位置と大きさを復元できる。

PS> Export-WindowRect .\wr.csv
PS> cat .\wr.csv
"Name,"X","Y","Width","Height"
"EXCEL,"38","131","589","615"
"powershell,"650","92","702","525"
PS> 
PS> # 適当にウィンドウを動かして、大きさを変える
PS> 
PS> Import-WindowRect .\wr.csv

Code

filter Import-WindowRect ($Path)
{Import-Csv $Path | %{Move-WindowRect $_.Name $_.X $_.Y $_.Width $_.Height}}

filter Export-WindowRect ($Path)
{Get-Process -Name * | %{Get-WindowRect $_.Name} | Export-Csv $Path -Encoding UTF8 -NoTypeInformation}

filter Move-WindowRect
{
  Param
  (
    [String]$Name,
    [Int]$X = 0,
    [Int]$Y = 0,
    [Int]$Width = 400,
    [Int]$Height = 300
  )

  Get-Process -Name $Name | ?{$_.MainWindowTitle -ne ""} | %{
    [Win32]::MoveWindow($_.MainWindowHandle, $X, $Y, $Width, $Height, $true)
  } | Out-Null
}

filter Get-WindowRect ([String]$Name)
{
  Get-Process -Name $Name | ?{$_.MainWindowTitle -ne ""} | %{
    $rc = New-Object RECT
    [Win32]::GetWindowRect($_.MainWindowHandle, [ref]$rc) | Out-Null
    ConvertTo-Rect2 $_.Name $rc
  }
}


# Helper
# ------

filter ConvertTo-Rect2 ($name, $rc)
{
  $rc2 = New-Object RECT2

  $rc2.Name = $name
  $rc2.X = $rc.Left
  $rc2.Y = $rc.Top
  $rc2.Width = $rc.Right - $rc.Left
  $rc2.Height = $rc.Bottom - $rc.Top

  $rc2
}

# C#
# --

Add-Type @"
  using System;
  using System.Runtime.InteropServices;

  public class Win32 {
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
  }

  public struct RECT
  {
    public int Left;
    public int Top;
    public int Right;
    public int Bottom;
  }

  public struct RECT2
  {
    public string Name;
    public int X;
    public int Y;
    public int Width;
    public int Height;
  }
"@
33
37
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
33
37

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?