Winforms 将动画.gif导入Powershell中的Windows.Forms

Winforms 将动画.gif导入Powershell中的Windows.Forms,winforms,powershell,animation,gif,showdialog,Winforms,Powershell,Animation,Gif,Showdialog,我是Powershell的新手,遇到了这个问题。我有一个很长的自动安装脚本,它运行在MySQL安装/帐户设置和其他.msi中 当用户等待脚本完成时,我在picturebox中显示了“Please wait”提示,其中包含一个旋转的pinwheel.gif 问题是,如果我将form.visible设置为$true并在脚本中稍后将其关闭,.gif本身将不会移动。它会显示但会丢失其动画。如果我将form.visible更改为“false”,并通过添加form.showdialog()参数将form更改

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

当用户等待脚本完成时,我在picturebox中显示了“Please wait”提示,其中包含一个旋转的pinwheel.gif

问题是,如果我将form.visible设置为$true并在脚本中稍后将其关闭,.gif本身将不会移动。它会显示但会丢失其动画。如果我将form.visible更改为“false”,并通过添加form.showdialog()参数将form更改为modal,则动画在.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.

为什么不使用WinForms中实现的
progressBar
控件

使用“字幕”动画速度,progressBar的行为将类似于“请稍候”,不会停止,并且更容易添加到脚本而不是gif


在Winforms中使用progressBar的示例:

我发现了一个线程,该线程对此有一个解决方法,可以创建一个新的运行空间并在其中打开表单。这样,.gif就可以正常运行,并且只要您想在代码中进一步深入,就可以发出form.close()命令。如果有人想将.gif用于除“请等待”指示器之外的其他内容,这将非常方便。从本网站发布:


嗯,令人惊讶的是,当我研究指标时,控制从未出现过。这很好。如果我不能用.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=$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()