using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
namespace MyProgressBar
{
class ProgressBarEx: ProgressBar
{
[Browsable(true), Category("Appearance"), Description("需要在进度条上显示的文字")]
public new string Text { get; set; }
[Browsable(true), Category("Appearance"), Description("是否在进度条上显示文字")]
public bool ShowText { get; set; }
[Browsable(true), Category("Appearance"), Description("进度条上的文字的字体")]
public new Font Font { get; set; }
[Browsable(true), Category("Appearance"), Description("进度条上的文字的颜色")]
protected Color fontColor;
protected Brush brush;
public Color FontColor { get { return fontColor; } set { fontColor = value; brush = new SolidBrush(value); } }
public ProgressBarEx()
: base()
{
ShowText = false;
Font = new Font("宋体", 9, FontStyle.Regular);
FontColor = Color.Black;
}
protected void DrawText()
{
string temp =Text;
if (string.IsNullOrEmpty(temp))
if(Value!=0)
temp = Value + "%";
SizeF size = TextRenderer.MeasureText(temp, Font);
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
this.CreateGraphics().DrawString(temp, Font, brush, this.Size.Width / 2, (this.Size.Height - size.Height) / 2, sf);
}
protected override void WndProc(ref System.Windows.Forms.Message m)
{
base.WndProc(ref m);
if (ShowText)
{ DrawText(); }
}
}
}
不知道为何不能在WndProc中使用switch(m.Msg) case: WM_PAINT 来完成重绘,另外重载的OnPaint事件处理函数总是不能执行。由于对WndProc不了解,只能用这种暴力的方式了。
而且对于Font和Color的默认值设置也不能成功,在网上找了一下用DefaultValue(typeof(Font),"宋体;9pt;style=Regular"),这种方法不能成功,后来只能退而求其次,放入构造函数中。
附上截图: