【问题标题】:Powershell: Using Show() does not display formPowershell:使用 Show() 不显示表单
【发布时间】:2021-06-05 02:56:03
【问题描述】:

我在使用 PowerShell 构建启动画面时遇到问题。我想在脚本在后台运行另一组指令时显示它。我最初使用的是 ShowDialog(),但我已经阅读并见证了它会停止执行脚本并被告知改用 Show()。

问题在于它会在瞬间显示表单并立即消失。我还读过,为了让它工作,我必须使用多个线程;在主线程中显示表单,同时使用 Start-Job 在另一个线程上运行其余部分。但是行为是一样的。如何让它显示 X 持续时间,然后在 Start-Sleep 后关闭?

这是我的代码:

$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()

感谢任何帮助。谢谢。

【问题讨论】:

  • 刚刚有机会尝试,该代码对我来说很好。
  • 这是In PowerShell Form.Show() does not work right, but Form.ShowDialog() does的一种复制品。我在那里给出了解释和一种转向,但其他贡献者给出了一些很好的解决方案。
  • 是的 JPBlanc,我事先阅读了那个帖子,但它没有用,所以我认为我做错了什么。与此同时,我确实想出了另一种方法,即调用第二个 Powershell 进程并在主后台作业完成后停止它。

标签: multithreading forms powershell


【解决方案1】:

ShowDialog() 替换为 show 并重试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-14
    • 1970-01-01
    • 2020-12-26
    • 1970-01-01
    • 2014-08-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多