首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >图形用户界面-向图形用户界面添加文本( PowerShell )

图形用户界面-向图形用户界面添加文本( PowerShell )
EN

Stack Overflow用户
提问于 2018-07-04 04:28:43
回答 3查看 2.7K关注 0票数 2

我试图在进度条(百分比等)上放置一个文本,但它不起作用。进度条文本基于this。下面是代码的简化版本。

代码语言:javascript
复制
#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()

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

EN

回答 3

Stack Overflow用户

发布于 2018-07-04 04:54:32

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

$progressBarText应为$progressBar.Text

票数 3
EN

Stack Overflow用户

发布于 2018-07-04 09:19:02

有什么原因PowerShell中的那个不能为你工作吗?下面是我每天多次使用的真实脚本的一段代码。您也许能够根据自己的需要对其进行调整。我知道它不是图形用户界面,但它是100%的PowerShell。

代码语言:javascript
复制
       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"
            }
        }

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

票数 1
EN

Stack Overflow用户

发布于 2020-04-15 20:55:37

我是如何做到的,它是如何工作的。

我把它放在我的脚本的开头,如下所示:

代码语言:javascript
复制
$Font = New-Object System.Drawing.Font('Arial', 10, 'Bold', 'Pixel')
$brush1 = New-Object Drawing.SolidBrush([System.Drawing.Color]::Black)
$PBCG = $ProgressBar1.CreateGraphics()

我编写了一个函数,如下所示:

代码语言:javascript
复制
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)
}

我希望这对你有帮助

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51162410

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档