爲什麼它附加。
在Windows圖形程序中,創建窗口的線程必須在消息泵中循環,以便將來自用戶操作的消息重新分配(轉換)到Windows中的事件。
在模態窗口中,處理窗口顯示的模態代碼運行自己的消息泵循環,直到窗口關閉才返回。這就是爲什麼ShowDialog()
之後的代碼在窗口關閉之前不會執行。
Show()
,只是要求顯示窗口,但如果沒有泵循環來管理來自用戶操作的消息,它就會凍結。
二的簡單方法有兩個線程
該cmdlet啓動工作中使用另一個線程從Powershell的分配池,以便使該對話框非模態,而且不結冰。
function goForm
{
[void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
$file = (get-item 'C:\temp\jpb.png')
#$file = (get-item "c:\image.jpg")
$img = [System.Drawing.Image]::Fromfile($file);
# This tip from http://stackoverflow.com/questions/3358372/windows-forms-look-different-in-powershell-and-powershell-ise-why/3359274#3359274
[System.Windows.Forms.Application]::EnableVisualStyles();
$form = new-object Windows.Forms.Form
$form.Text = "Image Viewer"
$form.Width = $img.Size.Width;
$form.Height = $img.Size.Height;
$pictureBox = new-object Windows.Forms.PictureBox
$pictureBox.Width = $img.Size.Width;
$pictureBox.Height = $img.Size.Height;
$pictureBox.Image = $img;
$form.controls.add($pictureBox)
$form.Add_Shown({ $form.Activate() })
$form.ShowDialog()
}
Clear-Host
start-job $function:goForm
$name = Read-Host "What is you name"
Write-Host "your name is $name"
展()可能不是一個好主意:http:// stackoverflow。com/questions/2192558/why-do-a-form-displayed-by-powershell-sometimes-not-show-up – keyboardP 2011-05-13 02:46:21
這是一個很好的參考。 – 2011-05-13 10:58:40