【问题标题】:PowerShell - Add text to ProgressBar (GUI)PowerShell - 将文本添加到 ProgressBar (GUI)
【发布时间】:2018-07-03 20:28:43
【问题描述】:

我正在尝试在进度条上添加文本(百分比等),但它不起作用。进度条文本基于this。以下是代码的简化版本。

#Form
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$Form = New-Object System.Windows.Forms.Form
$Form.Size = New-Object System.Drawing.Size (604,430)
$Form.Text = "Move User Files"
$Form.StartPosition = "CenterScreen"
$Form.MinimizeBox = $False
$Form.MaximizeBox = $False
$Form.WindowState = "Normal"
$Form.SizeGripStyle = "Hide"

#progres bar
$progressBar = New-Object System.Windows.Forms.ProgressBar
$progressBar.Name = 'ProgressBar'
$progressBar.Value = 0
$progressBar.Style = "Continuous"
$progressBar.Location = New-Object System.Drawing.Size (4,357)
$progressBar.Size = New-Object System.Drawing.Size (580,30)
$Form.Controls.Add($progressBar)

#This is the part that is not working
$gr = $progressBar.CreateGraphics()
$progressBarText = '0%'
$Font = new-object System.Drawing.Font("Bauhaus 93", 30, "Bold", "Pixel")
$Brush = New-Object Drawing.SolidBrush([System.Drawing.Color]::Black)
$PointF = [System.Drawing.PointF]::new($progressBar.Width /2 - ($gr.MeasureString($progressBarText,$Font).Width / 2),
    $progressBar.Height /2 - ($gr.MeasureString($progressBarText,$Font).Height / 2))
$gr.DrawString($progressBarText, $Font, $Brush, $PointF)

#Show The Form
$Form.Add_Shown({ $Form.Activate() })
[void]$Form.ShowDialog()

我没有收到任何错误,但它根本不显示文本。我错过了什么?有什么想法吗?

【问题讨论】:

    标签: powershell user-interface progress-bar


    【解决方案1】:

    您不应该在进度条上设置text 属性

    $progressBarText 应该是$progressBar.Text

    【讨论】:

    • 感谢您的帮助。不幸的是,我已经尝试过了,但没有奏效。事实上,System.Windows.Forms 的Documentation 告诉我们“此 API 支持产品基础架构,不打算直接从您的代码中使用。”。这就是为什么我试图将我在帖子中提到的来自 CodeProject.com 的示例翻译成 PowerShell。
    • 您尝试过 Sapien PowerShell Studio 吗?在 PowerShell 中制作 GUI 的最简单方法。他们有一个 Progressbar 以及一个 ProgressbarOverlay(我认为这就是他们所说的)。关于如何在他们的社区网站上完成这项工作的文档非常好。
    【解决方案2】:

    PowerShell 中的那个对你不起作用有什么原因吗?这是我每天多次使用的真实脚本的 sn-p。您也许可以根据自己的需要对其进行调整。我意识到它不是 GUI,而是 100% PowerShell。

           try {
               "Your Secret" | clip
                1..$Delay | % {
                    if (-not ( [console]::KeyAvailable) ) {
                        write-host "$($_)`r" -NoNewline
                        Write-Progress -Status "Press Any Key to continue" `
                            -Activity "Paste password before it is removed from the clipboard" `
                            -PercentComplete ($_ * 100 / $Delay)
                        Start-Sleep -Seconds 1
                    }
                }
            } finally {
                $null | clip
    
                if ([console]::KeyAvailable) {
                    $x = [console]::ReadKey()
                    Write-Information -MessageData $x -Tags "KeyStroke"
                }
            }
    

    (如何真正将安全密码输入剪贴板是留给读者的单独任务。)

    【讨论】:

    • 您可以使用-Status 参数来包含一些自定义文本,例如完成百分比。
    • 感谢您的回答,但我认为这与我的问题无关。我做了很多powershell脚本,它们工作得很好。我唯一的问题是将这个 C# 代码翻译成 PowerShell。
    • 这是一个powershell进度条。您的问题是如何使 GUI 进度条在 powershell 中工作。我知道这不是我的回复中所述的 GUI。但其他人可能会觉得这个答案很有用。
    【解决方案3】:

    我是怎么做到的,而且效果很好。

    我把它放在我的脚本的开头是这样的:

    $Font = New-Object System.Drawing.Font('Arial', 10, 'Bold', 'Pixel')
    $brush1 = New-Object Drawing.SolidBrush([System.Drawing.Color]::Black)
    $PBCG = $ProgressBar1.CreateGraphics()
    

    我做了一个这样写的函数:

    function UpdateText([String] $text){
        $x = ($ProgressBar1.Width / 2) - ([int]$PBCG.MeasureString($text, $Font).Width / 2)
        $y = ($ProgressBar1.Height / 2) - ([int]$PBCG.MeasureString($text, $Font).Height / 2)
        $PointF = [System.Drawing.PointF]::new($x, $y)
        $PBCG.DrawString($text, $Font, $brush1, $PointF)
    }
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2023-01-14
      • 1970-01-01
      • 2020-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多