PowerShell ディスプレイの解像度を考慮したフォーム画面の作成例(System.Windows.Forms.SystemInformation の活用)

PowerShell
# フォーム作成
$Form = New-Object System.Windows.Forms.Form 

#SystemInformationからWorkingAreaのLocationとSizeを取得
$maxlocation = [System.Windows.Forms.SystemInformation]::WorkingArea.Location
$maxsize = [System.Windows.Forms.SystemInformation]::WorkingArea.Size

#SystemInformationからWorkingAreaのSizeの比率を変更する
$maxsize.Height =  $maxsize.Height * 0.25
$maxsize.Width =  $maxsize.Width * 0.25

$form.Location = $maxlocation 
$form.Size = $maxsize  

$FormResult = $Form.ShowDialog()


自分のパソコンは画面の解像度が高い(4K)のため、世の中にあるサンプルフォーム画面(サイズを定数にした例)

をそのまま参考にツールを作成しようとすると画面イメージがあわない(つぶれてしまう。)

そこで、Windowsの情報から画面サイズを取り出して計算したら、いい感じにならないか調べていたら、

System.Windows.Forms.SystemInformation が使えることがわかり早速作成。

$maxsize.Width と $maxsize.Height の比率を変更したら、自由なサイズに変更可能!

ライセンス:本記事のテキスト/コードは特記なき限り CC BY 4.0 です。引用の際は出典URL(本ページ)を明記してください。
利用ポリシー もご参照ください。

コメント

タイトルとURLをコピーしました