Reputation: 21
I'm trying to put a text on a progress bar (percentage, etc) but it's not working. Progress bar text is based on this. Below is a simplified version of the code.
#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()
I Don't receive any error, but it simply doesn't show the text. What am I missing? any thoughts?
Upvotes: 2
Views: 4229
Reputation: 1
How I did it and it worked.
I put it at the beginning of my script like this:
$Font = New-Object System.Drawing.Font('Arial', 10, 'Bold', 'Pixel')
$brush1 = New-Object Drawing.SolidBrush([System.Drawing.Color]::Black)
$PBCG = $ProgressBar1.CreateGraphics()
I did a function that writes, like this:
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)
}
I hope it helped you
Upvotes: 0
Reputation: 8356
is there some reason the one in PowerShell won't work for you? Here's a snippet from a real script that I use multiple times per day. You might be able to tweak it to your needs. I realize it's not a GUI but it is 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"
}
}
(How you really get a secure password into the clipboard is a separate task left to the reader.)
Upvotes: 1
Reputation: 2676
Shouldn't you be setting the text
property on the progressbar
$progressBarText
should be $progressBar.Text
Upvotes: 3