Powershell进度条颜色

时间:2014-07-03 15:52:19

标签: powershell progress-bar

我正在尝试更改进度条值的颜色,但我似乎无法让它工作。进度条本身可以工作但它仍然是默认的绿色。任何帮助将不胜感激。

function  Add_ProgressBar ($name, $parent, $x, $y, $l, $h, $text){
    $object = New-Object System.Windows.Forms.progressbar
    $object.Location = New-Object System.Drawing.Point($x, $y)
    $object.Size = New-Object System.Drawing.size($l, $h)
    $object.Text = $text
    $object.Style  = 'Marquee'
    $object.ForeColor = 'Aqua'
    New-Variable $name -Value $object -Scope global
    (Get-Variable $parent).Value.Controls.Add((Get-Variable $name).Value)

}

1 个答案:

答案 0 :(得分:1)

如果您是从ISE运行它,可能是VisualStyles已启用...

[System.Windows.Forms.Application]::EnableVisualStyles()

...这会阻止您的进度条成为您想要的颜色。我不知道一个好的方法来禁用(或防止在ISE中启用它,假设它没有破坏所需的东西)。如果您从控制台运行代码,它应该看起来像您希望的那样。

我在我的控制台上运行了这个示例,当点击按钮时,它显示了带有红色前景的黑色背景。

[void][System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")

$WinForm = new-object Windows.Forms.Form   
$WinForm.text = "ListBox Control"   
$WinForm.Size = new-object Drawing.Size(350,200)
$Button = new-object Windows.Forms.Button


$Script:Progressbar = New-Object System.Windows.Forms.progressbar
$Script:Progressbar.Location = New-Object System.Drawing.Point(0, 120)
$Script:Progressbar.Width = 350
$Script:Progressbar.Height = 15
$Script:Progressbar.Style  = 'Marquee'
$Script:Progressbar.ForeColor = 'Red'
$Script:Progressbar.BackColor='Black'
$Script:Progressbar.Style = 'Continuous'
$winform.controls.add($Script:Progressbar)
$winform.controls.add($button)

$WinForm.Add_Shown($WinForm.Activate()) 
$Button.Add_Click({
    1..$Script:Progressbar.Maximum | ForEach {
        $Script:Progressbar.Increment(1)
    }
}) 
$WinForm.showdialog() | out-null