我在这里有一个有趣的问题.我创建一个日历选择器,供我们创建帐户时使用.它工作正常,并且仍在进行中,但是我注意到,当我在PowerShell ISE中运行脚本时,几分钟后它会锁定(我可以在之前几分钟编辑和保存代码).事件日志中没有任何内容.我得到一个对话框,说PowerShell是无响应的.内存使用情况似乎也正常.我不知道发生了什么.
原文链接:https://www.f2er.com/windows/371497.html无论我如何运行Powershell ISE(以管理员身份运行,运行另一个帐户和正常的ISE),我都在运行Windows 8.1.
一个同事建议它可能是公寓模型,所以我尝试过STA和MTA,但是问题出现在两种情况之中.当从控制台主机运行相同的代码时,不会发生这种情况.
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") $objForm = New-Object Windows.Forms.Form $objForm.Text = "Select a Date" $objForm.Size = New-Object Drawing.Size @(490,250) $objForm.StartPosition = "CenterScreen" $objForm.KeyPreview = $True $objForm.Add_KeyDown({ if ($_.KeyCode -eq "Enter") { $script:dtmDate=$objCalendar.SelectionStart $objForm.Close() } }) $objForm.Add_KeyDown({ if ($_.KeyCode -eq "Escape") { $objForm.Close() } }) $objCalendar = New-Object System.Windows.Forms.MonthCalendar $objCalendar.Text = "Start" $objCalendar.ShowTodayCircle = $False $objCalendar.MaxSelectionCount = 1 $objForm.Controls.Add($objCalendar) $objForm.Topmost = $True $objForm.Add_Shown({$objForm.Activate()}) [void] $objForm.ShowDialog() if ($dtmDate) { Write-Host "Date selected: $dtmDate" } $objForm.Dispose()
在回应@独特的保罗·史密斯
function Find-CalenderDateTest { [CmdletBinding()] param( [Parameter( Mandatory=$false )] [ValidateSet('long','short','powerpoint')] [ValidateNotNullOrEmpty()] [string] $DateFormat ) Begin{ [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") $objForm = New-Object Windows.Forms.Form $objForm.Text = "Select a Date" $objForm.Size = New-Object Drawing.Size @(243,250) $objForm.StartPosition = "CenterScreen" $objForm.KeyPreview = $True $dtmDate = $null $objForm.Add_KeyDown( { if ($_.KeyCode -eq "Enter") { $dtmDate=$objCalendar.SelectionStart $objForm.Close() } }) $objForm.Add_KeyDown({ if ($_.KeyCode -eq "Escape") { $objForm.Close() } }) #region OK Button $OKButton = New-Object System.Windows.Forms.Button $OKButton.Location = New-Object System.Drawing.Size(20,175) $OKButton.Size = New-Object System.Drawing.Size(75,23) $OKButton.Text = "OK" # Got rid of the Click event for OK Button,and instead just assigned its DialogResult property to OK. $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK $objForm.Controls.Add($OKButton) # Setting the form's AcceptButton property causes it to automatically intercept the Enter keystroke and # treat it as clicking the OK button (without having to write your own KeyDown events). $objForm.AcceptButton = $OKButton #endregion #region Cancel Button $CancelButton = New-Object System.Windows.Forms.Button $CancelButton.Location = New-Object System.Drawing.Size(80,175) $CancelButton.Size = New-Object System.Drawing.Size(75,23) $CancelButton.Text = "Cancel" # Got rid of the Click event for Cancel Button,and instead just assigned its DialogResult property to Cancel. $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel $objForm.Controls.Add($CancelButton) # Setting the form's CancelButton property causes it to automatically intercept the Escape keystroke and # treat it as clicking the OK button (without having to write your own KeyDown events). $objForm.CancelButton = $CancelButton #endregion $objCalendar = New-Object System.Windows.Forms.MonthCalendar $objCalendar.ShowTodayCircle = $False $objCalendar.MaxSelectionCount = 1 $objForm.Controls.Add($objCalendar) $objForm.Topmost = $True $objForm.Add_Shown({$objForm.Activate()}) $Results = $objForm.ShowDialog() } Process{} End{ if ($Results -eq "OK") { $objCalendar.SelectionStart } $objForm.Dispose() } }