ウィンドウ・キャプチャ

Capture-Window.ps1

# $OutputDir # 出力先のフォルダ・パス Param( [System.IO.DirectoryInfo] $OutputDir ) $src = ' using System; using System.Runtime.InteropServices; public static class W32 { [DllImport(@"User32.dll")] public extern static IntPtr GetForegroundWindow(); [DllImport(@"Dwmapi.dll")] public extern static int DwmGetWindowAttribute( IntPtr hWnd, uint nAttributes, ref RECT rcWindow, int nAttributesSize ); const uint DWMWA_EXTENDED_FRAME_BOUNDS = 9; [StructLayout(LayoutKind.Sequential)] public struct RECT { public int Left; // x position of upper-left corner public int Top; // y position of upper-left corner public int Right; // x position of lower-right corner public int Bottom; // y position of lower-right corner } public static RECT GetRect() { IntPtr hWnd = GetForegroundWindow(); RECT rcWindow = new RECT(); DwmGetWindowAttribute( hWnd, DWMWA_EXTENDED_FRAME_BOUNDS, ref rcWindow, Marshal.SizeOf(rcWindow) ); return rcWindow; } } ' Add-Type -AssemblyName System.Drawing trap { break } try { Add-Type $src Write-Host "型を追加しました" } catch { Write-Host "型は既に追加済みです" return } $images = Get-ChildItem $OutputDir.FullName -Filter:'*.png' $nIndex = 0 if( $images -ne $Null ) { $nIndex = $images | &{ Process{++ $nIndex} End{$nIndex} } } [System.Threading.Thread]::Sleep( 1000 ) $rc = [W32]::GetRect() $dt = (Get-Date).ToString( 'yyyy-MM-dd HH:mm:ss' ) $canvasSize = [System.Drawing.Size]::new( $rc.Right - $rc.Left, $rc.Bottom - $rc.Top ) $titleSize = [System.Drawing.Size]::new( 140, 16 ) $ft = [System.Drawing.Font]::new( 'MS ゴシック', 10 ) $bmp = [System.Drawing.Bitmap]::new( $rc.Right - $rc.Left, $rc.Bottom - $rc.Top ) [System.Drawing.Graphics] $g = [System.Drawing.Graphics]::FromImage( $bmp ) $g.CopyFromScreen( $rc.Left, $rc.Top, 0, 0, $canvasSize ) $xPos = $canvasSize.Width - $titleSize.Width $yPos = $canvasSize.Height - $titleSize.Height $g.FillRectangle( [System.Drawing.Brushes]::White, $xPos, $yPos, $titleSize.Width, $titleSize.Height ) $g.DrawString( $dt, $ft, [System.Drawing.Brushes]::Black, $xPos + 4, $yPos + 2 ) do { $nIndex ++ $path = '{0}\{1:D5}.png' -f $OutputDir.FullName, $nIndex if( [System.IO.File]::Exists($path) -eq $False ) { $bmp.Save( $path, [System.Drawing.Imaging.ImageFormat]::Png ) break } }while( $True ) $bmp.Dispose() $g.Dispose()

capture.js

// JScript (WSH) から .PS1 を実行する // JScript から実行することで PowerShell のウィンドウを含めないようにキャプチャする // // この .JS を実行してから 1 秒後にキャプチャする var sh = new ActiveXObject( "WScript.Shell" ); // 実行するコマンドレットのパス var sCmdLet = "Capture-Window.ps1"; // キャプチャ画像を保存するフォルダのパス var sOutputDir = "D:\\tmp\\o"; sh.Run( "PowerShell -ExecutionPolicy RemoteSigned -File \"" + sCmdLet + "\" \"" + sOutputDir + "\"", 0 );