使用PowerShell构建启动画面时遇到问题。我想在脚本在后台运行另一组指令时显示它。我最初使用ShowDialog(),但我已经阅读并目睹了它停止执行脚本并被告知使用Show()代替。
问题在于它显示了一瞬间的形式并立即消失。我还读到,为了使其工作,我必须使用多个线程;在主线程中显示表单,同时使用Start-Job在另一个线程上运行其余部分。但行为是一样的。我如何制作它以便显示X持续时间,然后在开始睡眠后关闭?
这是我的代码:
$Image1Path = "C:\Logo.png"
$Image2Path = "C:\Sync.gif"
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Windows.Forms.Application]::EnableVisualStyles()
#Create main form
$MainForm = New-Object System.Windows.Forms.Form
$MainForm.Controls.Add($Text1)
$MainForm.ClientSize = '830, 250'
$MainForm.ControlBox = $False
$MainForm.MaximizeBox = $False
$MainForm.MinimizeBox = $False
$MainForm.Name = "MainForm"
$MainForm.RightToLeftLayout = $True
$MainForm.StartPosition = 'CenterScreen'
$MainForm.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedDialog
#Create text label inside main form
$Label1 = New-Object System.Windows.Forms.Label
$Label1.Font = "Microsoft Sans Serif, 10.2pt, style=Bold"
$Label1.Location = '60, 110'
$Label1.Name = "Label1"
$Label1.Size = '710, 34'
$Label1.TabIndex = 0
$Label1.Text = "Some text here."
$Label1.TextAlign = 'MiddleCenter'
$MainForm.Controls.Add($Label1)
#Create picture box for Image1
$Image1 = [System.Drawing.Image]::FromFile($Image1Path)
$PictureBox1 = New-Object Windows.Forms.PictureBox
$PictureBox1.Width = $Image1.Size.Width
$PictureBox1.Height = $Image1.Size.Height
$PictureBox1.Image = $Image1
$PictureBox1.Location = '300, 20'
$MainForm.Controls.Add($PictureBox1)
#Create picture box for Image2
$Image2 = [System.Drawing.Image]::FromFile($Image2Path)
$PictureBox2 = New-Object Windows.Forms.PictureBox
$PictureBox2.Width = $Image2.Size.Width
$PictureBox2.Height = $Image2.Size.Height
$PictureBox2.Image = $Image2
$PictureBox2.Location = '390, 170'
$MainForm.Controls.Add($PictureBox2)
$MainForm.Add_Shown({$MainForm.Activate()})
$MainForm.Show()
Start-Sleep -Seconds 5
$MainForm.Close()
感谢任何帮助。谢谢。
答案 0 :(得分:0)
替换 ShowDialog() 而不是 show 并重试。