Jus*_*ing 14 powershell modal-dialog winforms
我试图通过powershell显示图像.我根据这篇论坛帖子制作了一个剧本.
如果我使用ShowDialog()它工作正常,除了PowerShell执行在对话框启动时停止.但是,这是设计用于模态对话框.如果我在PowershellISE中调用Form.Show(),表单会显示,但会冻结,无法移动或解除.如果我将代码复制并传递给PowerShell控制台,则行为类似.
如何使对话非模态,而不是冻结.
JPB*_*anc 19
第一个答案为什么要追加.
在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"
Run Code Online (Sandbox Code Playgroud)