问题描述
悠悠专家!
我有我的Windowsform(非WPF)几个progressbars,我想用不同的颜色各一台。
我怎样才能做到这一点?我GOOGLE了,发现我要创造我自己的控制。但我不知道,如何做到这一点。任何想法?例如progressBar1绿色,红色progressbar2
Yoyo experts!I have several progressbars on my Windowsform (NOT WPF), and I would like to use different colors for each one.How can I do this? I've googled , and found that I have to create my own control. But I have no clue , how to do this. Any idea? For example progressBar1 green, progressbar2 red.
编辑:喔,我想解决这个问题,无需拆卸Application.EnableVisualStyles();
线,因为它会搞砸我的表格查找:/
ohh, I would like to solve this, without removing the Application.EnableVisualStyles();line, because it will screw my form looking up :/
推荐答案
是,创建你自己的。一个粗略的草稿,让你80%在那里,美化需要:
Yes, create your own. A rough draft to get you 80% there, embellish as needed:
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);
}
}
这篇关于如何得出我自己对的WinForms进度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!