标签:winform style blog io ar color os sp for
接触Winform时间也不长,控件自绘也是刚开始学,现在的打算是一点一点积累,争取能够有点小成果。今天分享的是一个自定义的进度条控件,实现很简单,也没有做什么美化,所以有兴趣的就粗略看看。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Linq; using System.Text; using System.Windows.Forms; using System.Drawing; using System.Drawing.Drawing2D; namespace AnnularProgressBarDemo { public partial class AnnularProgressBar : Control { /// <summary> /// 前景色,即进度条颜色 /// </summary> private SolidBrush _ForeBrush = new SolidBrush(Color.Red); public SolidBrush ForeBrush { get { return _ForeBrush; } set { _ForeBrush = value; Invalidate(); } } /// <summary> /// 遮盖颜色,即环内部的颜色 /// </summary> private SolidBrush _MaskBrush = new SolidBrush(SystemColors.Control); public SolidBrush MaskBrush { get { return _MaskBrush; } set { _MaskBrush = value; Invalidate(); } } /// <summary> /// 起始角度 /// </summary> private float _StartAngle = 0.0f; public float StartAngle { get { return _StartAngle; } set { _StartAngle = value; Invalidate(); } } /// <summary> /// 跨度 /// </summary> private float _SweepAngle = 0.0f; public float SweepAngle { get { return _SweepAngle; } set { _SweepAngle = value; Invalidate(); } } /// <summary> /// 进度 /// </summary> private float _Value = 0.0f; public float Value { get { return _Value; } set { if (value < _MinValue) { _Value = _MinValue; } else if (value > _MaxValue) { _Value = _MaxValue; } else { _Value = value; SweepAngle = (_Value-_MinValue) * 360 / (_MaxValue-_MinValue); } Invalidate(); } } //最小值 private float _MinValue = 0.0f; public float MinValue { get { return _MinValue; } set { if (value < 0) { _MinValue = 0; } else { _MinValue = value; } } } /// <summary> /// 初始值 /// </summary> private float _InitValue = 0.0f; public float InitValue { get { return _InitValue; } set { if (value < _MinValue) { _InitValue = _MinValue; } else { _InitValue = value; } _Value = _InitValue; SweepAngle = (_InitValue - _MinValue) * 360 / (_MaxValue - _MinValue); } } /// <summary> /// 最大值 /// </summary> private float _MaxValue = 0.0f; public float MaxValue { get { return _MaxValue; } set { if (value < _MinValue) { _MaxValue = _MinValue; } else { _MaxValue = value; } } } /// <summary> /// 边框颜色 /// </summary> private Color _BorderColor = Color.Black; public Color BorderColor { get { return _BorderColor; } set { _BorderColor = value; borderpen = new Pen(BorderColor, BorderWidth); Invalidate(); } } /// <summary> /// 边框宽度 /// </summary> private int _BorderWidth = 1; public int BorderWidth { get { return _BorderWidth; } set { _BorderWidth = value; borderpen = new Pen(BorderColor, BorderWidth); Invalidate(); } } /// <summary> /// 边框画笔 /// </summary> private Pen borderpen; /// <summary> /// 外轮廓矩形 /// </summary> private Rectangle OuterRect; /// <summary> /// 内轮廓矩形 /// </summary> private Rectangle InnerRect; public AnnularProgressBar() { InitializeComponent(); this.SetStyle(ControlStyles.UserPaint, true);//自绘 this.SetStyle(ControlStyles.DoubleBuffer, true);// 双缓冲 this.SetStyle(ControlStyles.ResizeRedraw, true);//调整大小时重绘 this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景. this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);// 双缓冲 this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); //透明效果 } private void Init() { borderpen = new Pen(Color.Black, 1); } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); Graphics graph = e.Graphics; Image img = new Bitmap(ClientRectangle.Width, ClientRectangle.Height); Graphics gbmp = Graphics.FromImage(img); gbmp.SmoothingMode = SmoothingMode.HighQuality; gbmp.PageUnit = GraphicsUnit.Display; gbmp.FillPie(ForeBrush, OuterRect, StartAngle, SweepAngle); gbmp.DrawEllipse(borderpen, OuterRect); gbmp.FillEllipse(MaskBrush, InnerRect); gbmp.DrawEllipse(borderpen, InnerRect); graph.DrawImage(img, ClientRectangle); } protected override void OnSizeChanged(EventArgs e) { base.OnSizeChanged(e); OuterRect = ClientRectangle; OuterRect.Inflate(-1, -1); InnerRect = new Rectangle(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height); InnerRect.Inflate(-11, -11); } } }
方法很简单,就是靠GDI+画一个Pie,然后再画一个圆遮住中间的部分,其他的就是靠调节相关的值来实现了。用的话也很简单,初始化MinValue、MaxValue和InitValue值后就可以通过改变Value的值来完成了。
标签:winform style blog io ar color os sp for
原文地址:http://www.cnblogs.com/lvniao/p/4154061.html