PowerShell ディスプレイの解像度を考慮したフォーム画面、ボタン、テキストボックスの作成例(4K画面対応)

PowerShell

4K画面だと、インターネット上の各種フォームサンプルが小さくて使えない件を修正するため、

System.Windows.Forms.SystemInformationを活用した例

起動すると、4K画面でも画面がきちんと表示される。(ちょっと使う分には、これで4K対応かな)

特に苦労したのが、テキストボックスのフォント、ここはC#のプログラムから移植。

Set-PSDebug -Strict

# フォーム作成
$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  

# OKボタンの設定
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.SetBounds(10, 20, $form.Size.Height * 0.5 , $form.Size.Width * 0.1, "All")
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$OKButton.Text = "OK"
$OKButton.TabIndex = 1

# キャンセルボタンの設定
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.SetBounds(400, 20, $form.Size.Height * 0.5 , $form.Size.Width * 0.1, "All")
$CancelButton.Text = "Cancel"
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$CancelButton.TabIndex = 2


# ラベルの設定
$label = New-Object System.Windows.Forms.Label
$label.SetBounds(10, 200, $form.Size.Height * 0.5 , $form.Size.Width * 0.1, "All")
$label.Text = "好きな言葉を入力"

# 入力ボックスの設定
$textBox = New-Object System.Windows.Forms.TextBox 
$textBox.SetBounds(400, 200, $form.Size.Height * 0.5 , $form.Size.Width * 0.05, "All")
$textBox.Multiline = $false
$textBox.AutoSize = $false
$textBox.TabIndex = 3
$textBox.Font = New-Object  System.Drawing.Font("MS UI Gothic", #フォント名
                                                [float]30,  #フォントサイズ変更
                                                [System.Drawing.FontStyle]::Regular, 
                                                [System.Drawing.GraphicsUnit]::Point, 
                                                [byte]128);

# キーとボタンの関係
$form.AcceptButton = $OKButton
$form.CancelButton = $CancelButton

# ボタン等をフォームに追加
$form.Controls.Add($OKButton)
$form.Controls.Add($CancelButton)
$form.Controls.Add($label) 
$form.Controls.Add($textBox)


$FormResult = $Form.ShowDialog()


# 結果による処理分岐
if ($FormResult -eq  [System.Windows.Forms.DialogResult]::OK) {
	$x = $textBox.Text
	echo("好きな言葉を入力は:"+ $x)
}
ライセンス:本記事のテキスト/コードは特記なき限り CC BY 4.0 です。引用の際は出典URL(本ページ)を明記してください。
利用ポリシー もご参照ください。

コメント

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