作成 2010.01.05
更新 2011.11.27
PowerShell で Windows フォームを表示するサンプル
フォームを表示するサンプルです。
Function Open-Dialog(){
  [void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")

  # メニューバー
  $mymi1 = New-Object Windows.Forms.MenuItem("File")
  $mymi1_1 = New-Object Windows.Forms.MenuItem("Open")
  $mymi1_2 = New-Object Windows.Forms.MenuItem("Save")
  $mymi1_3 = New-Object Windows.Forms.MenuItem("Close")
  $myms = New-Object Windows.Forms.MenuItem("-")
  $res = $mymi1.MenuItems.Add($mymi1_1)
  $res = $mymi1.MenuItems.Add($mymi1_2)
  $res = $mymi1.MenuItems.Add($myms)
  $res = $mymi1.MenuItems.Add($mymi1_3)
  $mymi2 = New-Object Windows.Forms.MenuItem("Data")
  $mymainmenu = New-Object Windows.Forms.MainMenu
  $res = $mymainmenu.MenuItems.Add($mymi1)
  $res = $mymainmenu.MenuItems.Add($mymi2)

  # ボタン
  $myb1 = New-Object Windows.Forms.Button
  $myb1.Text = "OK"
  $myb1.Location = New-Object Drawing.Point(10,50)
  $myb2 = New-Object Windows.Forms.Button
  $myb2.Text = "Cancel"
  $myb2.Location = New-Object Drawing.Point($myb1.left, ($myb1.top + $myb1.height + 10))

  # ツールバー
  $mytbb1 = New-Object Windows.Forms.ToolBarButton
  $mytbb1.Text = "Open"
  $mytbb2 = New-Object Windows.Forms.ToolBarButton
  $mytbb2.Text = "Save"
  $mytb = New-Object Windows.Forms.ToolBar
  $res = $mytb.Buttons.Add($mytbb1)
  $res = $mytb.Buttons.Add($mytbb2)

  $myform = New-Object Windows.Forms.Form
  $myform.Text = "Original PowerShell Form"
  $myform.FormBorderStyle = [Windows.Forms.FormBorderStyle]::Sizable
  $myform.AcceptButton = $myb1
  $myform.CancelButton = $myb2
  $myform.StartPosition = [Windows.Forms.FormStartPosition]::CenterScreen

  $myform.Menu = $mymainmenu
  $myform.Controls.Add($mytb)
  $myform.Controls.Add($myb1)
  $myform.Controls.Add($myb2)
  $res = $myform.ShowDialog()
}
参照

Button クラス | Form クラス

タグ: PowerShell

©2004-2017 UPKEN IPv4