我试图在进度条(百分比等)上放置一个文本,但它不起作用。进度条文本基于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()我没有收到任何错误,但它只是不显示文本。我遗漏了什么?有什么想法吗?
发布于 2018-07-04 04:54:32
您不应该在进度条上设置text属性吗
$progressBarText应为$progressBar.Text
发布于 2018-07-04 09:19:02
有什么原因PowerShell中的那个不能为你工作吗?下面是我每天多次使用的真实脚本的一段代码。您也许能够根据自己的需要对其进行调整。我知道它不是图形用户界面,但它是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"
}
}(如何真正将安全密码放入剪贴板是留给读者的一个单独任务。)
发布于 2020-04-15 20:55:37
我是如何做到的,它是如何工作的。
我把它放在我的脚本的开头,如下所示:
$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)
}我希望这对你有帮助
https://stackoverflow.com/questions/51162410
复制相似问题