PowerShellの開発でお困りのことはありませんか?
そんな悩みは
当サイトで解決!!
17.指定した年月のカレンダーを表示する GUI版
UPDATE:2008/05/03
Windows Formにカレンダーを表示する関数を作成しました。
実行結果は下記の通りです。
View-Calendar.ps1
[void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms") #=============================================================================== # View-Calendar: 指定した年月のカレンダーを表示する(GUI版) # # パラメータ: # $Year: 年(省略した場合は現在の年) # $Month: 月(省略した場合は現在の月) # # 使用例1(年月指定) # PS > View-Calendar 2008 2 # # 使用例2(年月省略) # PS > View-Calendar # # copyright HIRO's.NET(http://hiros-dot.net/) #=============================================================================== function global:View-Calendar { Param ([int]$Year = $(Get-Date).Year, [int]$Month = $(Get-Date).Month) #===== Formの作成 ===== $form = New-Object System.Windows.Forms.Form $form.Text = "Calendar" $form.Width = 147 $form.Height = 210 $form.FormBorderStyle = [Windows.Forms.FormBorderStyle]::FixedToolWindow
#===== MonthCalendarの作成 ===== $cal = New-Object System.Windows.Forms.MonthCalendar $cal.Dock = [System.Windows.Forms.DockStyle]::Fill $cal.SelectionStart = New-Object DateTime($Year, $Month, 1) $cal.SelectionEnd = New-Object DateTime($Year, $Month, 1) $form.Controls.Add($cal) #===== Panelの作成 ===== $panel1 = New-Object System.Windows.Forms.Panel $form.Controls.Add($panel1) $panel1.Dock = [System.Windows.Forms.DockStyle]::Bottom $panel1.Height = 30
#===== [閉じる]ボタンの作成 ===== $btnClose = New-Object System.Windows.Forms.Button $btnClose.Location = New-Object System.Drawing.Point(61, 4) $btnClose.Text = "閉じる" $btnClose.anchor = [System.Windows.Forms.AnchorStyles]::Top -bor [System.Windows.Forms.AnchorStyles]::Right $btnClose.Add_Click({$form.Close()}) $panel1.Controls.Add($btnClose) [void]$form.ShowDialog() }
|
使用方法は
View-Calendar[Enter]
と入力すれば、当月のカレンダーを
View-Calendar 年 月[Enter]
と入力すれば、指定年月のカレンダーを表示します。
List1
PS > View-Calendar
PS > View-Calendar 2008 6
|
ポイント
見てのとおり、今回はMonthCalendarコントロールを使用しました。
MonthCalendarコントロールは、日にちをマウスで選択可能なので、フォームが閉じられる時に選択されている日を返すようにしてもよいかもしれませんね。
その場合は
[閉じる]ボタンが押される時の処理を
List2
$btnClose.Add_Click({$form.Close()})
|
から
List3
$btnClose.Add_Click({$form.DialogResult = "OK"; $form.Close()})
|
として、フォームのShowDialog()をList4のようにすればよいと思います。
List4
if ( $form.ShowDialog() -eq "OK" ) { retrurn $cal.SelectionStart.ToString() }
|