如何在PowerShell中创建独立窗口的GUI进度条?
需求:将PowerShell控制台进度条改为独立GUI窗口进度条
我在PowerShell中用以下代码实现了控制台进度条,运行正常,但进度条显示在控制台里。现在想实现点击触发时弹出独立窗口显示GUI进度条,请提供示例代码。
原控制台进度条代码:
Write-Progress -Activity $activity -PercentComplete 0 $totalMinutes = 0.15 # 进度条持续显示时长(分钟) $timerIntervalSecs = 0.1 # 定时器触发间隔(秒) $global:continueWhile = $true # 控制进度条启停的全局变量 $timer = [System.Timers.Timer]::new($timerIntervalSecs * 1000) $eventJob = Register-ObjectEvent -InputObject $timer -EventName Elapsed -Action { $endTime, $totalMinutes, $activity = $Event.MessageData.EndTime, $Event.MessageData.TotalMinutes, $Event.MessageData.Activity $timeLeft = $endTime - (Get-Date) $percent = (1 - $timeLeft.TotalSeconds / ($totalMinutes * 60)) * 100 if ($timeLeft -lt 0) { $percent = 100; $timeLeft = [timespan] 0; $global:continueWhile = $false } # 时长结束后停止进度条 Write-Progress -Activity $activity -Status "$([math]::Floor($percent))% complete, $([math]::Ceiling($timeLeft.TotalSeconds)) seconds remaining..." -PercentComplete $percent } -MessageData (@{ EndTime = (Get-Date).AddMinutes($totalMinutes); TotalMinutes = $totalMinutes; Activity = $activity }) $timer.Start() # 启动定时器 try { Write-Output "test output" while ($continueWhile) { Start-Sleep -Seconds 0.5 } } finally { $timer.Stop() Remove-Job $eventJob -Force Write-Progress -Activity $activity -Completed }
独立GUI窗口进度条示例代码
以下代码使用Windows Forms创建独立窗口的进度条,支持点击触发启动,模拟进度推进:
# 加载Windows Forms程序集 Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Drawing # 创建主窗口 $form = New-Object System.Windows.Forms.Form $form.Text = "进度窗口" $form.Size = New-Object System.Drawing.Size(400, 150) $form.StartPosition = "CenterScreen" $form.FormBorderStyle = "FixedSingle" $form.MaximizeBox = $false # 创建进度条控件 $progressBar = New-Object System.Windows.Forms.ProgressBar $progressBar.Location = New-Object System.Drawing.Point(20, 30) $progressBar.Size = New-Object System.Drawing.Size(340, 25) $progressBar.Maximum = 100 $progressBar.Value = 0 # 创建状态标签 $statusLabel = New-Object System.Windows.Forms.Label $statusLabel.Location = New-Object System.Drawing.Point(20, 70) $statusLabel.Size = New-Object System.Drawing.Size(340, 20) $statusLabel.Text = "准备就绪" # 创建启动按钮 $startButton = New-Object System.Windows.Forms.Button $startButton.Location = New-Object System.Drawing.Point(150, 100) $startButton.Size = New-Object System.Drawing.Size(100, 30) $startButton.Text = "开始" # 定时器用于更新进度 $timer = New-Object System.Windows.Forms.Timer $timer.Interval = 100 # 100毫秒更新一次 $endTime = $null $totalMinutes = 0.15 # 进度持续时长(分钟) # 按钮点击事件:启动进度 $startButton.Add_Click({ $startButton.Enabled = $false $endTime = (Get-Date).AddMinutes($totalMinutes) $timer.Start() }) # 定时器触发事件:更新进度条 $timer.Add_Tick({ $timeLeft = $endTime - (Get-Date) if ($timeLeft.TotalSeconds -lt 0) { $progressBar.Value = 100 $statusLabel.Text = "完成!100% complete" $timer.Stop() $startButton.Enabled = $true return } $percent = [math]::Floor((1 - $timeLeft.TotalSeconds / ($totalMinutes * 60)) * 100) $progressBar.Value = $percent $statusLabel.Text = "$percent% complete, $([math]::Ceiling($timeLeft.TotalSeconds)) seconds remaining..." }) # 将控件添加到窗口 $form.Controls.Add($progressBar) $form.Controls.Add($statusLabel) $form.Controls.Add($startButton) # 显示窗口 $form.ShowDialog() | Out-Null # 清理资源 $timer.Dispose() $form.Dispose()
代码说明
- 基于
System.Windows.Forms构建独立GUI窗口,包含进度条、状态标签和启动按钮 - 点击「开始」按钮后启动定时器,定时计算剩余时长并更新进度条数值与状态文本
- 进度完成后自动停止定时器,恢复按钮可用状态
- 窗口居中显示、固定大小且无法最大化,保证进度条展示效果
内容的提问来源于stack exchange,提问作者Halcoaching
相关产品推荐
相关产品推荐

