[英]PowerShell - Add text to ProgressBar (GUI)
我正在嘗試將文本放在進度條(百分比等)上,但它不起作用。 進度條文本基於此。 下面是代碼的簡化版本。
#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()
我沒有收到任何錯誤,但它根本不顯示文本。 我錯過了什么? 有什么想法嗎?
你不應該在進度條上設置text
屬性嗎
$progressBarText
應該是$progressBar.Text
PowerShell 中的那個對你不起作用有什么原因嗎? 這是我每天多次使用的真實腳本的片段。 您也許可以根據自己的需要對其進行調整。 我意識到它不是 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"
}
}
(如何真正將安全密碼輸入剪貼板是留給讀者的單獨任務。)
我是如何做到的,它奏效了。
我把它放在我的腳本的開頭是這樣的:
$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)
}
我希望它對你有幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.