2011-03-05 181 views
1

我在winform中使用windows进度条。它的最小值是20,最大值是120.我必须在进度条中设置value = 60的阈值,这样如果值保持低于60,那么进度条颜色将是红色,如果值大于60,则进度条颜色应该是绿色。 最重要的是,我想要将此阈值显示为应该可见的任何可能是进度条绿色或红色颜色的progessbar中的一行。 有没有人有想法?您的帮助将不胜感激。如何在windows进度条控件中设置阈值?

+1

你的意思是财产以后像我们在我的电脑资源管理器中查看驱动器卷的 – 2011-03-05 09:36:12

+0

可能重复[如何改变进度条的颜色在C#.NET 3.5?](http://stackoverflow.com/questions/778678/how-to-change-the-color-of-progressbar-in-c-net-3-5) – 2011-03-05 09:46:31

回答

1

你可以试试这个(但是你不会得到动画)。

它是添加了阈值功能的this answer的修改版本。

public class NewProgressBar : ProgressBar 
{ 
    //This property takes the Threshold. 
    public int Threshold{ get; set; } 

    public NewProgressBar() 
    { 
     this.SetStyle(ControlStyles.UserPaint, true); 
    } 

    protected override void OnPaint(PaintEventArgs e) 
    { 
     Rectangle rec = e.ClipRectangle; 

     rec.Width = (int)(rec.Width * ((double)Value/Maximum)) - 4; 
     if(ProgressBarRenderer.IsSupported) 
      ProgressBarRenderer.DrawHorizontalBar(e.Graphics, e.ClipRectangle); 
     rec.Height = rec.Height - 4; 
      //Check value is greater that Threshold 
      if(this.Value > Threshold) 
      { 
       e.Graphics.FillRectangle(Brushes.Green, 2, 2, rec.Width, rec.Height); 
      } 
      else 
      { 
       e.Graphics.FillRectangle(Brushes.Red, 2, 2, rec.Width, rec.Height); 
      } 
      //This line should do that 
      e.Graphics.DrawLine(Pens.Black,Threshold-1,2,Threshold-1,rec.Height); 
    } 
} 

现在你可以使用这样的:

NewProgressBar p = new NewProgressBar(); 
//Set properties 
//Set threshold 
p.Threshold = 60;  
+0

谢谢...为你的努力,但我需要的是在门槛上它也显示了值60的行,应该是可见的任何b e进度条的颜色..红色或绿色... – 2011-03-05 10:19:40

+0

@Sachin Patil:更新我的代码:) – 2011-03-05 10:35:20

+0

@ Shekhar_Pro:不工作..在DrawLine方法中x和y协调值的一些问题..仍然感谢。 。 – 2011-03-05 11:08:57

0

使用默认的ProgressBar实现,你将无法完全实现你想要的。你需要创建一个自定义控件。

如果您只想更改颜色,可以通过在百分比值更改时更改ForeColor属性来实现此目的。

+0

直到你有视觉效果才能工作启用.. – 2011-03-05 09:47:16

+0

是的,我知道... – 2011-03-05 09:51:12