この記事は2008/02/22にわんくまブログで書いたものです。
前回はキャラクタベースのカレンダー表示を紹介しました。
今回は、GUI版を作成したので紹介します。
実行イメージは下記の通りです。
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() }
ポイント
見てのとおり、今回はMonthCalendarコントロールを使用しました。
MonthCalendarコントロールは、日にちをマウスで選択可能なので、フォームが閉じられる時に選択されている日を返すようにしてもよいかもしれませんね。
その場合は
[閉じる]ボタンが押される時の処理を
$btnClose.Add_Click({$form.Close()})
から
$btnClose.Add_Click({$form.DialogResult = "OK"; $form.Close()})
として、フォームのShowDialog()を
if ( $form.ShowDialog() -eq "OK" ) { retrurn $cal.SelectionStart.ToString() }
とすればよいと思います。
Theme design by Jelle Druyts
Pick a theme: BlogXP business calmBlue Candid Blue dasBlog dasblogger DirectionalRedux Discreet Blog Blue Elegante essence Just Html MadsSimple Mobile Mono Movable Radio Blue Movable Radio Heat nautica022 orangeCream Portal Project84 Project84Grass Slate Sound Waves Tricoleur useit.com Voidclass2 BlogXP business calmBlue Candid Blue dasBlog dasblogger DirectionalRedux Discreet Blog Blue Elegante essence Just Html MadsSimple Mobile Mono Movable Radio Blue Movable Radio Heat nautica022 orangeCream Portal Project84 Project84Grass Slate Sound Waves Tricoleur useit.com Voidclass2
Powered by: newtelligence dasBlog 2.0.7226.0
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2009, HIRO
E-mail