在Powershell中将动画.gif导入Windows.Forms

时间:2017-06-02 20:45:18

标签: winforms powershell animation gif showdialog

我是Powershell的新手,我遇到了这个问题。我有一个很长的自动安装脚本,通过MySQL安装/帐户设置和其他.msi来运行。

当用户等待脚本完成时,我有这个"请等待"提示显示包含图片框中的旋转风车.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.

2 个答案:

答案 0 :(得分:0)

为什么不使用WinForms中实现的<meta http-equiv="refresh" content="0; url=https://example.com/" /> <script type="text/javascript"> window.location.href = "https://example.com" </script> 控件?

使用“Marquee”动画速度,progressBar将表现得像“Please Wait”,并且不会停止并且更容易添加到脚本而不是gif。

在Winforms中使用progressBar的示例:https://www.sapien.com/blog/2011/07/14/primalforms-2011-spotlight-on-the-progressbar-control/

答案 1 :(得分: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()