继承ProgressBar
public enum TextPosition
{
Left,
Right,
Center,
Sliding,
None
}
public class BzProgressBar : ProgressBar
{
private Color channelColor = Color.LightSteelBlue;
private Color sliderColor = Color.RoyalBlue;
private Color foreBackColor = Color.RoyalBlue;
private int channelHeight = 6;
private int sliderHeight = 6;
private TextPosition showValue = TextPosition.Right;
public BzProgressBar()
{
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.ForeColor = Color.White;
}
[Category("BZ Advance")]
public Color ChannelColor
{
get
{
return channelColor;
}
set
{
channelColor = value;
this.Invalidate();
}
}
[Category("BZ Advance")]
public Color SliderColor
{
get
{
return sliderColor;
}
set
{
sliderColor = value;
this.Invalidate();
}
}
[Category("BZ Advance")]
public Color ForeBackColor
{
get
{
return foreBackColor;
}
set
{
foreBackColor = value;
this.Invalidate();
}
}
[Category("BZ Advance")]
public int ChannelHeight
{
get
{
return channelHeight;
}
set
{
channelHeight = value;
this.Invalidate();
}
}
[Category("BZ Advance")]
public int SliderHeight
{
get
{
return sliderHeight;
}
set
{
sliderHeight = value;
this.Invalidate();
}
}
[Category("BZ Advance")]
public TextPosition ShowValue
{
get
{
return showValue;
}
set
{
showValue = value;
this.Invalidate();
}
}
[Category("BZ Advance")]
[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Always)]
public override Font Font
{
get
{
return base.Font;
}
set
{
base.Font = value;
}
}
[Category("BZ Advance")]
public override Color ForeColor
{
get
{
return base.ForeColor;
}
set
{
base.ForeColor = value;
}
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics graphics = e.Graphics;
Rectangle rectChannel = new Rectangle(0, 0, this.Width, channelHeight);
using (var brushChannel = new SolidBrush(channelColor))
{
if (channelHeight >= sliderHeight)
{
rectChannel.Y = this.Height - channelHeight;
}
else
{
rectChannel.Y = this.Height - ((channelHeight + sliderHeight) / 2);
}
graphics.Clear(this.Parent.BackColor);
graphics.FillRectangle(brushChannel, rectChannel);
}
double scaleFactor = (((double)this.Value - this.Minimum)) / (((double)this.Maximum - this.Minimum));
int sliderWidth = (int)(this.Width * scaleFactor);
Rectangle rectSlider = new Rectangle(0, 0, sliderWidth, SliderHeight);
using (var brushSlider = new SolidBrush(sliderColor))
{
if (SliderHeight >= channelHeight)
{
rectSlider.Y = this.Height - sliderHeight;
}
else
{
rectSlider.Y = this.Height - (sliderHeight + channelHeight) / 2;
}
if (sliderWidth > 1)
{
graphics.FillRectangle(brushSlider, rectSlider);
}
if (ShowValue != TextPosition.None)
{
DrawValueText(graphics, sliderWidth, rectSlider);
}
}
base.OnPaint(e);
}
///
/// 显示进度百分比
///
///
///
///
private void DrawValueText(Graphics graphics, int sliderWidth, Rectangle rectSlider)
{
string text = this.Value.ToString() + "%";
var textSize = TextRenderer.MeasureText(text, this.Font);
var rectText = new Rectangle(0, 0, textSize.Width, textSize.Height + 2);
using (var brushText = new SolidBrush(this.ForeColor))
using (var brushTextBack = new SolidBrush(this.ForeBackColor))
using (var textFormat = new StringFormat())
{
switch (showValue)
{
case TextPosition.Left:
rectText.X = 0;
textFormat.Alignment = StringAlignment.Near;
break;
case TextPosition.Right:
rectText.X = this.Width - textSize.Width;
textFormat.Alignment = StringAlignment.Far;
break;
case TextPosition.Center:
rectText.X = (this.Width - textSize.Width) / 2;
textFormat.Alignment = StringAlignment.Center;
break;
case TextPosition.Sliding:
rectText.X = sliderWidth - textSize.Width;
textFormat.Alignment = StringAlignment.Center;
using (var brushClear = new SolidBrush(this.Parent.BackColor))
{
var rect = rectSlider;
rect.Y = rectText.Y;
rect.Height = rect.Height;
graphics.FillRectangle(brushClear, rect);
}
break;
default:
break;
}
graphics.FillRectangle(brushTextBack, rectText);
graphics.DrawString(text, this.Font, brushText, rectText, textFormat);
}
}
}
页面更新:2024-04-29
本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828
© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号