lysProcessBar控件

自己以前的一个进度条控件。分享给大家。上图有真像。

此控件提供进度显示、值字体布局、进度条颜色渐变显示。

View Code
1 // ====================================================================================
2  // 模块功能:lysProcessBar 控件
3  // 创建时间:2009-10-10
4  // 创 建 人:liuyunsheng
5 // 备 注:未经本人同意不得修改相关内容
6 // ====================================================================================
7 using System;
8 using System.Collections.Generic;
9 using System.ComponentModel;
10 using System.Drawing;
11 using System.Data;
12 using System.Linq;
13 using System.Text;
14 using System.Windows.Forms;
15 using System.Drawing.Drawing2D;
16 namespace lysControl
17 {
18 public partial class lysProcessBar : UserControl
19 {
20 #region 变量
21
22 /// <summary>
23 /// 临时绘图
24 /// </summary>
25 private Graphics m_graphics;
26 /// <summary>
27 /// 绘图缓冲区
28 /// </summary>
29 private BufferedGraphics m_bufferGraphics;
30 /// <summary>
31 /// 图形双缓冲区
32 /// </summary>
33 private BufferedGraphicsContext m_currentContext;
34
35 /// <summary>
36 /// 结束颜色
37 /// </summary>
38 private Color _yColor = Color.DarkBlue;
39 /// <summary>
40 /// 开始颜色
41 /// </summary>
42 private Color _xColor = Color.White;
43 /// <summary>
44 /// 值颜色
45 /// </summary>
46 private Color _valueFontColor = Color.Red;
47
48 /// <summary>
49 ///
50 /// </summary>
51 private int _value = 0;
52
53 /// <summary>
54 /// 最大值
55 /// </summary>
56 private int _maxValue = 100;
57
58 #endregion
59
60 #region 属性
61
62 [DefaultValue(true)]
63 [Category("lysProcessBar")]
64 [Description("左侧颜色")]
65 [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
66 public Color XColor
67 {
68 set
69 {
70 _xColor = value;
71 }
72
73 get
74 {
75 return _xColor;
76 }
77 }
78
79 [DefaultValue(true)]
80 [Category("lysProcessBar")]
81 [Description("右侧颜色")]
82 [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
83 public Color YColor
84 {
85 set
86 {
87 _yColor = value;
88 }
89
90 get
91 {
92 return _yColor;
93 }
94 }
95
96 [Category("lysProcessBar")]
97 [Description("值字体布局")]
98 [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
99 public FontDockStyle FontDock
100 {
101 set;
102 get;
103 }
104
105 [Category("lysProcessBar")]
106 [Description("显示值")]
107 [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
108 public bool ShowFont
109 { set; get; }
110
111 [Category("lysProcessBar")]
112 [Description("显示值颜色")]
113 [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
114 public Color ValueFontColor
115 {
116 set
117 {
118 _valueFontColor = value;
119 }
120
121 get
122 {
123 return _valueFontColor;
124 }
125 }
126
127 [DefaultValue(true)]
128 [Category("lysProcessBar")]
129 [Description("最大值")]
130 public int MaxValue
131 {
132 set
133 {
134 _maxValue = value;
135 }
136
137 get
138 {
139 return _maxValue;
140 }
141 }
142
143 [DefaultValue(true)]
144 [Category("lysProcessBar")]
145 [Description("")]
146 public int Value
147 {
148 set
149 {
150 _value = value;
151
152 if (_value > MaxValue)
153 {
154 _value = MaxValue;
155 }
156
157 else if (_value < 0)
158 {
159 _value = 0;
160 }
161
162 this.DrawProcessBar();
163 }
164
165 get
166 {
167 return _value;
168 }
169 }
170
171 [DefaultValue(true)]
172 [Category("lysProcessBar")]
173 [Description("颜色渐变方向")]
174 [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
175 public LinearGradientMode ProcessMode
176 { get; set; }
177
178 [Category("lysProcessBar")]
179 [Description("显示百分比")]
180 [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
181 public bool Precent
182 { set; get; }
183
184 #endregion
185
186 #region 方法
187
188 public lysProcessBar()
189 {
190 InitializeComponent();
191 }
192
193 private void lysProcessBar_Paint(object sender, PaintEventArgs e)
194 {
195 this.DrawProcessBar();
196 }
197
198 private void lysProcessBar_Validating(object sender, CancelEventArgs e)
199 {
200 this.DrawProcessBar();
201 }
202
203 public void DrawProcessBar()
204 {
205 Graphics _graphics = Graphics.FromHwnd(base.Handle);
206 Rectangle _rectangle = new Rectangle(0, 0, base.Size.Width, base.Size.Height);
207 m_currentContext = BufferedGraphicsManager.Current;
208 m_bufferGraphics = m_currentContext.Allocate(_graphics, _rectangle);
209 m_graphics = _graphics;
210 _rectangle = new Rectangle(new Point(1, 1), new Size(base.Size.Width - 2, base.Height - 2));
211 SolidBrush _solidBrush = new SolidBrush(Color.White);
212 m_bufferGraphics.Graphics.FillRectangle(_solidBrush, _rectangle);
213 _solidBrush.Dispose();
214 int _num = base.Size.Width;
215 int _width = (int)Math.Floor((double)((_num * this.Value) / ((double)this.MaxValue)));
216
217 if (_width != 0 && _width > 2)
218 {
219 _rectangle = new Rectangle(new Point(1, 1), new Size(_width - 2, base.Height - 2));
220 LinearGradientBrush _linearGradientBrush = new LinearGradientBrush(_rectangle, XColor, YColor, ProcessMode);
221 m_bufferGraphics.Graphics.FillRectangle(_linearGradientBrush, _rectangle);
222 _linearGradientBrush.Dispose();
223 }
224
225 if (this.ShowFont == true)
226 {
227 Font drawFont = new Font("Arial", 8);
228 SolidBrush _solidbrush = new SolidBrush(this.ValueFontColor);
229 PointF _pointF = new PointF();
230 StringFormat _stringFormat = new StringFormat();
231
232 switch (this.FontDock)
233 {
234 case FontDockStyle.Top:
235 _pointF = new PointF((float)(base.Size.Width / 2) - drawFont.Size * 2, 1);
236 break;
237
238 case FontDockStyle.Bottom:
239 _pointF = new PointF((float)(base.Size.Width / 2) - drawFont.Size * 2, (float)(base.Size.Height - drawFont.Size * 2));
240 break;
241
242 case FontDockStyle.Left:
243 _pointF = new PointF(1, (float)(base.Size.Height / 2 - drawFont.Size));
244 _stringFormat.Alignment = StringAlignment.Near;
245 break;
246
247 case FontDockStyle.Right:
248 _pointF = new PointF(base.Size.Width - 1, (float)(base.Size.Height / 2 - drawFont.Size));
249 _stringFormat.Alignment = StringAlignment.Far;
250 break;
251
252 case FontDockStyle.Center:
253 _pointF = new PointF((float)(base.Size.Width / 2) - drawFont.Size * 2, (float)(base.Size.Height / 2 - drawFont.Size));
254 break;
255 }
256
257 if (this.Precent == true)
258 {
259 m_bufferGraphics.Graphics.DrawString(this.Value.ToString() + "%", drawFont, _solidbrush, _pointF, _stringFormat);
260 }
261
262 else
263 {
264 m_bufferGraphics.Graphics.DrawString(this.Value.ToString() + "/" + this.MaxValue.ToString(), drawFont, _solidbrush, _pointF, _stringFormat);
265 }
266
267 _stringFormat.Dispose();
268 drawFont.Dispose();
269 _solidbrush.Dispose();
270 }
271
272 m_bufferGraphics.Render(m_graphics);
273 }
274
275 #endregion
276 }
277
278 public enum FontDockStyle
279 {
280 // 摘要:
281 // 该控件未停靠。
282 None = 0,
283 //
284 // 摘要:
285 // 该控件的上边缘停靠在其包含控件的顶端。
286 Top = 1,
287 //
288 // 摘要:
289 // 该控件的下边缘停靠在其包含控件的底部。
290 Bottom = 2,
291 //
292 // 摘要:
293 // 该控件的左边缘停靠在其包含控件的左边缘。
294 Left = 3,
295 //
296 // 摘要:
297 // 该控件的右边缘停靠在其包含控件的右边缘。
298 Right = 4,
299 //
300 // 摘要:
301 // 控件的各个边缘分别停靠在其包含控件的各个边缘,并且适当调整大小。
302 Center = 5,
303 }
304 }

运用双缓存机制避免闪烁.

原文地址:https://www.cnblogs.com/liuyunsheng/p/2020845.html