【问题标题】:How to drawn my own progressbar on winforms?如何在winforms中绘制自己的进度条?
【发布时间】:2011-04-19 00:01:15
【问题描述】:

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

编辑:哦,我想解决这个问题,但不删除 Application.EnableVisualStyles(); 行,因为它会搞砸我的表格查找:/

【问题讨论】:

标签: c# winforms user-interface colors progress-bar


【解决方案1】:

是的,创建你自己的。让你达到 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);
    }
}

【讨论】:

  • 它可以工作,但我现在无法访问我的设计器:( 为防止在加载设计器之前可能丢失数据,必须解决以下错误:解析 EntityName 时发生错误。第 2 行,位置 97 .
猜你喜欢
  • 2011-10-26
  • 2012-10-30
  • 2010-10-12
  • 2013-06-12
  • 1970-01-01
  • 1970-01-01
  • 2012-08-21
  • 1970-01-01
相关资源
最近更新 更多