这里提供的代码是示意图中的第二个进度条的代码。
代码:
Code
1// 功能:普通的进度条,带有百分比提示
2// 描述:
3// 编码:温伟鹏
4// 日期:12/18/2008 08:39:22
5
6using System;
7using System.Collections.Generic;
8using System.Text;
9using System.ComponentModel;
10using System.Drawing;
11using System.Drawing.Drawing2D;
12using System.Drawing.Text;
13using System.Threading;
14
15namespace Esint.UI.WinFormUI
16{
17 public class StandardProgressBar:ControlBase
18 {
19 private BackgroundBase barBackground;
20 private float maximum;
21 private float minimum;
22 private string percentMsgFormat;
23 private float value;
24
25 /**//// <summary>
26 /// 获取或设置进度条的Bar前景色
27 /// </summary>
28 [Category("Appearance"), Description("获取或设置进度条的Bar前景色")]
29 public BackgroundBase BarBackground
30 {
31 get { return this.barBackground; }
32 set { this.barBackground = value; this.Refresh(); }
33 }
34 /**//// <summary>
35 /// 获取或设置进度条的最大值
36 /// </summary>
37 [Category("Appearance"), DefaultValue(100f), Description("获取或设置进度条的最大值")]
38 public float Maximum
39 {
40 get { return this.maximum; }
41 set {
42 this.maximum = value;
43 if (this.maximum <= 0)
44 {
45 this.maximum = 100;
46 }
47 if (this.maximum <= this.minimum)
48 {
49 this.maximum = this.minimum + 1;
50 }
51 this.Refresh();
52 }
53 }
54 /**//// <summary>
55 /// 获取或设置进度条的最小值
56 /// </summary>
57 [Category("Appearance"), DefaultValue(0f), Description("获取或设置进度条的最小值")]
58 public float Minimum
59 {
60 get { return this.minimum; }
61 set {
62 this.minimum = value;
63 if (this.minimum < 0)
64 {
65 this.minimum = 0;
66 }
67 if (this.minimum >= this.maximum)
68 {
69 this.minimum = this.maximum - 1;
70 }
71 this.Refresh();
72 }
73 }
74 /**//// <summary>
75 /// 获取或设置进度条的当前值
76 /// </summary>
77 [Category("Appearance"), DefaultValue(0f), Description("获取或设置进度条的当前值")]
78 public float Value
79 {
80 get { return this.value; }
81 set {
82 this.value = value;
83 if (this.value < this.minimum)
84 {
85 this.value = this.minimum;
86 }
87 if (this.value > this.maximum)
88 {
89 this.value = this.maximum;
90 }
91 this.Refresh();
92 }
93 }
94 /**//// <summary>
95 /// 获取或设置进度条的百分比格式化字符串
96 /// </summary>
97 [Category("Appearance"), DefaultValue("{0}%"), Description("获取或设置进度条的百分比格式化字符串")]
98 public string PercentMsgFormat
99 {
100 get { return this.percentMsgFormat; }
101 set { this.percentMsgFormat = value; this.Refresh(); }
102 }
103 /**//// <summary>
104 /// 获取进度条的进度百分比
105 /// </summary>
106 public string PercentMsg
107 {
108 get {
109 float percent = Value * 100 / Maximum;
110
111 return string.Format(PercentMsgFormat, percent.ToString());
112 }
113 }
114
115 public StandardProgressBar()
116 :base()
117 {
118 }
119
120 protected override void InitializeBackground()
121 {
122 base.InitializeBackground();
123
124 this.Background = new BackgroundBase();
125
126 Color[] colors =
127 {
128 Color.White,
129 Color.FromArgb(249,249,249),
130 Color.FromArgb(220,220,220),
131 Color.FromArgb(198,198,198),
132 Color.FromArgb(220,220,220),
133 Color.FromArgb(249,249,249),
134 Color.White
135 };
136
137 float[] positions =
138 {
139 0.0f,
140 0.2f,
141 0.4f,
142 0.5f,
143 0.6f,
144 0.8f,
145 1.0f
146 };
147
148 this.Background.ColorBlend.Colors = colors;
149 this.Background.ColorBlend.Positions = positions;
150
151 this.barBackground = new BackgroundBase();
152
153 colors = new Color[]
154 {
155 Color.FromArgb(210,255,198),
156 Color.FromArgb(180,255,147),
157 Color.FromArgb(157,233,122),
158 Color.FromArgb(60,132,20),
159 Color.FromArgb(157,233,122),
160 Color.FromArgb(180,255,147),
161 Color.FromArgb(210,255,198)
162 };
163
164 positions = new float[]
165 {
166 0.0f,
167 0.2f,
168 0.4f,
169 0.5f,
170 0.6f,
171 0.8f,
172 1.0f
173 };
174
175 this.barBackground.ColorBlend.Colors = colors;
176 this.barBackground.ColorBlend.Positions = positions;
177 }
178
179 protected override void InitializeSelf()
180 {
181 //base.InitializeSelf();
182
183 this.minimum = 0;
184 this.maximum = 100;
185 this.value = 0;
186 this.PercentMsgFormat = "{0}%";
187 this.Name = "StandardProgressBar";
188
189 this.MinimumSize = new Size(200,20);
190
191 Graphics g = Graphics.FromHwnd(Handle);
192 Size size = g.MeasureString(PercentMsg, this.Font).ToSize();
193 size.Height += 4;
194 if (this.MinimumSize.Width < size.Width + 20)
195 {
196 size.Width += 20;
197 }
198 else
199 {
200 size.Width = this.MinimumSize.Width;
201 }
202 this.MinimumSize = size;
203
204 g.Dispose();
205 }
206
207 /**//// <summary>
208 /// 刷新控件
209 /// </summary>
210 public override void Refresh()
211 {
212 //base.Refresh();
213 ThreadStart method = null;
214 if (this.InvokeRequired)
215 {
216 if (method == null)
217 {
218 method = delegate
219 {
220 Invalidate();
221 };
222 }
223 this.Invoke(method);
224 }
225 }
226
227 protected override void OnPaintBackground(System.Windows.Forms.PaintEventArgs e)
228 {
229 //base.OnPaintBackground(e);
230
231 if (!Visible)
232 {
233 return;
234 }
235
236 Graphics g = e.Graphics;
237 g.SmoothingMode = SmoothingMode.AntiAlias;
238
239 Rectangle rect = this.ClientRectangle;
240 LinearGradientBrush backgroundBrush = new LinearGradientBrush(rect,
241 Color.White, Color.Black, 90f);
242 backgroundBrush.InterpolationColors = Background.ColorBlend;
243
244 g.FillRectangle(backgroundBrush, rect);
245
246 int progressWidth = (int)((Width - 1) * (Value/Maximum));
247
248 //只有当进度大于0的时候才绘制
249 if (progressWidth > 0)
250 {
251 Rectangle progressRect = new Rectangle(1, 0,
252 progressWidth,
253 Height - 1);
254 LinearGradientBrush progressBrush = new LinearGradientBrush(progressRect,
255 Color.White, Color.Black, 90f);
256
257 progressBrush.InterpolationColors = BarBackground.ColorBlend;
258
259 g.FillRectangle(progressBrush, progressRect);
260
261 if (progressBrush != null) { progressBrush.Dispose(); }
262 }
263 }
264
265 protected override void OnPaintBorder(System.Windows.Forms.PaintEventArgs e)
266 {
267 //base.OnPaintBorder(e);
268 if (!Visible)
269 {
270 return;
271 }
272
273 Graphics g = e.Graphics;
274 g.SmoothingMode = SmoothingMode.AntiAlias;
275
276 Rectangle rect = this.ClientRectangle;
277 Pen borderPen = new Pen(new SolidBrush(Border.BorderColor), Border.BorderWidth);
278
279 rect.Width--;
280 rect.Height--;
281
282 g.DrawRectangle(borderPen, rect);
283 }
284
285 protected override void OnPaintContent(System.Windows.Forms.PaintEventArgs e)
286 {
287 //base.OnPaintContent(e);
288 if (!Visible)
289 {
290 return;
291 }
292
293 Graphics g = e.Graphics;
294 g.SmoothingMode = SmoothingMode.AntiAlias;
295 g.TextRenderingHint = TextRenderingHint.SystemDefault;
296
297 Size size = g.MeasureString(PercentMsg, this.Font).ToSize();
298
299 PointF txtLocation = new PointF(
300 (float)((Width - size.Width) / 2),
301 (float)((Height - size.Height) / 2));
302
303 g.DrawString(PercentMsg, this.Font, new SolidBrush(ForeColor), txtLocation);
304
305 }
306 }
307}
308
309
1// 功能:普通的进度条,带有百分比提示
2// 描述:
3// 编码:温伟鹏
4// 日期:12/18/2008 08:39:22
5
6using System;
7using System.Collections.Generic;
8using System.Text;
9using System.ComponentModel;
10using System.Drawing;
11using System.Drawing.Drawing2D;
12using System.Drawing.Text;
13using System.Threading;
14
15namespace Esint.UI.WinFormUI
16{
17 public class StandardProgressBar:ControlBase
18 {
19 private BackgroundBase barBackground;
20 private float maximum;
21 private float minimum;
22 private string percentMsgFormat;
23 private float value;
24
25 /**//// <summary>
26 /// 获取或设置进度条的Bar前景色
27 /// </summary>
28 [Category("Appearance"), Description("获取或设置进度条的Bar前景色")]
29 public BackgroundBase BarBackground
30 {
31 get { return this.barBackground; }
32 set { this.barBackground = value; this.Refresh(); }
33 }
34 /**//// <summary>
35 /// 获取或设置进度条的最大值
36 /// </summary>
37 [Category("Appearance"), DefaultValue(100f), Description("获取或设置进度条的最大值")]
38 public float Maximum
39 {
40 get { return this.maximum; }
41 set {
42 this.maximum = value;
43 if (this.maximum <= 0)
44 {
45 this.maximum = 100;
46 }
47 if (this.maximum <= this.minimum)
48 {
49 this.maximum = this.minimum + 1;
50 }
51 this.Refresh();
52 }
53 }
54 /**//// <summary>
55 /// 获取或设置进度条的最小值
56 /// </summary>
57 [Category("Appearance"), DefaultValue(0f), Description("获取或设置进度条的最小值")]
58 public float Minimum
59 {
60 get { return this.minimum; }
61 set {
62 this.minimum = value;
63 if (this.minimum < 0)
64 {
65 this.minimum = 0;
66 }
67 if (this.minimum >= this.maximum)
68 {
69 this.minimum = this.maximum - 1;
70 }
71 this.Refresh();
72 }
73 }
74 /**//// <summary>
75 /// 获取或设置进度条的当前值
76 /// </summary>
77 [Category("Appearance"), DefaultValue(0f), Description("获取或设置进度条的当前值")]
78 public float Value
79 {
80 get { return this.value; }
81 set {
82 this.value = value;
83 if (this.value < this.minimum)
84 {
85 this.value = this.minimum;
86 }
87 if (this.value > this.maximum)
88 {
89 this.value = this.maximum;
90 }
91 this.Refresh();
92 }
93 }
94 /**//// <summary>
95 /// 获取或设置进度条的百分比格式化字符串
96 /// </summary>
97 [Category("Appearance"), DefaultValue("{0}%"), Description("获取或设置进度条的百分比格式化字符串")]
98 public string PercentMsgFormat
99 {
100 get { return this.percentMsgFormat; }
101 set { this.percentMsgFormat = value; this.Refresh(); }
102 }
103 /**//// <summary>
104 /// 获取进度条的进度百分比
105 /// </summary>
106 public string PercentMsg
107 {
108 get {
109 float percent = Value * 100 / Maximum;
110
111 return string.Format(PercentMsgFormat, percent.ToString());
112 }
113 }
114
115 public StandardProgressBar()
116 :base()
117 {
118 }
119
120 protected override void InitializeBackground()
121 {
122 base.InitializeBackground();
123
124 this.Background = new BackgroundBase();
125
126 Color[] colors =
127 {
128 Color.White,
129 Color.FromArgb(249,249,249),
130 Color.FromArgb(220,220,220),
131 Color.FromArgb(198,198,198),
132 Color.FromArgb(220,220,220),
133 Color.FromArgb(249,249,249),
134 Color.White
135 };
136
137 float[] positions =
138 {
139 0.0f,
140 0.2f,
141 0.4f,
142 0.5f,
143 0.6f,
144 0.8f,
145 1.0f
146 };
147
148 this.Background.ColorBlend.Colors = colors;
149 this.Background.ColorBlend.Positions = positions;
150
151 this.barBackground = new BackgroundBase();
152
153 colors = new Color[]
154 {
155 Color.FromArgb(210,255,198),
156 Color.FromArgb(180,255,147),
157 Color.FromArgb(157,233,122),
158 Color.FromArgb(60,132,20),
159 Color.FromArgb(157,233,122),
160 Color.FromArgb(180,255,147),
161 Color.FromArgb(210,255,198)
162 };
163
164 positions = new float[]
165 {
166 0.0f,
167 0.2f,
168 0.4f,
169 0.5f,
170 0.6f,
171 0.8f,
172 1.0f
173 };
174
175 this.barBackground.ColorBlend.Colors = colors;
176 this.barBackground.ColorBlend.Positions = positions;
177 }
178
179 protected override void InitializeSelf()
180 {
181 //base.InitializeSelf();
182
183 this.minimum = 0;
184 this.maximum = 100;
185 this.value = 0;
186 this.PercentMsgFormat = "{0}%";
187 this.Name = "StandardProgressBar";
188
189 this.MinimumSize = new Size(200,20);
190
191 Graphics g = Graphics.FromHwnd(Handle);
192 Size size = g.MeasureString(PercentMsg, this.Font).ToSize();
193 size.Height += 4;
194 if (this.MinimumSize.Width < size.Width + 20)
195 {
196 size.Width += 20;
197 }
198 else
199 {
200 size.Width = this.MinimumSize.Width;
201 }
202 this.MinimumSize = size;
203
204 g.Dispose();
205 }
206
207 /**//// <summary>
208 /// 刷新控件
209 /// </summary>
210 public override void Refresh()
211 {
212 //base.Refresh();
213 ThreadStart method = null;
214 if (this.InvokeRequired)
215 {
216 if (method == null)
217 {
218 method = delegate
219 {
220 Invalidate();
221 };
222 }
223 this.Invoke(method);
224 }
225 }
226
227 protected override void OnPaintBackground(System.Windows.Forms.PaintEventArgs e)
228 {
229 //base.OnPaintBackground(e);
230
231 if (!Visible)
232 {
233 return;
234 }
235
236 Graphics g = e.Graphics;
237 g.SmoothingMode = SmoothingMode.AntiAlias;
238
239 Rectangle rect = this.ClientRectangle;
240 LinearGradientBrush backgroundBrush = new LinearGradientBrush(rect,
241 Color.White, Color.Black, 90f);
242 backgroundBrush.InterpolationColors = Background.ColorBlend;
243
244 g.FillRectangle(backgroundBrush, rect);
245
246 int progressWidth = (int)((Width - 1) * (Value/Maximum));
247
248 //只有当进度大于0的时候才绘制
249 if (progressWidth > 0)
250 {
251 Rectangle progressRect = new Rectangle(1, 0,
252 progressWidth,
253 Height - 1);
254 LinearGradientBrush progressBrush = new LinearGradientBrush(progressRect,
255 Color.White, Color.Black, 90f);
256
257 progressBrush.InterpolationColors = BarBackground.ColorBlend;
258
259 g.FillRectangle(progressBrush, progressRect);
260
261 if (progressBrush != null) { progressBrush.Dispose(); }
262 }
263 }
264
265 protected override void OnPaintBorder(System.Windows.Forms.PaintEventArgs e)
266 {
267 //base.OnPaintBorder(e);
268 if (!Visible)
269 {
270 return;
271 }
272
273 Graphics g = e.Graphics;
274 g.SmoothingMode = SmoothingMode.AntiAlias;
275
276 Rectangle rect = this.ClientRectangle;
277 Pen borderPen = new Pen(new SolidBrush(Border.BorderColor), Border.BorderWidth);
278
279 rect.Width--;
280 rect.Height--;
281
282 g.DrawRectangle(borderPen, rect);
283 }
284
285 protected override void OnPaintContent(System.Windows.Forms.PaintEventArgs e)
286 {
287 //base.OnPaintContent(e);
288 if (!Visible)
289 {
290 return;
291 }
292
293 Graphics g = e.Graphics;
294 g.SmoothingMode = SmoothingMode.AntiAlias;
295 g.TextRenderingHint = TextRenderingHint.SystemDefault;
296
297 Size size = g.MeasureString(PercentMsg, this.Font).ToSize();
298
299 PointF txtLocation = new PointF(
300 (float)((Width - size.Width) / 2),
301 (float)((Height - size.Height) / 2));
302
303 g.DrawString(PercentMsg, this.Font, new SolidBrush(ForeColor), txtLocation);
304
305 }
306 }
307}
308
309
下面是进度条的基类代码:
其中涉及到的几个类的代码:
Code
1// 功能:基于Control的控件基类
2// 描述:
3// 编码:温伟鹏
4// 日期:12/16/2008 08:34:42
5
6using System;
7using System.Collections.Generic;
8using System.Text;
9using System.Windows.Forms;
10using System.ComponentModel;
11using System.Drawing;
12using System.Drawing.Drawing2D;
13using System.Runtime.InteropServices;
14using System.Threading;
15
16namespace Esint.UI.WinFormUI
17{
18 [DefaultEvent("Click")]
19 [ComVisible(true)]
20 [DefaultProperty("Text")]
21 public class ControlBase:Control
22 {
23 private ControlBorder border;
24 private BackgroundBase background;
25 private ControlShadow shadow;
26 private bool showShadow;
27 //状态
28 private bool isHovered;
29 private bool isFocused;
30 private bool isFocusedByKey;
31 private bool isKeyDown;
32 private bool isMouseDown;
33 private bool isPressed { get { return isKeyDown || (isMouseDown && isHovered); } }
34 /**//// <summary>
35 /// 控件放置的方向(水平、垂直)
36 /// </summary>
37 private Orientation orientation;
38 /**//// <summary>
39 /// 原有的控件放置方向
40 /// </summary>
41 private Orientation originalOrientation;
42 /**//// <summary>
43 /// 获取或设置控件放置的方向(水平、垂直)
44 /// </summary>
45 [Category("Appearance"), Description("获取或设置控件放置的方向(水平、垂直)")]
46 public Orientation Orientation
47 {
48 get { return this.orientation; }
49 set
50 {
51 this.originalOrientation = this.Orientation;
52 if (value != this.orientation)
53 {
54 TransformOrientation(value);
55 this.orientation = value;
56 }
57 }
58 }
59 /**//// <summary>
60 /// 获取或设置控件的边框样式
61 /// </summary>
62 [Category("Appearance"), Description("获取或设置控件的边框样式")]
63 public virtual ControlBorder Border
64 {
65 get
66 {
67 return this.border;
68 }
69 set
70 {
71 this.border = value;
72 }
73 }
74 /**//// <summary>
75 /// 获取或设置控件背景样式
76 /// </summary>
77 [Category("Appearance"), Description("获取或设置控件背景样式")]
78 public virtual BackgroundBase Background
79 {
80 get { return this.background; }
81 set { this.background = value; }
82 }
83
84 /**//// <summary>
85 /// 获取或设置控件阴影
86 /// </summary>
87 [Category("Appearance"), Description("获取或设置控件阴影")]
88 public virtual ControlShadow Shadow
89 {
90 get { return this.shadow; }
91 set { this.shadow = value; }
92 }
93 /**//// <summary>
94 /// 获取或设置控件是否显示阴影
95 /// </summary>
96 [Category("Appearance"), Description("获取或设置控件是否显示阴影")]
97 public virtual bool ShowShadow
98 {
99 get { return this.showShadow; }
100 set { this.showShadow = value; }
101 }
102 /**//// <summary>
103 /// 获取控件当前是否存在焦点
104 /// </summary>
105 [Browsable(false)]
106 public new bool Focused
107 {
108 get { return this.isFocused; }
109 }
110 /**//// <summary>
111 /// 获取控件当前是否被鼠标悬停
112 /// </summary>
113 [Browsable(false)]
114 public bool Hovered
115 {
116 get { return this.isHovered; }
117 }
118 /**//// <summary>
119 /// 获取控件是否是由键盘键得到焦点
120 /// </summary>
121 [Browsable(false)]
122 public bool FocusedByKey
123 {
124 get { return this.isFocusedByKey; }
125 }
126 /**//// <summary>
127 /// 转换标尺
128 /// </summary>
129 /// <param name="ori"></param>
130 protected void TransformOrientation(Orientation ori)
131 {
132 int width = Width;
133 int height = Height;
134
135 if (Orientation != ori)
136 {
137 Height = width;
138 Width = height;
139 }
140 }
141
142 public ControlBase()
143 {
144 InitializeStyles();
145 InitializeComponents();
146 }
147 /**//// <summary>
148 /// 初始化控件样式和行为
149 /// </summary>
150 protected virtual void InitializeStyles()
151 {
152 this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
153 this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
154 this.SetStyle(ControlStyles.UserPaint, true);
155 this.SetStyle(ControlStyles.ResizeRedraw, true);
156 this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
157 this.SetStyle(ControlStyles.Opaque, false);
158 }
159 /**//// <summary>
160 /// 初始化组件
161 /// </summary>
162 protected virtual void InitializeComponents()
163 {
164 //边框
165 InitializeBorder();
166 //背景
167 InitializeBackground();
168 //阴影
169 InitializeShadow();
170 //自身
171 InitializeSelf();
172 }
173 /**//// <summary>
174 /// 初始化边框
175 /// </summary>
176 protected virtual void InitializeBorder()
177 {
178 this.border = new ControlBorder(1, Color.Black, BorderCornerMode.Square, 0);
179 this.border.Margin = new Margin();
180 this.border.OnBorderColorChanged += new BorderColorChanged(ControlNeedRefresh);
181 this.border.OnBorderCornerModeChanged += new BorderCornerModeChanged(ControlNeedRefresh);
182 this.border.OnBorderCornerRoundChanged += new BorderCornerRoundChanged(ControlNeedRefresh);
183 this.border.OnBorderMarginChanged += new BorderMarginChanged(ControlNeedRefresh);
184 this.border.OnBorderWidthChanged += new BorderWidthChanged(ControlNeedRefresh);
185 }
186 /**//// <summary>
187 /// 初始化背景
188 /// </summary>
189 protected virtual void InitializeBackground()
190 {
191 this.background = new BackgroundBase();
192 //定义参与渐变的色彩
193 Color[] colors =
194 {
195 Color.White,
196 Color.White,
197 Color.FromArgb(228,232,238),
198 Color.FromArgb(202,212,222),
199 Color.FromArgb(228,232,238),
200 Color.White,
201 Color.White
202 };
203 float[] positions =
204 {
205 0.0f,
206 0.1f,
207 0.36f,
208 0.5f,
209 0.64f,
210 0.9f,
211 1.0f
212 };
213 this.background.ColorBlend.Colors = colors;
214 this.background.ColorBlend.Positions = positions;
215 this.background.UseGradientColor = true;
216
217 this.background.OnColorBlendChanged += new BackgroundBase.ColorBlendChanged(ControlNeedRefresh);
218 }
219 /**//// <summary>
220 /// 初始化阴影
221 /// </summary>
222 protected virtual void InitializeShadow()
223 {
224 this.shadow = new ControlShadow();
225 this.shadow.OnShadowColorChanged += new ShadowColorChanged(ControlNeedRefresh);
226 this.shadow.OnShadowThicknessChanged += new ShadowThicknessChanged(ControlNeedRefresh);
227 this.showShadow = true;
228 }
229 /**//// <summary>
230 /// 初始化自身
231 /// </summary>
232 protected virtual void InitializeSelf()
233 {
234 this.Resize += new EventHandler(ControlNeedRefresh);
235 this.Size = new Size(200, 40);
236 this.Orientation = Orientation.Horizontal;
237 }
238 /**//// <summary>
239 /// 属性被修改,要重绘控件
240 /// </summary>
241 /// <param name="sender"></param>
242 /// <param name="e"></param>
243 protected virtual void ControlNeedRefresh(object sender, EventArgs e)
244 {
245 this.Refresh();
246 }
247
248 覆写鼠标和键盘事件,控制控件状态(点击、焦点、悬浮、失去焦点)#region 覆写鼠标和键盘事件,控制控件状态(点击、焦点、悬浮、失去焦点)
249 /**//// <summary>
250 /// 覆写Click
251 /// </summary>
252 /// <param name="e"></param>
253 protected override void OnClick(EventArgs e)
254 {
255 isKeyDown = isMouseDown = false;
256
257 base.OnClick(e);
258 }
259 protected override void OnEnter(EventArgs e)
260 {
261 isFocused = isFocusedByKey = true;
262
263 base.OnEnter(e);
264 }
265 protected override void OnLeave(EventArgs e)
266 {
267 base.OnLeave(e);
268 isFocused = isFocusedByKey = isKeyDown = isMouseDown = false;
269 Invalidate();
270 }
271 protected override void OnKeyDown(KeyEventArgs e)
272 {
273 if (e.KeyCode == Keys.Space)
274 {
275 isKeyDown = true;
276 Invalidate();
277 }
278 base.OnKeyDown(e);
279 }
280 protected override void OnKeyUp(KeyEventArgs e)
281 {
282 if (isKeyDown && e.KeyCode == Keys.Space)
283 {
284 isKeyDown = false;
285 Invalidate();
286 }
287 base.OnKeyUp(e);
288 }
289 protected override void OnMouseDown(MouseEventArgs e)
290 {
291 if (!isMouseDown && e.Button == MouseButtons.Left)
292 {
293 isMouseDown = true;
294 isFocusedByKey = false;
295 Invalidate();
296 }
297 base.OnMouseDown(e);
298 }
299 protected override void OnMouseUp(MouseEventArgs e)
300 {
301 if (isMouseDown)
302 {
303 isMouseDown = false;
304 Invalidate();
305 }
306 base.OnMouseUp(e);
307 }
308 protected override void OnMouseMove(MouseEventArgs e)
309 {
310 base.OnMouseMove(e);
311
312 if (e.Button != MouseButtons.None)
313 {
314 if (!ClientRectangle.Contains(e.X, e.Y))
315 {
316 if (isHovered)
317 {
318 isHovered = false;
319 Invalidate();
320 }
321 }
322 else if (!isHovered)
323 {
324 isHovered = true;
325 Invalidate();
326 }
327 }
328 }
329 protected override void OnMouseEnter(EventArgs e)
330 {
331 isHovered = true;
332 Invalidate();
333 base.OnMouseEnter(e);
334 }
335 protected override void OnMouseLeave(EventArgs e)
336 {
337 isHovered = false;
338 Invalidate();
339 base.OnMouseLeave(e);
340 }
341 #endregion
342
343 /**//// <summary>
344 /// 绘制背景
345 /// </summary>
346 /// <param name="pevent"></param>
347 protected override void OnPaintBackground(PaintEventArgs e)
348 {
349 base.OnPaintBackground(e);
350 //如果不可见,则不绘制
351 if (!Visible)
352 {
353 return;
354 }
355 //是否使用渐变色
356 if (background.UseGradientColor)
357 {
358
359 Graphics g = e.Graphics;
360 g.SmoothingMode = SmoothingMode.AntiAlias;
361
362 int height = Height;
363 int width = Width;
364
365 if (this.Orientation == Orientation.Vertical
366 && this.Orientation != this.originalOrientation)
367 {
368 g.RotateTransform(90);
369 g.TranslateTransform(0, -width + 1);
370 height = Width;
371 width = Height;
372 }
373
374 LinearGradientBrush lgb = new LinearGradientBrush(
375 new Point(0, 0),
376 new Point(0, height),
377 Color.Black,
378 Color.White
379 );
380
381 lgb.InterpolationColors = background.ColorBlend;
382
383 g.FillRectangle(lgb, 0, 0, width, height);
384
385 if (lgb != null) { lgb.Dispose(); }
386 }
387 }
388 /**//// <summary>
389 /// 绘制
390 /// </summary>
391 /// <param name="e"></param>
392 protected override void OnPaint(PaintEventArgs e)
393 {
394 //base.OnPaint(e);
395
396 //如果不可见,则不绘制
397 if (!Visible)
398 {
399 return;
400 }
401
402 OnPaintBorder(e);
403
404 OnPaintContent(e);
405 }
406 /**//// <summary>
407 /// 绘制边框
408 /// </summary>
409 /// <param name="e"></param>
410 protected virtual void OnPaintBorder(PaintEventArgs e)
411 {
412 Graphics g = e.Graphics;
413 g.SmoothingMode = SmoothingMode.AntiAlias;
414
415 Pen borderPen = new Pen(Border.BorderColor, Border.BorderWidth);
416 Rectangle borderRect = new Rectangle(0, 0, Width - 1, Height - 1);
417 GraphicsPath roundRect = null;
418 //如果是方形拐角,则绘制矩形
419 if (Border.CornerMode == BorderCornerMode.Square)
420 {
421 g.DrawRectangle(borderPen, borderRect);
422 }
423 else
424 {
425 //如果是圆角,则绘制圆角矩形
426 roundRect = DrawHelper.CreateRoundRectangle(borderRect, Border.CornerRound);
427 g.DrawPath(borderPen, roundRect);
428 }
429
430 if (borderPen != null) { borderPen.Dispose(); }
431 if (roundRect != null) { roundRect.Dispose(); }
432 }
433 /**//// <summary>
434 /// 绘制内容
435 /// </summary>
436 /// <param name="e"></param>
437 protected virtual void OnPaintContent(PaintEventArgs e)
438 {
439 }
440 /**//// <summary>
441 /// 发送消息
442 /// </summary>
443 /// <param name="msg"></param>
444 /// <param name="wparam"></param>
445 /// <param name="lparam"></param>
446 /// <returns></returns>
447 internal IntPtr SendMessage(int msg, int wparam, int lparam)
448 {
449 return Win32.SendMessage(new HandleRef(this, this.Handle), msg, wparam, lparam);
450 }
451 }
452}
453
454
1// 功能:基于Control的控件基类
2// 描述:
3// 编码:温伟鹏
4// 日期:12/16/2008 08:34:42
5
6using System;
7using System.Collections.Generic;
8using System.Text;
9using System.Windows.Forms;
10using System.ComponentModel;
11using System.Drawing;
12using System.Drawing.Drawing2D;
13using System.Runtime.InteropServices;
14using System.Threading;
15
16namespace Esint.UI.WinFormUI
17{
18 [DefaultEvent("Click")]
19 [ComVisible(true)]
20 [DefaultProperty("Text")]
21 public class ControlBase:Control
22 {
23 private ControlBorder border;
24 private BackgroundBase background;
25 private ControlShadow shadow;
26 private bool showShadow;
27 //状态
28 private bool isHovered;
29 private bool isFocused;
30 private bool isFocusedByKey;
31 private bool isKeyDown;
32 private bool isMouseDown;
33 private bool isPressed { get { return isKeyDown || (isMouseDown && isHovered); } }
34 /**//// <summary>
35 /// 控件放置的方向(水平、垂直)
36 /// </summary>
37 private Orientation orientation;
38 /**//// <summary>
39 /// 原有的控件放置方向
40 /// </summary>
41 private Orientation originalOrientation;
42 /**//// <summary>
43 /// 获取或设置控件放置的方向(水平、垂直)
44 /// </summary>
45 [Category("Appearance"), Description("获取或设置控件放置的方向(水平、垂直)")]
46 public Orientation Orientation
47 {
48 get { return this.orientation; }
49 set
50 {
51 this.originalOrientation = this.Orientation;
52 if (value != this.orientation)
53 {
54 TransformOrientation(value);
55 this.orientation = value;
56 }
57 }
58 }
59 /**//// <summary>
60 /// 获取或设置控件的边框样式
61 /// </summary>
62 [Category("Appearance"), Description("获取或设置控件的边框样式")]
63 public virtual ControlBorder Border
64 {
65 get
66 {
67 return this.border;
68 }
69 set
70 {
71 this.border = value;
72 }
73 }
74 /**//// <summary>
75 /// 获取或设置控件背景样式
76 /// </summary>
77 [Category("Appearance"), Description("获取或设置控件背景样式")]
78 public virtual BackgroundBase Background
79 {
80 get { return this.background; }
81 set { this.background = value; }
82 }
83
84 /**//// <summary>
85 /// 获取或设置控件阴影
86 /// </summary>
87 [Category("Appearance"), Description("获取或设置控件阴影")]
88 public virtual ControlShadow Shadow
89 {
90 get { return this.shadow; }
91 set { this.shadow = value; }
92 }
93 /**//// <summary>
94 /// 获取或设置控件是否显示阴影
95 /// </summary>
96 [Category("Appearance"), Description("获取或设置控件是否显示阴影")]
97 public virtual bool ShowShadow
98 {
99 get { return this.showShadow; }
100 set { this.showShadow = value; }
101 }
102 /**//// <summary>
103 /// 获取控件当前是否存在焦点
104 /// </summary>
105 [Browsable(false)]
106 public new bool Focused
107 {
108 get { return this.isFocused; }
109 }
110 /**//// <summary>
111 /// 获取控件当前是否被鼠标悬停
112 /// </summary>
113 [Browsable(false)]
114 public bool Hovered
115 {
116 get { return this.isHovered; }
117 }
118 /**//// <summary>
119 /// 获取控件是否是由键盘键得到焦点
120 /// </summary>
121 [Browsable(false)]
122 public bool FocusedByKey
123 {
124 get { return this.isFocusedByKey; }
125 }
126 /**//// <summary>
127 /// 转换标尺
128 /// </summary>
129 /// <param name="ori"></param>
130 protected void TransformOrientation(Orientation ori)
131 {
132 int width = Width;
133 int height = Height;
134
135 if (Orientation != ori)
136 {
137 Height = width;
138 Width = height;
139 }
140 }
141
142 public ControlBase()
143 {
144 InitializeStyles();
145 InitializeComponents();
146 }
147 /**//// <summary>
148 /// 初始化控件样式和行为
149 /// </summary>
150 protected virtual void InitializeStyles()
151 {
152 this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
153 this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
154 this.SetStyle(ControlStyles.UserPaint, true);
155 this.SetStyle(ControlStyles.ResizeRedraw, true);
156 this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
157 this.SetStyle(ControlStyles.Opaque, false);
158 }
159 /**//// <summary>
160 /// 初始化组件
161 /// </summary>
162 protected virtual void InitializeComponents()
163 {
164 //边框
165 InitializeBorder();
166 //背景
167 InitializeBackground();
168 //阴影
169 InitializeShadow();
170 //自身
171 InitializeSelf();
172 }
173 /**//// <summary>
174 /// 初始化边框
175 /// </summary>
176 protected virtual void InitializeBorder()
177 {
178 this.border = new ControlBorder(1, Color.Black, BorderCornerMode.Square, 0);
179 this.border.Margin = new Margin();
180 this.border.OnBorderColorChanged += new BorderColorChanged(ControlNeedRefresh);
181 this.border.OnBorderCornerModeChanged += new BorderCornerModeChanged(ControlNeedRefresh);
182 this.border.OnBorderCornerRoundChanged += new BorderCornerRoundChanged(ControlNeedRefresh);
183 this.border.OnBorderMarginChanged += new BorderMarginChanged(ControlNeedRefresh);
184 this.border.OnBorderWidthChanged += new BorderWidthChanged(ControlNeedRefresh);
185 }
186 /**//// <summary>
187 /// 初始化背景
188 /// </summary>
189 protected virtual void InitializeBackground()
190 {
191 this.background = new BackgroundBase();
192 //定义参与渐变的色彩
193 Color[] colors =
194 {
195 Color.White,
196 Color.White,
197 Color.FromArgb(228,232,238),
198 Color.FromArgb(202,212,222),
199 Color.FromArgb(228,232,238),
200 Color.White,
201 Color.White
202 };
203 float[] positions =
204 {
205 0.0f,
206 0.1f,
207 0.36f,
208 0.5f,
209 0.64f,
210 0.9f,
211 1.0f
212 };
213 this.background.ColorBlend.Colors = colors;
214 this.background.ColorBlend.Positions = positions;
215 this.background.UseGradientColor = true;
216
217 this.background.OnColorBlendChanged += new BackgroundBase.ColorBlendChanged(ControlNeedRefresh);
218 }
219 /**//// <summary>
220 /// 初始化阴影
221 /// </summary>
222 protected virtual void InitializeShadow()
223 {
224 this.shadow = new ControlShadow();
225 this.shadow.OnShadowColorChanged += new ShadowColorChanged(ControlNeedRefresh);
226 this.shadow.OnShadowThicknessChanged += new ShadowThicknessChanged(ControlNeedRefresh);
227 this.showShadow = true;
228 }
229 /**//// <summary>
230 /// 初始化自身
231 /// </summary>
232 protected virtual void InitializeSelf()
233 {
234 this.Resize += new EventHandler(ControlNeedRefresh);
235 this.Size = new Size(200, 40);
236 this.Orientation = Orientation.Horizontal;
237 }
238 /**//// <summary>
239 /// 属性被修改,要重绘控件
240 /// </summary>
241 /// <param name="sender"></param>
242 /// <param name="e"></param>
243 protected virtual void ControlNeedRefresh(object sender, EventArgs e)
244 {
245 this.Refresh();
246 }
247
248 覆写鼠标和键盘事件,控制控件状态(点击、焦点、悬浮、失去焦点)#region 覆写鼠标和键盘事件,控制控件状态(点击、焦点、悬浮、失去焦点)
249 /**//// <summary>
250 /// 覆写Click
251 /// </summary>
252 /// <param name="e"></param>
253 protected override void OnClick(EventArgs e)
254 {
255 isKeyDown = isMouseDown = false;
256
257 base.OnClick(e);
258 }
259 protected override void OnEnter(EventArgs e)
260 {
261 isFocused = isFocusedByKey = true;
262
263 base.OnEnter(e);
264 }
265 protected override void OnLeave(EventArgs e)
266 {
267 base.OnLeave(e);
268 isFocused = isFocusedByKey = isKeyDown = isMouseDown = false;
269 Invalidate();
270 }
271 protected override void OnKeyDown(KeyEventArgs e)
272 {
273 if (e.KeyCode == Keys.Space)
274 {
275 isKeyDown = true;
276 Invalidate();
277 }
278 base.OnKeyDown(e);
279 }
280 protected override void OnKeyUp(KeyEventArgs e)
281 {
282 if (isKeyDown && e.KeyCode == Keys.Space)
283 {
284 isKeyDown = false;
285 Invalidate();
286 }
287 base.OnKeyUp(e);
288 }
289 protected override void OnMouseDown(MouseEventArgs e)
290 {
291 if (!isMouseDown && e.Button == MouseButtons.Left)
292 {
293 isMouseDown = true;
294 isFocusedByKey = false;
295 Invalidate();
296 }
297 base.OnMouseDown(e);
298 }
299 protected override void OnMouseUp(MouseEventArgs e)
300 {
301 if (isMouseDown)
302 {
303 isMouseDown = false;
304 Invalidate();
305 }
306 base.OnMouseUp(e);
307 }
308 protected override void OnMouseMove(MouseEventArgs e)
309 {
310 base.OnMouseMove(e);
311
312 if (e.Button != MouseButtons.None)
313 {
314 if (!ClientRectangle.Contains(e.X, e.Y))
315 {
316 if (isHovered)
317 {
318 isHovered = false;
319 Invalidate();
320 }
321 }
322 else if (!isHovered)
323 {
324 isHovered = true;
325 Invalidate();
326 }
327 }
328 }
329 protected override void OnMouseEnter(EventArgs e)
330 {
331 isHovered = true;
332 Invalidate();
333 base.OnMouseEnter(e);
334 }
335 protected override void OnMouseLeave(EventArgs e)
336 {
337 isHovered = false;
338 Invalidate();
339 base.OnMouseLeave(e);
340 }
341 #endregion
342
343 /**//// <summary>
344 /// 绘制背景
345 /// </summary>
346 /// <param name="pevent"></param>
347 protected override void OnPaintBackground(PaintEventArgs e)
348 {
349 base.OnPaintBackground(e);
350 //如果不可见,则不绘制
351 if (!Visible)
352 {
353 return;
354 }
355 //是否使用渐变色
356 if (background.UseGradientColor)
357 {
358
359 Graphics g = e.Graphics;
360 g.SmoothingMode = SmoothingMode.AntiAlias;
361
362 int height = Height;
363 int width = Width;
364
365 if (this.Orientation == Orientation.Vertical
366 && this.Orientation != this.originalOrientation)
367 {
368 g.RotateTransform(90);
369 g.TranslateTransform(0, -width + 1);
370 height = Width;
371 width = Height;
372 }
373
374 LinearGradientBrush lgb = new LinearGradientBrush(
375 new Point(0, 0),
376 new Point(0, height),
377 Color.Black,
378 Color.White
379 );
380
381 lgb.InterpolationColors = background.ColorBlend;
382
383 g.FillRectangle(lgb, 0, 0, width, height);
384
385 if (lgb != null) { lgb.Dispose(); }
386 }
387 }
388 /**//// <summary>
389 /// 绘制
390 /// </summary>
391 /// <param name="e"></param>
392 protected override void OnPaint(PaintEventArgs e)
393 {
394 //base.OnPaint(e);
395
396 //如果不可见,则不绘制
397 if (!Visible)
398 {
399 return;
400 }
401
402 OnPaintBorder(e);
403
404 OnPaintContent(e);
405 }
406 /**//// <summary>
407 /// 绘制边框
408 /// </summary>
409 /// <param name="e"></param>
410 protected virtual void OnPaintBorder(PaintEventArgs e)
411 {
412 Graphics g = e.Graphics;
413 g.SmoothingMode = SmoothingMode.AntiAlias;
414
415 Pen borderPen = new Pen(Border.BorderColor, Border.BorderWidth);
416 Rectangle borderRect = new Rectangle(0, 0, Width - 1, Height - 1);
417 GraphicsPath roundRect = null;
418 //如果是方形拐角,则绘制矩形
419 if (Border.CornerMode == BorderCornerMode.Square)
420 {
421 g.DrawRectangle(borderPen, borderRect);
422 }
423 else
424 {
425 //如果是圆角,则绘制圆角矩形
426 roundRect = DrawHelper.CreateRoundRectangle(borderRect, Border.CornerRound);
427 g.DrawPath(borderPen, roundRect);
428 }
429
430 if (borderPen != null) { borderPen.Dispose(); }
431 if (roundRect != null) { roundRect.Dispose(); }
432 }
433 /**//// <summary>
434 /// 绘制内容
435 /// </summary>
436 /// <param name="e"></param>
437 protected virtual void OnPaintContent(PaintEventArgs e)
438 {
439 }
440 /**//// <summary>
441 /// 发送消息
442 /// </summary>
443 /// <param name="msg"></param>
444 /// <param name="wparam"></param>
445 /// <param name="lparam"></param>
446 /// <returns></returns>
447 internal IntPtr SendMessage(int msg, int wparam, int lparam)
448 {
449 return Win32.SendMessage(new HandleRef(this, this.Handle), msg, wparam, lparam);
450 }
451 }
452}
453
454
Code
1// 功能:背景基类
2// 描述:
3// 编码:温伟鹏
4// 日期:12/15/2008 16:53:21
5
6using System;
7using System.Collections.Generic;
8using System.Text;
9using System.Drawing.Drawing2D;
10using System.Drawing;
11using System.ComponentModel;
12
13namespace Esint.UI.WinFormUI
14{
15 /**//// <summary>
16 /// 背景类的基类
17 /// </summary>
18 [TypeConverter(typeof(BackgroundBaseConverter))]
19 public class BackgroundBase
20 {
21 public delegate void ColorBlendChanged(object sender, EventArgs e);
22
23 public event ColorBlendChanged OnColorBlendChanged;
24
25 private ColorBlend blend;
26 /**//// <summary>
27 /// 是否使用渐变色
28 /// </summary>
29 private bool useGradientColor;
30
31 public BackgroundBase()
32 {
33 this.blend = new ColorBlend();
34 Color[] colors = new Color[] { Color.White, Color.FromName("Desktop") };
35 float[] positions = new float[] { 0.0f, 1.0f };
36 this.blend.Colors = colors;
37 this.blend.Positions = positions;
38 this.useGradientColor = true;
39 }
40
41 public BackgroundBase(ColorBlend blend)
42 {
43 this.blend = blend;
44 }
45
46 public BackgroundBase(Color[] colors, float[] positions)
47 :this(colors,positions,true)
48 {
49
50 }
51
52 public BackgroundBase(Color[] colors, float[] positions, bool useGradientColor)
53 {
54 this.blend = new ColorBlend();
55 this.blend.Colors = colors;
56 this.blend.Positions = positions;
57 this.useGradientColor = useGradientColor;
58 }
59 /**//// <summary>
60 /// 多色渐变中以内插值取代颜色混合的颜色和位置数组
61 /// </summary>
62 [Browsable(false)]
63 public virtual ColorBlend ColorBlend
64 {
65 get { return this.blend; }
66 set {
67 if (value != this.blend)
68 {
69 this.blend = value;
70
71 if (OnColorBlendChanged != null)
72 OnColorBlendChanged(this, null);
73 }
74 }
75 }
76
77 /**//// <summary>
78 /// 获取或设置是否能够应用渐变色
79 /// </summary>
80 public virtual bool UseGradientColor
81 {
82 get
83 {
84 return this.useGradientColor;
85 }
86 set
87 {
88 this.useGradientColor = value;
89 }
90 }
91 }
92}
93
1// 功能:背景基类
2// 描述:
3// 编码:温伟鹏
4// 日期:12/15/2008 16:53:21
5
6using System;
7using System.Collections.Generic;
8using System.Text;
9using System.Drawing.Drawing2D;
10using System.Drawing;
11using System.ComponentModel;
12
13namespace Esint.UI.WinFormUI
14{
15 /**//// <summary>
16 /// 背景类的基类
17 /// </summary>
18 [TypeConverter(typeof(BackgroundBaseConverter))]
19 public class BackgroundBase
20 {
21 public delegate void ColorBlendChanged(object sender, EventArgs e);
22
23 public event ColorBlendChanged OnColorBlendChanged;
24
25 private ColorBlend blend;
26 /**//// <summary>
27 /// 是否使用渐变色
28 /// </summary>
29 private bool useGradientColor;
30
31 public BackgroundBase()
32 {
33 this.blend = new ColorBlend();
34 Color[] colors = new Color[] { Color.White, Color.FromName("Desktop") };
35 float[] positions = new float[] { 0.0f, 1.0f };
36 this.blend.Colors = colors;
37 this.blend.Positions = positions;
38 this.useGradientColor = true;
39 }
40
41 public BackgroundBase(ColorBlend blend)
42 {
43 this.blend = blend;
44 }
45
46 public BackgroundBase(Color[] colors, float[] positions)
47 :this(colors,positions,true)
48 {
49
50 }
51
52 public BackgroundBase(Color[] colors, float[] positions, bool useGradientColor)
53 {
54 this.blend = new ColorBlend();
55 this.blend.Colors = colors;
56 this.blend.Positions = positions;
57 this.useGradientColor = useGradientColor;
58 }
59 /**//// <summary>
60 /// 多色渐变中以内插值取代颜色混合的颜色和位置数组
61 /// </summary>
62 [Browsable(false)]
63 public virtual ColorBlend ColorBlend
64 {
65 get { return this.blend; }
66 set {
67 if (value != this.blend)
68 {
69 this.blend = value;
70
71 if (OnColorBlendChanged != null)
72 OnColorBlendChanged(this, null);
73 }
74 }
75 }
76
77 /**//// <summary>
78 /// 获取或设置是否能够应用渐变色
79 /// </summary>
80 public virtual bool UseGradientColor
81 {
82 get
83 {
84 return this.useGradientColor;
85 }
86 set
87 {
88 this.useGradientColor = value;
89 }
90 }
91 }
92}
93
Code
1// 功能:控件边框
2// 描述:
3// 编码:温伟鹏
4// 日期:12/12/2008 08:52:39
5
6using System;
7using System.Collections.Generic;
8using System.Text;
9using System.Drawing;
10using System.ComponentModel;
11
12namespace Esint.UI.WinFormUI
13{
14 public delegate void BorderWidthChanged(object sender,EventArgs e);
15 public delegate void BorderColorChanged(object sender,EventArgs e);
16 public delegate void BorderCornerModeChanged(object sender,EventArgs e);
17 public delegate void BorderCornerRoundChanged(object sender,EventArgs e);
18 public delegate void BorderMarginChanged(object sender,EventArgs e);
19 /**//// <summary>
20 /// 控件边框
21 /// </summary>
22 [TypeConverter(typeof(ControlBorderConverter))]
23 public class ControlBorder
24 {
25 Events#region Events
26 public event BorderWidthChanged OnBorderWidthChanged;
27 public event BorderColorChanged OnBorderColorChanged;
28 public event BorderCornerModeChanged OnBorderCornerModeChanged;
29 public event BorderCornerRoundChanged OnBorderCornerRoundChanged;
30 public event BorderMarginChanged OnBorderMarginChanged;
31 #endregion
32
33 private float borderWidth;
34 private Color borderColor;
35 //
36 private BorderCornerMode cornerMode;
37 //默认拐角的大小,如果是圆角,则圆角的宽度和高度都是此值
38 private int cornerRound = 10;
39 /**//// <summary>
40 /// 自绘边框距控件边界的间隔
41 /// </summary>
42 private Margin borderMargin;
43
44 public ControlBorder()
45 :this(1)
46 {
47 }
48
49 public ControlBorder(float bWidth)
50 :this(bWidth,Color.Black)
51 {
52 }
53
54 public ControlBorder(float bWidth, Color bColor)
55 :this(bWidth,bColor, BorderCornerMode.Round,10)
56 {
57 }
58
59 public ControlBorder(float bWidth, Color bColor, BorderCornerMode cMode, int cRound)
60 :this(bWidth,bColor,cMode,cRound,new Margin())
61 {
62 }
63 public ControlBorder(float bWidth, Color bColor, BorderCornerMode cMode, int cRound, Margin margin)
64 {
65 this.borderWidth = bWidth;
66 this.borderColor = bColor;
67 this.cornerMode = cMode;
68 this.cornerRound = cRound;
69 this.borderMargin = new Margin();
70 this.borderMargin.OnTopChanged += new Margin.TopChanged(MarginChanged);
71 this.borderMargin.OnLeftChanged += new Margin.LeftChanged(MarginChanged);
72 this.borderMargin.OnBottomChanged += new Margin.BottomChanged(MarginChanged);
73 this.borderMargin.OnRightChanged += new Margin.RightChanged(MarginChanged);
74 this.borderMargin = margin;
75 }
76 /**//// <summary>
77 /// 边框距边界的间隔修改
78 /// </summary>
79 /// <param name="sender"></param>
80 /// <param name="e"></param>
81 void MarginChanged(object sender, EventArgs e)
82 {
83 if (OnBorderMarginChanged != null)
84 OnBorderMarginChanged(this, e);
85 }
86 /**//// <summary>
87 /// 边框画笔宽度
88 /// </summary>
89 [Description("获取或设置边框画笔宽度")]
90 public float BorderWidth
91 {
92 get { return this.borderWidth; }
93 set {
94 this.borderWidth = value;
95 if (OnBorderWidthChanged != null)
96 OnBorderWidthChanged(this, null);
97 }
98 }
99 /**//// <summary>
100 /// 边框颜色
101 /// </summary>
102 [Description("获取或设置边框颜色")]
103 public Color BorderColor
104 {
105 get { return this.borderColor; }
106 set {
107 this.borderColor = value;
108 if (OnBorderColorChanged != null)
109 OnBorderColorChanged(this, null);
110 }
111 }
112 /**//// <summary>
113 /// 边框Corner的形状(暂时不支持直角,以后再改)
114 /// </summary>
115 [Browsable(false)]
116 [Description("获取或设置边框Corner的形状")]
117 public BorderCornerMode CornerMode
118 {
119 get { return this.cornerMode; }
120 set {
121 this.cornerMode = value;
122 if (OnBorderCornerModeChanged != null)
123 OnBorderCornerModeChanged(this, null);
124 }
125 }
126 /**//// <summary>
127 /// 边框Corner为圆角时的大小
128 /// </summary>
129 [Description("获取或设置边框Corner为圆角时的大小")]
130 public int CornerRound
131 {
132 get { return this.cornerRound; }
133 set {
134 this.cornerRound = value;
135 if (CornerMode != BorderCornerMode.Square)
136 {
137 if (OnBorderCornerRoundChanged != null)
138 OnBorderCornerRoundChanged(this, null);
139 }
140 }
141 }
142 /**//// <summary>
143 /// 获取或设置边框距控件边界的间隔
144 /// </summary>
145 [Description("获取或设置边框距控件边界的间隔")]
146 public Margin Margin
147 {
148 get { return this.borderMargin; }
149 set { this.borderMargin = value; }
150 }
151 }
152 /**//// <summary>
153 /// 边框拐角的类型
154 /// </summary>
155 public enum BorderCornerMode
156 {
157 /**//// <summary>
158 /// 直角
159 /// </summary>
160 Square = 1,
161 /**//// <summary>
162 /// 圆角
163 /// </summary>
164 Round = 2,
165 }
166}
167
168
1// 功能:控件边框
2// 描述:
3// 编码:温伟鹏
4// 日期:12/12/2008 08:52:39
5
6using System;
7using System.Collections.Generic;
8using System.Text;
9using System.Drawing;
10using System.ComponentModel;
11
12namespace Esint.UI.WinFormUI
13{
14 public delegate void BorderWidthChanged(object sender,EventArgs e);
15 public delegate void BorderColorChanged(object sender,EventArgs e);
16 public delegate void BorderCornerModeChanged(object sender,EventArgs e);
17 public delegate void BorderCornerRoundChanged(object sender,EventArgs e);
18 public delegate void BorderMarginChanged(object sender,EventArgs e);
19 /**//// <summary>
20 /// 控件边框
21 /// </summary>
22 [TypeConverter(typeof(ControlBorderConverter))]
23 public class ControlBorder
24 {
25 Events#region Events
26 public event BorderWidthChanged OnBorderWidthChanged;
27 public event BorderColorChanged OnBorderColorChanged;
28 public event BorderCornerModeChanged OnBorderCornerModeChanged;
29 public event BorderCornerRoundChanged OnBorderCornerRoundChanged;
30 public event BorderMarginChanged OnBorderMarginChanged;
31 #endregion
32
33 private float borderWidth;
34 private Color borderColor;
35 //
36 private BorderCornerMode cornerMode;
37 //默认拐角的大小,如果是圆角,则圆角的宽度和高度都是此值
38 private int cornerRound = 10;
39 /**//// <summary>
40 /// 自绘边框距控件边界的间隔
41 /// </summary>
42 private Margin borderMargin;
43
44 public ControlBorder()
45 :this(1)
46 {
47 }
48
49 public ControlBorder(float bWidth)
50 :this(bWidth,Color.Black)
51 {
52 }
53
54 public ControlBorder(float bWidth, Color bColor)
55 :this(bWidth,bColor, BorderCornerMode.Round,10)
56 {
57 }
58
59 public ControlBorder(float bWidth, Color bColor, BorderCornerMode cMode, int cRound)
60 :this(bWidth,bColor,cMode,cRound,new Margin())
61 {
62 }
63 public ControlBorder(float bWidth, Color bColor, BorderCornerMode cMode, int cRound, Margin margin)
64 {
65 this.borderWidth = bWidth;
66 this.borderColor = bColor;
67 this.cornerMode = cMode;
68 this.cornerRound = cRound;
69 this.borderMargin = new Margin();
70 this.borderMargin.OnTopChanged += new Margin.TopChanged(MarginChanged);
71 this.borderMargin.OnLeftChanged += new Margin.LeftChanged(MarginChanged);
72 this.borderMargin.OnBottomChanged += new Margin.BottomChanged(MarginChanged);
73 this.borderMargin.OnRightChanged += new Margin.RightChanged(MarginChanged);
74 this.borderMargin = margin;
75 }
76 /**//// <summary>
77 /// 边框距边界的间隔修改
78 /// </summary>
79 /// <param name="sender"></param>
80 /// <param name="e"></param>
81 void MarginChanged(object sender, EventArgs e)
82 {
83 if (OnBorderMarginChanged != null)
84 OnBorderMarginChanged(this, e);
85 }
86 /**//// <summary>
87 /// 边框画笔宽度
88 /// </summary>
89 [Description("获取或设置边框画笔宽度")]
90 public float BorderWidth
91 {
92 get { return this.borderWidth; }
93 set {
94 this.borderWidth = value;
95 if (OnBorderWidthChanged != null)
96 OnBorderWidthChanged(this, null);
97 }
98 }
99 /**//// <summary>
100 /// 边框颜色
101 /// </summary>
102 [Description("获取或设置边框颜色")]
103 public Color BorderColor
104 {
105 get { return this.borderColor; }
106 set {
107 this.borderColor = value;
108 if (OnBorderColorChanged != null)
109 OnBorderColorChanged(this, null);
110 }
111 }
112 /**//// <summary>
113 /// 边框Corner的形状(暂时不支持直角,以后再改)
114 /// </summary>
115 [Browsable(false)]
116 [Description("获取或设置边框Corner的形状")]
117 public BorderCornerMode CornerMode
118 {
119 get { return this.cornerMode; }
120 set {
121 this.cornerMode = value;
122 if (OnBorderCornerModeChanged != null)
123 OnBorderCornerModeChanged(this, null);
124 }
125 }
126 /**//// <summary>
127 /// 边框Corner为圆角时的大小
128 /// </summary>
129 [Description("获取或设置边框Corner为圆角时的大小")]
130 public int CornerRound
131 {
132 get { return this.cornerRound; }
133 set {
134 this.cornerRound = value;
135 if (CornerMode != BorderCornerMode.Square)
136 {
137 if (OnBorderCornerRoundChanged != null)
138 OnBorderCornerRoundChanged(this, null);
139 }
140 }
141 }
142 /**//// <summary>
143 /// 获取或设置边框距控件边界的间隔
144 /// </summary>
145 [Description("获取或设置边框距控件边界的间隔")]
146 public Margin Margin
147 {
148 get { return this.borderMargin; }
149 set { this.borderMargin = value; }
150 }
151 }
152 /**//// <summary>
153 /// 边框拐角的类型
154 /// </summary>
155 public enum BorderCornerMode
156 {
157 /**//// <summary>
158 /// 直角
159 /// </summary>
160 Square = 1,
161 /**//// <summary>
162 /// 圆角
163 /// </summary>
164 Round = 2,
165 }
166}
167
168
Code
1// 功能:自绘控件边框与控件的原有边框的间距
2// 描述:
3// 编码:温伟鹏
4// 日期:12/12/2008 13:14:39
5
6using System;
7using System.Collections.Generic;
8using System.Text;
9using System.ComponentModel;
10
11namespace Esint.UI.WinFormUI
12{
13 /**//// <summary>
14 /// 边框距离控件外框的距离
15 /// </summary>
16 [TypeConverter(typeof(MarginConverter))]
17 public class Margin
18 {
19 Delegate#region Delegate
20 public delegate void TopChanged(object sender, EventArgs e);
21 public delegate void LeftChanged(object sender, EventArgs e);
22 public delegate void BottomChanged(object sender, EventArgs e);
23 public delegate void RightChanged(object sender, EventArgs e);
24 #endregion
25
26 Events#region Events
27 public event TopChanged OnTopChanged;
28 public event LeftChanged OnLeftChanged;
29 public event BottomChanged OnBottomChanged;
30 public event RightChanged OnRightChanged;
31 #endregion
32
33 private int top;
34 private int left;
35 private int bottom;
36 private int right;
37
38 public Margin()
39 : this(0, 0)
40 {
41 }
42
43 public Margin(int top, int left)
44 : this(top, left, top, left)
45 {
46 }
47
48 public Margin(int top, int left, int bottom, int right)
49 {
50 this.top = top;
51 this.left = left;
52 this.bottom = bottom;
53 this.right = right;
54 }
55 /**//// <summary>
56 /// 获取或设置顶部间隔
57 /// </summary>
58 public int Top
59 {
60 get { return this.top; }
61 set {
62 this.top = value;
63 if (OnTopChanged != null)
64 OnTopChanged(this, null);
65 }
66 }
67 /**//// <summary>
68 /// 获取或设置左侧间隔
69 /// </summary>
70 public int Left
71 {
72 get { return this.left; }
73 set {
74 this.left = value;
75 if (OnLeftChanged != null)
76 OnLeftChanged(this, null);
77 }
78 }
79 /**//// <summary>
80 /// 获取或设置底部间隔
81 /// </summary>
82 public int Bottom
83 {
84 get { return this.bottom; }
85 set {
86 this.bottom = value;
87 if (OnBottomChanged != null)
88 OnBottomChanged(this, null);
89 }
90 }
91 /**//// <summary>
92 /// 获取或设置右侧间隔
93 /// </summary>
94 public int Right
95 {
96 get { return this.right; }
97 set {
98 this.right = value;
99 if (OnRightChanged != null)
100 OnRightChanged(this, null);
101 }
102 }
103 }
104}
105
106
1// 功能:自绘控件边框与控件的原有边框的间距
2// 描述:
3// 编码:温伟鹏
4// 日期:12/12/2008 13:14:39
5
6using System;
7using System.Collections.Generic;
8using System.Text;
9using System.ComponentModel;
10
11namespace Esint.UI.WinFormUI
12{
13 /**//// <summary>
14 /// 边框距离控件外框的距离
15 /// </summary>
16 [TypeConverter(typeof(MarginConverter))]
17 public class Margin
18 {
19 Delegate#region Delegate
20 public delegate void TopChanged(object sender, EventArgs e);
21 public delegate void LeftChanged(object sender, EventArgs e);
22 public delegate void BottomChanged(object sender, EventArgs e);
23 public delegate void RightChanged(object sender, EventArgs e);
24 #endregion
25
26 Events#region Events
27 public event TopChanged OnTopChanged;
28 public event LeftChanged OnLeftChanged;
29 public event BottomChanged OnBottomChanged;
30 public event RightChanged OnRightChanged;
31 #endregion
32
33 private int top;
34 private int left;
35 private int bottom;
36 private int right;
37
38 public Margin()
39 : this(0, 0)
40 {
41 }
42
43 public Margin(int top, int left)
44 : this(top, left, top, left)
45 {
46 }
47
48 public Margin(int top, int left, int bottom, int right)
49 {
50 this.top = top;
51 this.left = left;
52 this.bottom = bottom;
53 this.right = right;
54 }
55 /**//// <summary>
56 /// 获取或设置顶部间隔
57 /// </summary>
58 public int Top
59 {
60 get { return this.top; }
61 set {
62 this.top = value;
63 if (OnTopChanged != null)
64 OnTopChanged(this, null);
65 }
66 }
67 /**//// <summary>
68 /// 获取或设置左侧间隔
69 /// </summary>
70 public int Left
71 {
72 get { return this.left; }
73 set {
74 this.left = value;
75 if (OnLeftChanged != null)
76 OnLeftChanged(this, null);
77 }
78 }
79 /**//// <summary>
80 /// 获取或设置底部间隔
81 /// </summary>
82 public int Bottom
83 {
84 get { return this.bottom; }
85 set {
86 this.bottom = value;
87 if (OnBottomChanged != null)
88 OnBottomChanged(this, null);
89 }
90 }
91 /**//// <summary>
92 /// 获取或设置右侧间隔
93 /// </summary>
94 public int Right
95 {
96 get { return this.right; }
97 set {
98 this.right = value;
99 if (OnRightChanged != null)
100 OnRightChanged(this, null);
101 }
102 }
103 }
104}
105
106
Code
1// 功能:控件显示的阴影
2// 描述:
3// 编码:温伟鹏
4// 日期:12/12/2008 08:58:58
5
6using System;
7using System.Collections.Generic;
8using System.Text;
9using System.Drawing;
10using System.ComponentModel;
11
12namespace Esint.UI.WinFormUI
13{
14 public delegate void ShadowThicknessChanged(object sender,EventArgs e);
15 public delegate void ShadowColorChanged(object sender,EventArgs e);
16 /**//// <summary>
17 /// 控件阴影
18 /// </summary>
19 [TypeConverter(typeof(ControlShadowConverter))]
20 public class ControlShadow
21 {
22 Events#region Events
23 public event ShadowColorChanged OnShadowColorChanged;
24 public event ShadowThicknessChanged OnShadowThicknessChanged;
25 #endregion
26
27 private int shadowThickness;
28 private Color shadowColor;
29
30 public ControlShadow()
31 :this(3,Color.DarkGray)
32 {
33 }
34
35 public ControlShadow(Color sdColor)
36 :this(3,sdColor)
37 {
38 }
39
40 public ControlShadow(int sdThickness, Color sdColor)
41 {
42 this.shadowThickness = sdThickness;
43 this.shadowColor = sdColor;
44 }
45 /**//// <summary>
46 /// 获取或设置阴影厚度
47 /// </summary>
48 [Description("获取或设置阴影的厚度")]
49 public int ShadowThickness
50 {
51 get { return this.shadowThickness; }
52 set {
53 this.shadowThickness = value;
54 if (OnShadowThicknessChanged != null)
55 OnShadowThicknessChanged(this, null);
56 }
57 }
58 /**//// <summary>
59 /// 获取或设置阴影颜色
60 /// </summary>
61 [Description("获取或设置阴影的颜色")]
62 public Color ShadowColor
63 {
64 get { return this.shadowColor; }
65 set {
66 this.shadowColor = value;
67 if (OnShadowColorChanged != null)
68 OnShadowColorChanged(this, null);
69 }
70 }
71 }
72}
73
1// 功能:控件显示的阴影
2// 描述:
3// 编码:温伟鹏
4// 日期:12/12/2008 08:58:58
5
6using System;
7using System.Collections.Generic;
8using System.Text;
9using System.Drawing;
10using System.ComponentModel;
11
12namespace Esint.UI.WinFormUI
13{
14 public delegate void ShadowThicknessChanged(object sender,EventArgs e);
15 public delegate void ShadowColorChanged(object sender,EventArgs e);
16 /**//// <summary>
17 /// 控件阴影
18 /// </summary>
19 [TypeConverter(typeof(ControlShadowConverter))]
20 public class ControlShadow
21 {
22 Events#region Events
23 public event ShadowColorChanged OnShadowColorChanged;
24 public event ShadowThicknessChanged OnShadowThicknessChanged;
25 #endregion
26
27 private int shadowThickness;
28 private Color shadowColor;
29
30 public ControlShadow()
31 :this(3,Color.DarkGray)
32 {
33 }
34
35 public ControlShadow(Color sdColor)
36 :this(3,sdColor)
37 {
38 }
39
40 public ControlShadow(int sdThickness, Color sdColor)
41 {
42 this.shadowThickness = sdThickness;
43 this.shadowColor = sdColor;
44 }
45 /**//// <summary>
46 /// 获取或设置阴影厚度
47 /// </summary>
48 [Description("获取或设置阴影的厚度")]
49 public int ShadowThickness
50 {
51 get { return this.shadowThickness; }
52 set {
53 this.shadowThickness = value;
54 if (OnShadowThicknessChanged != null)
55 OnShadowThicknessChanged(this, null);
56 }
57 }
58 /**//// <summary>
59 /// 获取或设置阴影颜色
60 /// </summary>
61 [Description("获取或设置阴影的颜色")]
62 public Color ShadowColor
63 {
64 get { return this.shadowColor; }
65 set {
66 this.shadowColor = value;
67 if (OnShadowColorChanged != null)
68 OnShadowColorChanged(this, null);
69 }
70 }
71 }
72}
73
这是我从最近写的UI库中选出来的,版本有点旧,大家也可以看出来,代码有点多,同时有些代码也是可有可无的。呵呵。不过懒得改了,大家就当练习吧!