2017-06-02 54 views
0

我是Powershell的新手,并且遇到了这个问题。我有一个非常长的自动化安装脚本,通过MySQL安装/帐户设置和其他.msi的运行。在Powershell中导入一个动画的.gif到Windows.Forms

当用户等待脚本完成时,我有这个“Please Wait”提示画面,其中包含图片框中的旋转风轮.gif。

问题是,如果我将form.visible设置为$ true并且稍后在脚本中关闭它,那么.gif本身不会移动。它显示但失去了它的动画。如果我将form.visible更改为“false”,并通过添加form.showdialog()参数将窗体更改为模式,则动画在.gif中是完美的,但脚本会暂停并且只能在关闭窗口后才能进行。有没有一种方法可以让脚本在不丢失.gif中的动画的情况下进展?请帮忙。由于某种原因,我发现Powershell和.gif的论坛很少。这是表单的代码。

$Form = New-Object system.Windows.Forms.Form 
$Form.Location= New-Object System.Drawing.Size(100,100) 
$Form.Size= New-Object System.Drawing.Size(550,170) 
$Form.StartPosition = "Manual" 
$Form.Visible=$true 
$Form.Enabled = $true 


[reflection.assembly]::LoadWithPartialName("System.Windows.Forms") 
$file = (get-item 'PathToMyGifFile') 
$img = [System.Drawing.Image]::Fromfile($file); 

[System.Windows.Forms.Application]::EnableVisualStyles(); 

$pictureBox = new-object Windows.Forms.PictureBox 
$pictureBox.Location = New-Object System.Drawing.Size(0,1) 
$pictureBox.Size = New-Object System.Drawing.Size(100,80) 
$pictureBox.Image = $img 
$Form.controls.add($pictureBox) 

$Label1 = New-Object System.Windows.Forms.Label 
$Label1.Text = "Please wait for Installation to complete" 
$Label1.Location= New-Object System.Drawing.Size(110,35) 
$Label1.Size = New-Object System.Drawing.Size(400,25) 
$Label1Font = New-Object System.Drawing.Font("Tahoma",10, 
[System.Drawing.FontStyle]::Bold) 

$Label1.Font = $Label1Font 
$Label1.BackColor = "white" 
$Form.Controls.Add($Label1) 

$Label2 = New-Object System.Windows.Forms.Label 
$Label2.Text = "This may take several minutes . . ." 
$Label2.Location= New-Object System.Drawing.Size(120,60) 
$Label2.Size = New-Object System.Drawing.Size(370,75) 
$Label2Font = New-Object System.Drawing.Font("Tahoma",10, 
[System.Drawing.FontStyle]::Regular) 

$Label2.Font = $Label2Font 
$Label2.BackColor = "white" 
$Form.Controls.Add($Label2) 

$BackDrop = New-Object System.Windows.Forms.Label 
$BackDrop.Location = New-Object System.Drawing.Size(0,0) 
$BackDrop.Size = New-Object System.Drawing.Size(550,150) 
$BackDrop.BorderStyle = [System.Windows.Forms.BorderStyle]::FixedSingle; 
$BackDrop.BackColor = [System.Drawing.Color]::White 

$Form.Controls.Add($WaitBackGroundBox) 
$Form.Topmost = $True 
[void] $Form.ShowDialog() # If absent, animation is lost. If present 
          # (combined with form.visible = $false), script 
          # halts until the form itself is closed. 

回答

0

我发现这个解决方法是创建一个新的运行空间,并在其中打开形式的线程。这样,.gif正常运行,并且只要您希望进一步下载代码,就可以发出form.close()命令。这会派上用场,以防万一有人想要使用.gif作为Please Wait指示符以外的其他用途。发布从这个网站:

https://www.vistax64.com/powershell/16998-howto-create-windows-form-without-stopping-script-processing.html

$Form = New-Object system.Windows.Forms.Form 
$Form.Location= New-Object System.Drawing.Size(100,100) 
$Form.Size= New-Object System.Drawing.Size(550,170) 
$Form.StartPosition = "Manual" 
$Form.Visible=$false 
$Form.Enabled = $true 
$Form.Add_Shown({$Form.Activate()}) 

[reflection.assembly]::LoadWithPartialName("System.Windows.Forms") 
$file = (get-item 'C:\path_to_your_gif.gif') 
$img = [System.Drawing.Image]::Fromfile($file); 

[System.Windows.Forms.Application]::EnableVisualStyles(); 

$pictureBox = new-object Windows.Forms.PictureBox 
$pictureBox.Location = New-Object System.Drawing.Size(0,1) 
$pictureBox.Size = New-Object System.Drawing.Size(100,80) 
$pictureBox.Image = $img 
$Form.controls.add($pictureBox) 

$Label = New-Object System.Windows.Forms.Label 
$Label.Text = "Please wait for Installation to complete" 
$Label.Location= New-Object System.Drawing.Size(110,35) 
$Label.Size = New-Object System.Drawing.Size(400,25) 
$LabelFont = New-Object System.Drawing.Font("Tahoma",10, 
[System.Drawing.FontStyle]::Bold) 
$Label.Font = $PleaseWaitLabelFont 
$Label.BackColor = "white" 
$Form.Controls.Add($WaitLabel) 

$WaitForm.Topmost = $True 

$rs = [Management.Automation.Runspaces.RunspaceFactory]::CreateRunspace() 
$rs.Open() 
$rs.SessionStateProxy.SetVariable("Form", $Form) 
$data = [hashtable]::Synchronized(@{text=""}) 
$rs.SessionStateProxy.SetVariable("data", $data) 
$p = $rs.CreatePipeline({ [void] $Form.ShowDialog()}) 
$p.Input.Close() 
$p.InvokeAsync() 

## Enter the rest of your script here while you want the form to display 

$WaitForm.close() 
$rs.close() 
0

为什么不使用的WinForms实现progressBar控制?

使用“选取框”动画速度,progressBar将表现得像“Please Wait”,不会停止,并且更容易添加到您的脚本而不是gif。在使用的WinForms进度的

例子:https://www.sapien.com/blog/2011/07/14/primalforms-2011-spotlight-on-the-progressbar-control/

+0

嗯,令人惊讶的控制没有来了,当我正在研究的指标。这很漂亮。如果我无法使用.gif修复表单,那么这不是一个糟糕的解决方法。谢谢! –