C#控件之:进度条(ProgressBar)

时间:2023-03-09 17:51:24
C#控件之:进度条(ProgressBar)

一、重绘进度条

public class CustomProgressBar:ProgressBar
{
public CustomProgressBar()
{
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
}
protected override void OnPaint(PaintEventArgs e)
{
SolidBrush brush = null;
Rectangle rec = new Rectangle(, , this.Width, this.Height); if(ProgressBarRenderer.IsSupported)
{
ProgressBarRenderer.DrawHorizontalBar(e.Graphics, rec);
}
Pen pen = new Pen(this.ForeColor, );
e.Graphics.DrawRectangle(pen, rec);
e.Graphics.FillRectangle(new SolidBrush(this.BackColor), , , rec.Width - , rec.Height - ); rec.Height -= ;
rec.Width = (int)(rec.Width * ((double)Value / Maximum)) - ;
brush = new SolidBrush(this.ForeColor);
e.Graphics.FillRectangle(brush, , , rec.Width, rec.Height);
}
}