VS C# Winform 重写进度条

class HorizonProgressBar : ProgressBar
{
private double doubleValue = 0;
[Category("行为"), Description("变量值"), Browsable(true)]
public double DoubleValue
{
get { return doubleValue; }
set { doubleValue = value; }
}

public double greatMax = 0;
[Category("行为"), Description("变量值"), Browsable(true)]
public double GreatMax
{
get { return greatMax; }
set { greatMax = value; }
}

public double greatMin = 0;
[Category("行为"), Description("变量值"), Browsable(true)]
public double GreatMin
{
get { return greatMin; }
set { greatMin = value; }
}

private string unitText;
[Category("行为"), Description("变量值"), Browsable(true)]
public string UnitText
{
get { return unitText; }
set { unitText = value; }
}
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.Style |= 0x04;
return cp;
}
}
public HorizonProgressBar()
{
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
}
/// <summary>
/// 从下到上
/// </summary>
/// <param name="e"></param>
protected override void OnPaint(PaintEventArgs e)
{
int HEIGHT = 18;
int HEIGHT_Bottom = 12;

Rectangle rec = new Rectangle(0, 0 + HEIGHT, this.Width - 1, this.Height - 1 - HEIGHT_Bottom);

Pen pen = new Pen(Color.Gray, 1);
SolidBrush brushGray = new SolidBrush(Color.FromArgb(172, 177, 187));

SolidBrush brushGreen = new SolidBrush(Color.FromArgb(20, 115, 230));
SolidBrush brushRange = new SolidBrush(this .ForeColor);
Font font = new Font("微软雅黑", 9f, FontStyle.Regular);
Font fontValue = new Font("微软雅黑", 12f, FontStyle.Bold);
//绘制进度条进度
Rectangle rec2 = new Rectangle(1, 1 + HEIGHT, rec.Width, rec.Height - HEIGHT);
rec2.Width = (int)(rec2.Width * ((double)DoubleValue / Maximum)) - 1;
rec2.Height -= 1;
e.Graphics.FillRectangle(brushGreen, rec2);
// e.Graphics.DrawRectangle(pen, rec);
//绘制进度条空白处
Rectangle rec1 = new Rectangle(rec2.Width, 1 + HEIGHT, rec.Width - rec2.Width, rec.Height - 1 - HEIGHT);
e.Graphics.FillRectangle(brushGray, rec1);


Pen bluepen = new Pen(Color.White, 1);

Point pN1 = new Point(1 + (int)(this.Width * ((double)GreatMin / this.Maximum)), HEIGHT+1);
Point pN6 = new Point(1 + (int)(this.Width * ((double)GreatMax / this.Maximum)), HEIGHT+1);
e.Graphics.DrawLine(bluepen, pN1, new Point(pN1.X, pN1.Y + rec2.Height-1));
e.Graphics.DrawLine(bluepen, pN6, new Point(pN6.X, pN6.Y + rec2.Height-1));

//绘制刻度数字
string str1 = GreatMin.ToString() + UnitText;
string str6 = GreatMax.ToString() + UnitText;
e.Graphics.DrawString(DoubleValue.ToString("f1"), fontValue, brushRange, (this.Width - (20 - 5 * Value.ToString().Length)) / 2, -2);
//e.Graphics.DrawString(str1, font, brushRange, 20 - 5 * str1.Length, -2 + this.Height - HEIGHT_Bottom+1);
//e.Graphics.DrawString(str6, font, brushRange, (int)((this.Width - 1) - 15 - (5 * str6.Length)), -2 + this.Height - HEIGHT_Bottom+1);
if (GreatMin==0)
{
e.Graphics.DrawString(str1, font, brushRange,pN1.X -( str1.Length), -2 + this.Height - HEIGHT_Bottom + 1);
}
else
e.Graphics.DrawString(str1, font, brushRange,pN1.X -( 8 * str1.Length), -2 + this.Height - HEIGHT_Bottom + 1);
e.Graphics.DrawString(str6, font, brushRange, pN6.X, -2 + this.Height - HEIGHT_Bottom + 1);
}


}

一个不努力的人在努力做到努力
原文地址:https://www.cnblogs.com/Betty-IT/p/15162351.html