如何在Winforms上绘制自己的进度栏?

Dom*_*tal 1 c# user-interface colors winforms progress-bar

溜溜专家!我的Windowsform(NOT WPF)上有多个进度条,我想为每种颜色使用不同的颜色。我怎样才能做到这一点?我搜索了,发现必须创建自己的控件。但是我不知道如何执行此操作。任何的想法?例如,progressBar1为绿色,progressbar2为红色。

编辑:哦,我想解决此问题,而不删除Application.EnableVisualStyles();。行,因为它将使我的表单查找不便:/

Han*_*ant 5

是的,创建自己的。粗略的草稿可让您达到80%的水平,并根据需要进行修饰:

using System;
using System.Drawing;
using System.Windows.Forms;

class MyProgressBar : Control {
    public MyProgressBar() {
        this.SetStyle(ControlStyles.ResizeRedraw, true);
        this.SetStyle(ControlStyles.Selectable, false);
        Maximum = 100;
        this.ForeColor = Color.Red;
        this.BackColor = Color.White;
    }
    public decimal Minimum { get; set; }  // fix: call Invalidate in setter
    public decimal Maximum { get; set; }  // fix as above

    private decimal mValue;
    public decimal Value {
        get { return mValue; }
        set { mValue = value; Invalidate(); }
    }

    protected override void OnPaint(PaintEventArgs e) {
        var rc = new RectangleF(0, 0, (float)(this.Width * (Value - Minimum) / Maximum), this.Height);
        using (var br = new SolidBrush(this.ForeColor)) {
            e.Graphics.FillRectangle(br, rc);
        }
        base.OnPaint(e);
    }
}
Run Code Online (Sandbox Code Playgroud)