Windows PowerShell Part4

このエントリーをはてなブックマークに追加
206名無し~3.EXE
とりあえずマウス位置のウィンドウをキャプチャするのを作ってみたんですが
大きく表示している Chrome とか デスクトップ とかしか拾ってくれません。
なんか 矩形の2番目にくる Top がいつも0になるんでポインタや構造体サイズ当たりが間違ってるのかなと思うんですけど
どこが間違っているんでしょうか。
32ビットウィンドウだけが見えると言うわけでもなさそうで。

$Win32APIs = Add-Type -Language CSharp -Namespace Win32APIs -Name Window -MemberDefinition @"

    public struct Rect{ public int left, top, right, bottom; }

    [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]
    public static extern IntPtr WindowFromPoint(int x, int y);

    [ DllImport( "user32.dll" ) ]
    public static extern int GetWindowRect( IntPtr hwnd, out Rect lpRect );
"@

function GetWindowRectFromPoint ($Point) {
  $handle = [Win32APIs.Window]::WindowFromPoint([int]$Point.X, [int]$Point.Y)
  $rect = New-Object Win32APIs.Window+Rect
  $ret = [Win32APIs.Window]::GetWindowRect($handle, [ref]$rect)
  if ($ret) {
    return $rect
  }
}

$Point  = [System.Windows.Forms.Cursor]::Position
$Rect   = GetWindowRectFromPoint($Point)
$Bitmap  = New-Object System.Drawing.Bitmap -ArgumentList $( $Rect.right - $Rect.left ), $( $Rect.bottom - $Rect.top )
$graphics = [Drawing.Graphics]::FromImage($bitmap)
$graphics.CopyFromScreen( $Rect.left, $Rect.top, 0, 0, $bitmap.Size)
[System.Windows.Forms.Clipboard]::SetDataObject($bitmap)