powershell 高DPI(Hi-DPI)対応GUIサンプルとGUI作成時の勘所について

PowerShell

 PowerShellのフォームサンプルに高DPI(Hi-DPI)対応のものがないため、Microsoftのサンプルソースをベースに改造

 あまり需要がないからなのか、PowerShellでGUIを作ることにハードルが高いのか、海外のサイトも含め知見を探したが、Powershell用に作成された簡単サンプルコードが見つからなず、仕方なくC#のコードを見ながら対応、なかなか大変だった。。

高DPI版 ユーザー設定の入力ボックス の サンプル

修正の勘所1 フォーム全体のサイズ調整

 AutoScaleDimensions と AutoScaleMode を設定すること

#For High DPI, We Set AutoScaleDimensions and AutoScaleMode
#The reason SizeF is set to 96 is that the standard Windows resolution for the display is 96.
#Maybe you shuld try that All object resets Drawing.Point And Drawing.Size
#In some cases, review the Drawing.Point and Drawing.Size of all objects, depending on the resolution of your display.
$form.AutoScaleDimensions =  New-Object System.Drawing.SizeF(96, 96)
$form.AutoScaleMode  = [System.Windows.Forms.AutoScaleMode]::Dpi

修正の勘所2 部品によってはフォントによるサイズ調整 

AutoScaleDimensions と AutoScaleMode の設定が有効にならない部品(ここではテキストボックス)については、フォントの文字サイズを大きくすることで部品のサイズを調整する。

#For High DPI, We Set Font Size
#In Japanese, "Yu Gothic UI" is the best font type. However, this font is for Japan only. Therefore, the default font definition is undefined.
#$Font = New-Object System.Drawing.Font("Yu Gothic UI",45,([System.Drawing.FontStyle]::Regular),[System.Drawing.GraphicsUnit]::Pixel)
$Font = New-Object System.Drawing.Font("",45,([System.Drawing.FontStyle]::Regular),[System.Drawing.GraphicsUnit]::Pixel)
$textBox.Font =  $Font 

修正の勘所3 全体の表示位置調整

最後に、各部品の配置位置(System.Drawing.Point)、サイズ(System.Drawing.Size)を微調整して完成。

Github

ユーザー設定の入力ボックス 以外も含め、 高DPI(Hi-DPI)対応GUIのサンプルも作成し、Githubで管理中

GitHub - papanda925/PowerShell_HiDPI_GUI_Sample: PowerShell High DPI Type Display GUI Sample
PowerShell High DPI Type Display GUI Sample. Contribute to papanda925/PowerShell_HiDPI_GUI_Sample development by creatin...

参考URL

c# — DPI対応アプリケーションの作成 (web-dev-qa-db-ja.com)

ユーザー設定の入力ボックスを作成する – PowerShell | Microsoft Docs 等

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

コメント

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