C# 如何在windows progressbar控件中设置阈值?

C# 如何在windows progressbar控件中设置阈值?,c#,winforms,C#,Winforms,我正在winform中使用windows progressbar。它的最小值是20,最大值是120。我必须在进度条中为值=60设置阈值,这样,如果值保持在60以下,则进度条颜色将为红色;如果值大于60,则进度条颜色应为绿色。 最重要的是,我想在progressbar中将这个阈值显示为一条线,无论progressbar的颜色是绿色还是红色,它都应该是可见的。 有人知道吗??非常感谢您的帮助。使用默认的ProgressBar实现,您将无法完全实现所需的功能。您需要创建一个自定义控件 如果您只想更改颜

我正在winform中使用windows progressbar。它的最小值是20,最大值是120。我必须在进度条中为值=60设置阈值,这样,如果值保持在60以下,则进度条颜色将为红色;如果值大于60,则进度条颜色应为绿色。 最重要的是,我想在progressbar中将这个阈值显示为一条线,无论progressbar的颜色是绿色还是红色,它都应该是可见的。
有人知道吗??非常感谢您的帮助。

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


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

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

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

您可以尝试此操作(但不会获得动画)

它是的修改版本,添加了阈值功能

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;    
你可以试试这个(但是你不会得到动画)

它是的修改版本,添加了阈值功能

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;    

您的意思是我们在我的计算机资源管理器中看到的有关驱动器卷的内容可能重复您的意思是我们在我的计算机资源管理器中看到的有关驱动器卷的内容可能重复谢谢。。。为了您的努力,但我需要的是在阈值上,它还应该显示一条值为60的线,该线应该是可见的,无论progressbar是什么颜色。。红色还是绿色…@Shekhar_Pro:不工作。。拉线法中x和y坐标值存在一些问题。。还是谢谢…谢谢。。。为了您的努力,但我需要的是在阈值上,它还应该显示一条值为60的线,该线应该是可见的,无论progressbar是什么颜色。。红色还是绿色…@Shekhar_Pro:不工作。。拉线法中x和y坐标值存在一些问题。。还是谢谢。。