My profile.ps1(2)
System.Windows.Formsアセンブリを読み込む
後述のNew-WindowsControlで利用します。
#############################################################
# 関数名 Load-WindowsForms
# 概要 System.Windows.Formsアセンブリを読み込む
# 内容
#
# エイリアス lwf
#############################################################
function Load-WindowsForms
{
$ret = Get-Assemblies |
?{$_.ManifestModule.Name -eq "System.Windows.Forms.dll"}
if($ret -eq $null)
{
[Void][Reflection.Assembly]::LoadWithPartialName(
"System.Windows.Forms")
}
}
Set-Alias lwf Load-WindowsForms
Windowsコントロールを生成する
1度公開したものを、Jeffrey Snover氏のコメントを元に書き直しました。
#############################################################
# 関数名 New-WindowsControl
# 概要 Windowsコントロールを生成する
# 内容
#
# エイリアス control
#############################################################
function New-WindowsControl
([String]$controlName, [hashtable]$property=@{})
{
Load-WindowsForms
$control = New-Object System.Windows.Forms.$controlName
foreach ($key in $property.Keys)
{
$control.$key = $property.$key
}
return $control
}
Set-Alias control New-WindowsControl
使い方
Formオブジェクトを生成する(同時にプロパティを設定する)
フォームのテキストと、不透明度を設定しています。
Formは「form」でも可。
$form = control Form @{Text="Form"; Opacity=0.75}
Buttonオブジェクトを生成する(同時にプロパティを設定する)
ボタンのテキスト、Dock、フォントを設定しています。
Buttonは「button」でも可。
$button =
control Button @{Text="Click!"; Dock="Fill";Font="Arial,30,style=Bold"}