一个比较美观的控件。代码如下:
1using System;
2using System.Collections;
3using System.ComponentModel;
4using System.ComponentModel.Design;
5using System.Drawing;
6using System.Drawing.Drawing2D;
7using System.Data;
8using System.Windows.Forms;
9
10namespace CodeVendor.Controls
11{
12 /**//// <summary>较特别的且可变色的GBOX.--Nie</summary>
13 [ToolboxBitmap(typeof(Grouper), "CodeVendor.Controls.Grouper.bmp")]
14 [Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]
15 public class Grouper : System.Windows.Forms.UserControl
16 {
17 Enumerations#region Enumerations
18
19 /**//// <summary>A special gradient enumeration.</summary>
20 public enum GroupBoxGradientMode
21 {
22 /**//// <summary>Specifies no gradient mode.</summary>
23 None = 4,
24
25 /**//// <summary>Specifies a gradient from upper right to lower left.</summary>
26 BackwardDiagonal = 3,
27
28 /**//// <summary>Specifies a gradient from upper left to lower right.</summary>
29 ForwardDiagonal = 2,
30
31 /**//// <summary>Specifies a gradient from left to right.</summary>
32 Horizontal = 0,
33
34 /**//// <summary>Specifies a gradient from top to bottom.</summary>
35 Vertical = 1
36 }
37
38
39 #endregion
40
41 Variables#region Variables
42
43 private System.ComponentModel.Container components = null;
44 private int V_RoundCorners = 10;
45 private string V_GroupTitle = "The Grouper";
46 private System.Drawing.Color V_BorderColor = Color.Black;
47 private float V_BorderThickness = 1;
48 private bool V_ShadowControl = false;
49 private System.Drawing.Color V_BackgroundColor = Color.White;
50 private System.Drawing.Color V_BackgroundGradientColor = Color.White;
51 private GroupBoxGradientMode V_BackgroundGradientMode = GroupBoxGradientMode.None;
52 private System.Drawing.Color V_ShadowColor = Color.DarkGray;
53 private int V_ShadowThickness = 3;
54 private System.Drawing.Image V_GroupImage = null;
55 private System.Drawing.Color V_CustomGroupBoxColor = Color.White;
56 private bool V_PaintGroupBox = false;
57 private System.Drawing.Color V_BackColor = Color.Transparent;
58
59 #endregion
60
61 Properties#region Properties
62
63 /**//// <summary>设置控件背景颜色.----Nie</summary>
64 //[Category("Appearance"), Description("This feature will paint the background color of the control.")]
65 [Category("Appearance"), Description("This feature will paint the background color of the control.---Shem.Nie")]
66 public override System.Drawing.Color BackColor{get{return V_BackColor;} set{V_BackColor=value; this.Refresh();}}
67
68 /**//// <summary>设置控件标题背景颜色.---Nie</summary>
69 //[Category("Appearance"), Description("This feature will paint the group title background to the specified color if PaintGroupBox is set to true.")]
70 [Category("Appearance"), Description("This feature will paint the group title background to the specified color if PaintGroupBox is set to true..---Shem.Nie")]
71 public System.Drawing.Color CustomGroupBoxColor{get{return V_CustomGroupBoxColor;} set{V_CustomGroupBoxColor=value; this.Refresh();}}
72
73 /**//// <summary>This feature will paint the group title background to the CustomGroupBoxColor.</summary>
74 //[Category("Appearance"), Description("This feature will paint the group title background to the CustomGroupBoxColor.")]
75 [Category("Appearance"), Description("This feature will paint the group title background to the CustomGroupBoxColor.")]
76 public bool PaintGroupBox{get{return V_PaintGroupBox;} set{V_PaintGroupBox=value; this.Refresh();}}
77
78 /**//// <summary>This feature can add a 16 x 16 image to the group title bar.</summary>
79 [Category("Appearance"), Description("This feature can add a 16 x 16 image to the group title bar.")]
80 public System.Drawing.Image GroupImage{get{return V_GroupImage;} set{V_GroupImage=value; this.Refresh();}}
81
82 /**//// <summary>This feature will change the control's shadow color.</summary>
83 [Category("Appearance"), Description("This feature will change the control's shadow color.")]
84 public System.Drawing.Color ShadowColor{get{return V_ShadowColor;} set{V_ShadowColor=value; this.Refresh();}}
85
86 /**//// <summary>This feature will change the size of the shadow border.</summary>
87 [Category("Appearance"), Description("This feature will change the size of the shadow border.")]
88 public int ShadowThickness
89 {
90 get{return V_ShadowThickness;}
91 set
92 {
93 if(value>10)
94 {
95 V_ShadowThickness=10;
96 }
97 else
98 {
99 if(value<1){V_ShadowThickness=1;}
100 else{V_ShadowThickness=value; }
101 }
102
103 this.Refresh();
104 }
105 }
106
107
108 /**//// <summary>This feature will change the group control color. This color can also be used in combination with BackgroundGradientColor for a gradient paint.</summary>
109 [Category("Appearance"), Description("This feature will change the group control color. This color can also be used in combination with BackgroundGradientColor for a gradient paint.")]
110 public System.Drawing.Color BackgroundColor{get{return V_BackgroundColor;} set{V_BackgroundColor=value; this.Refresh();}}
111
112 /**//// <summary>This feature can be used in combination with BackgroundColor to create a gradient background.</summary>
113 [Category("Appearance"), Description("This feature can be used in combination with BackgroundColor to create a gradient background.")]
114 public System.Drawing.Color BackgroundGradientColor{get{return V_BackgroundGradientColor;} set{V_BackgroundGradientColor=value; this.Refresh();}}
115
116 /**//// <summary>This feature turns on background gradient painting.</summary>
117 [Category("Appearance"), Description("This feature turns on background gradient painting.")]
118 public GroupBoxGradientMode BackgroundGradientMode{get{return V_BackgroundGradientMode;} set{V_BackgroundGradientMode=value; this.Refresh();}}
119
120 /**//// <summary>This feature will round the corners of the control.</summary>
121 [Category("Appearance"), Description("This feature will round the corners of the control.")]
122 public int RoundCorners
123 {
124 get{return V_RoundCorners;}
125 set
126 {
127 if(value>25)
128 {
129 V_RoundCorners=25;
130 }
131 else
132 {
133 if(value<1){V_RoundCorners=1;}
134 else{V_RoundCorners=value; }
135 }
136
137 this.Refresh();
138 }
139 }
140
141 /**//// <summary>This feature will add a group title to the control.</summary>
142 [Category("Appearance"), Description("This feature will add a group title to the control.")]
143 public string GroupTitle{get{return V_GroupTitle;} set{V_GroupTitle=value; this.Refresh();}}
144
145 /**//// <summary>This feature will allow you to change the color of the control's border.</summary>
146 [Category("Appearance"), Description("This feature will allow you to change the color of the control's border.")]
147 public System.Drawing.Color BorderColor{get{return V_BorderColor;} set{V_BorderColor=value; this.Refresh();}}
148
149 /**//// <summary>This feature will allow you to set the control's border size.</summary>
150 [Category("Appearance"), Description("This feature will allow you to set the control's border size.")]
151 public float BorderThickness
152 {
153 get{return V_BorderThickness;}
154 set
155 {
156 if(value>3)
157 {
158 V_BorderThickness=3;
159 }
160 else
161 {
162 if(value<1){V_BorderThickness=1;}
163 else{V_BorderThickness=value;}
164 }
165 this.Refresh();
166 }
167 }
168
169 /**//// <summary>This feature will allow you to turn on control shadowing.</summary>
170 [Category("Appearance"), Description("This feature will allow you to turn on control shadowing.")]
171 public bool ShadowControl{get{return V_ShadowControl;} set{V_ShadowControl=value; this.Refresh();}}
172
173 #endregion
174
175 Constructor#region Constructor
176
177 /**//// <summary>This method will construct a new GroupBox control.</summary>
178 public Grouper()
179 {
180 InitializeStyles();
181 InitializeGroupBox();
182 }
183
184
185 #endregion
186
187 DeConstructor#region DeConstructor
188
189 /**//// <summary>This method will dispose of the GroupBox control.</summary>
190 protected override void Dispose( bool disposing )
191 {
192 if(disposing){if(components!=null){components.Dispose();}}
193 base.Dispose(disposing);
194 }
195
196
197 #endregion
198
199 Initialization#region Initialization
200
201 /**//// <summary>This method will initialize the controls custom styles.</summary>
202 private void InitializeStyles()
203 {
204 //Set the control styles----------------------------------
205 this.SetStyle(ControlStyles.DoubleBuffer, true);
206 this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
207 this.SetStyle(ControlStyles.UserPaint, true);
208 this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
209 //--------------------------------------------------------
210 }
211
212
213 /**//// <summary>This method will initialize the GroupBox control.</summary>
214 private void InitializeGroupBox()
215 {
216 components = new System.ComponentModel.Container();
217 this.Resize+=new EventHandler(GroupBox_Resize);
218 this.DockPadding.All = 20;
219 this.Name = "GroupBox";
220 this.Size = new System.Drawing.Size(368, 288);
221 }
222
223
224 #endregion
225
226 Protected Methods#region Protected Methods
227
228 /**//// <summary>Overrides the OnPaint method to paint control.</summary>
229 /// <param name="e">The paint event arguments.</param>
230 protected override void OnPaint(PaintEventArgs e)
231 {
232 PaintBack(e.Graphics);
233 PaintGroupText(e.Graphics);
234 }
235
236 #endregion
237
238 Private Methods#region Private Methods
239
240 /**//// <summary>This method will paint the group title.</summary>
241 /// <param name="g">The paint event graphics object.</param>
242 private void PaintGroupText(System.Drawing.Graphics g)
243 {
244 //Check if string has something-------------
245 if(this.GroupTitle==string.Empty){return;}
246 //------------------------------------------
247
248 //Set Graphics smoothing mode to Anit-Alias--
249 g.SmoothingMode = SmoothingMode.AntiAlias;
250 //-------------------------------------------
251
252 //Declare Variables------------------
253 SizeF StringSize = g.MeasureString(this.GroupTitle, this.Font);
254 Size StringSize2 = StringSize.ToSize();
255 if(this.GroupImage!=null){StringSize2.Width+=18;}
256 int ArcWidth = this.RoundCorners;
257 int ArcHeight = this.RoundCorners;
258 int ArcX1 = 20;
259 int ArcX2 = (StringSize2.Width+34) - (ArcWidth + 1);
260 int ArcY1 = 0;
261 int ArcY2 = 24 - (ArcHeight + 1);
262 System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
263 System.Drawing.Brush BorderBrush = new SolidBrush(this.BorderColor);
264 System.Drawing.Pen BorderPen = new Pen(BorderBrush, this.BorderThickness);
265 System.Drawing.Drawing2D.LinearGradientBrush BackgroundGradientBrush = null;
266 System.Drawing.Brush BackgroundBrush = (this.PaintGroupBox) ? new SolidBrush(this.CustomGroupBoxColor) : new SolidBrush(this.BackgroundColor);
267 System.Drawing.SolidBrush TextColorBrush = new SolidBrush(this.ForeColor);
268 System.Drawing.SolidBrush ShadowBrush = null;
269 System.Drawing.Drawing2D.GraphicsPath ShadowPath = null;
270 //-----------------------------------
271
272 //Check if shadow is needed----------
273 if(this.ShadowControl)
274 {
275 ShadowBrush = new SolidBrush(this.ShadowColor);
276 ShadowPath = new System.Drawing.Drawing2D.GraphicsPath();
277 ShadowPath.AddArc(ArcX1+(this.ShadowThickness-1), ArcY1+(this.ShadowThickness-1), ArcWidth, ArcHeight, 180, GroupBoxConstants.SweepAngle); // Top Left
278 ShadowPath.AddArc(ArcX2+(this.ShadowThickness-1), ArcY1+(this.ShadowThickness-1), ArcWidth, ArcHeight, 270, GroupBoxConstants.SweepAngle); //Top Right
279 ShadowPath.AddArc(ArcX2+(this.ShadowThickness-1), ArcY2+(this.ShadowThickness-1), ArcWidth, ArcHeight, 360, GroupBoxConstants.SweepAngle); //Bottom Right
280 ShadowPath.AddArc(ArcX1+(this.ShadowThickness-1), ArcY2+(this.ShadowThickness-1), ArcWidth, ArcHeight, 90, GroupBoxConstants.SweepAngle); //Bottom Left
281 ShadowPath.CloseAllFigures();
282
283 //Paint Rounded Rectangle------------
284 g.FillPath(ShadowBrush, ShadowPath);
285 //-----------------------------------
286 }
287 //-----------------------------------
288
289 //Create Rounded Rectangle Path------
290 path.AddArc(ArcX1, ArcY1, ArcWidth, ArcHeight, 180, GroupBoxConstants.SweepAngle); // Top Left
291 path.AddArc(ArcX2, ArcY1, ArcWidth, ArcHeight, 270, GroupBoxConstants.SweepAngle); //Top Right
292 path.AddArc(ArcX2, ArcY2, ArcWidth, ArcHeight, 360, GroupBoxConstants.SweepAngle); //Bottom Right
293 path.AddArc(ArcX1, ArcY2, ArcWidth, ArcHeight, 90, GroupBoxConstants.SweepAngle); //Bottom Left
294 path.CloseAllFigures();
295 //-----------------------------------
296
297 //Check if Gradient Mode is enabled--
298 if(this.PaintGroupBox)
299 {
300 //Paint Rounded Rectangle------------
301 g.FillPath(BackgroundBrush, path);
302 //-----------------------------------
303 }
304 else
305 {
306 if(this.BackgroundGradientMode==GroupBoxGradientMode.None)
307 {
308 //Paint Rounded Rectangle------------
309 g.FillPath(BackgroundBrush, path);
310 //-----------------------------------
311 }
312 else
313 {
314 BackgroundGradientBrush = new LinearGradientBrush(new Rectangle(0,0,this.Width,this.Height), this.BackgroundColor, this.BackgroundGradientColor, (LinearGradientMode)this.BackgroundGradientMode);
315
316 //Paint Rounded Rectangle------------
317 g.FillPath(BackgroundGradientBrush, path);
318 //-----------------------------------
319 }
320 }
321 //-----------------------------------
322
323 //Paint Borded-----------------------
324 g.DrawPath(BorderPen, path);
325 //-----------------------------------
326
327 //Paint Text-------------------------
328 int CustomStringWidth = (this.GroupImage!=null) ? 44 : 28;
329 g.DrawString(this.GroupTitle, this.Font, TextColorBrush, CustomStringWidth, 5);
330 //-----------------------------------
331
332 //Draw GroupImage if there is one----
333 if(this.GroupImage!=null)
334 {
335 g.DrawImage(this.GroupImage, 28,4, 16, 16);
336 }
337 //-----------------------------------
338
339 //Destroy Graphic Objects------------
340 if(path!=null){path.Dispose();}
341 if(BorderBrush!=null){BorderBrush.Dispose();}
342 if(BorderPen!=null){BorderPen.Dispose();}
343 if(BackgroundGradientBrush!=null){BackgroundGradientBrush.Dispose();}
344 if(BackgroundBrush!=null){BackgroundBrush.Dispose();}
345 if(TextColorBrush!=null){TextColorBrush .Dispose();}
346 if(ShadowBrush!=null){ShadowBrush.Dispose();}
347 if(ShadowPath!=null){ShadowPath.Dispose();}
348 //-----------------------------------
349 }
350
351
352 /**//// <summary>This method will paint the control.</summary>
353 /// <param name="g">The paint event graphics object.</param>
354 private void PaintBack(System.Drawing.Graphics g)
355 {
356 //Set Graphics smoothing mode to Anit-Alias--
357 g.SmoothingMode = SmoothingMode.AntiAlias;
358 //-------------------------------------------
359
360 //Declare Variables------------------
361 int ArcWidth = this.RoundCorners * 2;
362 int ArcHeight = this.RoundCorners * 2;
363 int ArcX1 = 0;
364 int ArcX2 = (this.ShadowControl) ? (this.Width - (ArcWidth + 1))-this.ShadowThickness : this.Width - (ArcWidth + 1);
365 int ArcY1 = 10;
366 int ArcY2 = (this.ShadowControl) ? (this.Height - (ArcHeight + 1))-this.ShadowThickness : this.Height - (ArcHeight + 1);
367 System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
368 System.Drawing.Brush BorderBrush = new SolidBrush(this.BorderColor);
369 System.Drawing.Pen BorderPen = new Pen(BorderBrush, this.BorderThickness);
370 System.Drawing.Drawing2D.LinearGradientBrush BackgroundGradientBrush = null;
371 System.Drawing.Brush BackgroundBrush = new SolidBrush(this.BackgroundColor);
372 System.Drawing.SolidBrush ShadowBrush = null;
373 System.Drawing.Drawing2D.GraphicsPath ShadowPath = null;
374 //-----------------------------------
375
376 //Check if shadow is needed----------
377 if(this.ShadowControl)
378 {
379 ShadowBrush = new SolidBrush(this.ShadowColor);
380 ShadowPath = new System.Drawing.Drawing2D.GraphicsPath();
381 ShadowPath.AddArc(ArcX1+this.ShadowThickness, ArcY1+this.ShadowThickness, ArcWidth, ArcHeight, 180, GroupBoxConstants.SweepAngle); // Top Left
382 ShadowPath.AddArc(ArcX2+this.ShadowThickness, ArcY1+this.ShadowThickness, ArcWidth, ArcHeight, 270, GroupBoxConstants.SweepAngle); //Top Right
383 ShadowPath.AddArc(ArcX2+this.ShadowThickness, ArcY2+this.ShadowThickness, ArcWidth, ArcHeight, 360, GroupBoxConstants.SweepAngle); //Bottom Right
384 ShadowPath.AddArc(ArcX1+this.ShadowThickness, ArcY2+this.ShadowThickness, ArcWidth, ArcHeight, 90, GroupBoxConstants.SweepAngle); //Bottom Left
385 ShadowPath.CloseAllFigures();
386
387 //Paint Rounded Rectangle------------
388 g.FillPath(ShadowBrush, ShadowPath);
389 //-----------------------------------
390 }
391 //-----------------------------------
392
393 //Create Rounded Rectangle Path------
394 path.AddArc(ArcX1, ArcY1, ArcWidth, ArcHeight, 180, GroupBoxConstants.SweepAngle); // Top Left
395 path.AddArc(ArcX2, ArcY1, ArcWidth, ArcHeight, 270, GroupBoxConstants.SweepAngle); //Top Right
396 path.AddArc(ArcX2, ArcY2, ArcWidth, ArcHeight, 360, GroupBoxConstants.SweepAngle); //Bottom Right
397 path.AddArc(ArcX1, ArcY2, ArcWidth, ArcHeight, 90, GroupBoxConstants.SweepAngle); //Bottom Left
398 path.CloseAllFigures();
399 //-----------------------------------
400
401 //Check if Gradient Mode is enabled--
402 if(this.BackgroundGradientMode==GroupBoxGradientMode.None)
403 {
404 //Paint Rounded Rectangle------------
405 g.FillPath(BackgroundBrush, path);
406 //-----------------------------------
407 }
408 else
409 {
410 BackgroundGradientBrush = new LinearGradientBrush(new Rectangle(0,0,this.Width,this.Height), this.BackgroundColor, this.BackgroundGradientColor, (LinearGradientMode)this.BackgroundGradientMode);
411
412 //Paint Rounded Rectangle------------
413 g.FillPath(BackgroundGradientBrush, path);
414 //-----------------------------------
415 }
416 //-----------------------------------
417
418 //Paint Borded-----------------------
419 g.DrawPath(BorderPen, path);
420 //-----------------------------------
421
422 //Destroy Graphic Objects------------
423 if(path!=null){path.Dispose();}
424 if(BorderBrush!=null){BorderBrush.Dispose();}
425 if(BorderPen!=null){BorderPen.Dispose();}
426 if(BackgroundGradientBrush!=null){BackgroundGradientBrush.Dispose();}
427 if(BackgroundBrush!=null){BackgroundBrush.Dispose();}
428 if(ShadowBrush!=null){ShadowBrush.Dispose();}
429 if(ShadowPath!=null){ShadowPath.Dispose();}
430 //-----------------------------------
431 }
432
433
434 /**//// <summary>This method fires when the GroupBox resize event occurs.</summary>
435 /// <param name="sender">The object the sent the event.</param>
436 /// <param name="e">The event arguments.</param>
437 private void GroupBox_Resize(object sender, EventArgs e)
438 {
439 this.Refresh();
440 }
441
442
443 #endregion
444 }
445}
446