改版后的groupbox[原创]
一个比较美观的控件。代码如下:
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))]
15public class Grouper : System.Windows.Forms.UserControl
16![]()
{
17Enumerations#region Enumerations
18
19/**//// <summary>A special gradient enumeration.</summary>
20public enum GroupBoxGradientMode
21![]()
{
22/**//// <summary>Specifies no gradient mode.</summary>
23None = 4,
24
25/**//// <summary>Specifies a gradient from upper right to lower left.</summary>
26BackwardDiagonal = 3,
27
28/**//// <summary>Specifies a gradient from upper left to lower right.</summary>
29ForwardDiagonal = 2,
30
31/**//// <summary>Specifies a gradient from left to right.</summary>
32Horizontal = 0,
33
34/**//// <summary>Specifies a gradient from top to bottom.</summary>
35Vertical = 1
36}
37
38![]()
39#endregion
40
41Variables#region Variables
42
43private System.ComponentModel.Container components = null;
44private int V_RoundCorners = 10;
45private string V_GroupTitle = "The Grouper";
46private System.Drawing.Color V_BorderColor = Color.Black;
47private float V_BorderThickness = 1;
48private bool V_ShadowControl = false;
49private System.Drawing.Color V_BackgroundColor = Color.White;
50private System.Drawing.Color V_BackgroundGradientColor = Color.White;
51private GroupBoxGradientMode V_BackgroundGradientMode = GroupBoxGradientMode.None;
52private System.Drawing.Color V_ShadowColor = Color.DarkGray;
53private int V_ShadowThickness = 3;
54private System.Drawing.Image V_GroupImage = null;
55private System.Drawing.Color V_CustomGroupBoxColor = Color.White;
56private bool V_PaintGroupBox = false;
57private System.Drawing.Color V_BackColor = Color.Transparent;
58
59#endregion
60
61Properties#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")]
66public 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")]
71public 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.")]
76public 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.")]
80public 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.")]
84public 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.")]
88public int ShadowThickness
89![]()
{
90get
{return V_ShadowThickness;}
91set
92![]()
{
93if(value>10)
94![]()
{
95V_ShadowThickness=10;
96}
97else
98![]()
{
99if(value<1)
{V_ShadowThickness=1;}
100else
{V_ShadowThickness=value; }
101}
102
103this.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.")]
110public 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.")]
114public 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.")]
118public 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.")]
122public int RoundCorners
123![]()
{
124get
{return V_RoundCorners;}
125set
126![]()
{
127if(value>25)
128![]()
{
129V_RoundCorners=25;
130}
131else
132![]()
{
133if(value<1)
{V_RoundCorners=1;}
134else
{V_RoundCorners=value; }
135}
136![]()
137this.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.")]
143public 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.")]
147public 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.")]
151public float BorderThickness
152![]()
{
153get
{return V_BorderThickness;}
154set
155![]()
{
156if(value>3)
157![]()
{
158V_BorderThickness=3;
159}
160else
161![]()
{
162if(value<1)
{V_BorderThickness=1;}
163else
{V_BorderThickness=value;}
164}
165this.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.")]
171public bool ShadowControl
{get
{return V_ShadowControl;} set
{V_ShadowControl=value; this.Refresh();}}
172
173#endregion
174
175Constructor#region Constructor
176![]()
177/**//// <summary>This method will construct a new GroupBox control.</summary>
178public Grouper()
179![]()
{
180InitializeStyles();
181InitializeGroupBox();
182}
183
184![]()
185#endregion
186
187DeConstructor#region DeConstructor
188
189/**//// <summary>This method will dispose of the GroupBox control.</summary>
190protected override void Dispose( bool disposing )
191![]()
{
192if(disposing)
{if(components!=null)
{components.Dispose();}}
193base.Dispose(disposing);
194}
195
196
197#endregion
198
199Initialization#region Initialization
200
201/**//// <summary>This method will initialize the controls custom styles.</summary>
202private void InitializeStyles()
203![]()
{
204//Set the control styles----------------------------------
205this.SetStyle(ControlStyles.DoubleBuffer, true);
206this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
207this.SetStyle(ControlStyles.UserPaint, true);
208this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
209//--------------------------------------------------------
210}
211
212
213/**//// <summary>This method will initialize the GroupBox control.</summary>
214private void InitializeGroupBox()
215![]()
{
216components = new System.ComponentModel.Container();
217this.Resize+=new EventHandler(GroupBox_Resize);
218this.DockPadding.All = 20;
219this.Name = "GroupBox";
220this.Size = new System.Drawing.Size(368, 288);
221}
222
223![]()
224#endregion
225
226Protected Methods#region Protected Methods
227![]()
228/**//// <summary>Overrides the OnPaint method to paint control.</summary>
229/// <param name="e">The paint event arguments.</param>
230protected override void OnPaint(PaintEventArgs e)
231![]()
{
232PaintBack(e.Graphics);
233PaintGroupText(e.Graphics);
234}
235
236#endregion
237
238Private 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>
242private void PaintGroupText(System.Drawing.Graphics g)
243![]()
{
244//Check if string has something-------------
245if(this.GroupTitle==string.Empty)
{return;}
246//------------------------------------------
247
248//Set Graphics smoothing mode to Anit-Alias--
249g.SmoothingMode = SmoothingMode.AntiAlias;
250//-------------------------------------------
251
252//Declare Variables------------------
253SizeF StringSize = g.MeasureString(this.GroupTitle, this.Font);
254Size StringSize2 = StringSize.ToSize();
255if(this.GroupImage!=null)
{StringSize2.Width+=18;}
256int ArcWidth = this.RoundCorners;
257int ArcHeight = this.RoundCorners;
258int ArcX1 = 20;
259int ArcX2 = (StringSize2.Width+34) - (ArcWidth + 1);
260int ArcY1 = 0;
261int ArcY2 = 24 - (ArcHeight + 1);
262System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
263System.Drawing.Brush BorderBrush = new SolidBrush(this.BorderColor);
264System.Drawing.Pen BorderPen = new Pen(BorderBrush, this.BorderThickness);
265System.Drawing.Drawing2D.LinearGradientBrush BackgroundGradientBrush = null;
266System.Drawing.Brush BackgroundBrush = (this.PaintGroupBox) ? new SolidBrush(this.CustomGroupBoxColor) : new SolidBrush(this.BackgroundColor);
267System.Drawing.SolidBrush TextColorBrush = new SolidBrush(this.ForeColor);
268System.Drawing.SolidBrush ShadowBrush = null;
269System.Drawing.Drawing2D.GraphicsPath ShadowPath = null;
270//-----------------------------------
271
272//Check if shadow is needed----------
273if(this.ShadowControl)
274![]()
{
275ShadowBrush = new SolidBrush(this.ShadowColor);
276ShadowPath = new System.Drawing.Drawing2D.GraphicsPath();
277ShadowPath.AddArc(ArcX1+(this.ShadowThickness-1), ArcY1+(this.ShadowThickness-1), ArcWidth, ArcHeight, 180, GroupBoxConstants.SweepAngle); // Top Left
278ShadowPath.AddArc(ArcX2+(this.ShadowThickness-1), ArcY1+(this.ShadowThickness-1), ArcWidth, ArcHeight, 270, GroupBoxConstants.SweepAngle); //Top Right
279ShadowPath.AddArc(ArcX2+(this.ShadowThickness-1), ArcY2+(this.ShadowThickness-1), ArcWidth, ArcHeight, 360, GroupBoxConstants.SweepAngle); //Bottom Right
280ShadowPath.AddArc(ArcX1+(this.ShadowThickness-1), ArcY2+(this.ShadowThickness-1), ArcWidth, ArcHeight, 90, GroupBoxConstants.SweepAngle); //Bottom Left
281ShadowPath.CloseAllFigures();
282
283//Paint Rounded Rectangle------------
284g.FillPath(ShadowBrush, ShadowPath);
285//-----------------------------------
286}
287//-----------------------------------
288
289//Create Rounded Rectangle Path------
290path.AddArc(ArcX1, ArcY1, ArcWidth, ArcHeight, 180, GroupBoxConstants.SweepAngle); // Top Left
291path.AddArc(ArcX2, ArcY1, ArcWidth, ArcHeight, 270, GroupBoxConstants.SweepAngle); //Top Right
292path.AddArc(ArcX2, ArcY2, ArcWidth, ArcHeight, 360, GroupBoxConstants.SweepAngle); //Bottom Right
293path.AddArc(ArcX1, ArcY2, ArcWidth, ArcHeight, 90, GroupBoxConstants.SweepAngle); //Bottom Left
294path.CloseAllFigures();
295//-----------------------------------
296
297//Check if Gradient Mode is enabled--
298if(this.PaintGroupBox)
299![]()
{
300//Paint Rounded Rectangle------------
301g.FillPath(BackgroundBrush, path);
302//-----------------------------------
303}
304else
305![]()
{
306if(this.BackgroundGradientMode==GroupBoxGradientMode.None)
307![]()
{
308//Paint Rounded Rectangle------------
309g.FillPath(BackgroundBrush, path);
310//-----------------------------------
311}
312else
313![]()
{
314BackgroundGradientBrush = new LinearGradientBrush(new Rectangle(0,0,this.Width,this.Height), this.BackgroundColor, this.BackgroundGradientColor, (LinearGradientMode)this.BackgroundGradientMode);
315![]()
316//Paint Rounded Rectangle------------
317g.FillPath(BackgroundGradientBrush, path);
318//-----------------------------------
319}
320}
321//-----------------------------------
322
323//Paint Borded-----------------------
324g.DrawPath(BorderPen, path);
325//-----------------------------------
326
327//Paint Text-------------------------
328int CustomStringWidth = (this.GroupImage!=null) ? 44 : 28;
329g.DrawString(this.GroupTitle, this.Font, TextColorBrush, CustomStringWidth, 5);
330//-----------------------------------
331
332//Draw GroupImage if there is one----
333if(this.GroupImage!=null)
334![]()
{
335g.DrawImage(this.GroupImage, 28,4, 16, 16);
336}
337//-----------------------------------
338
339//Destroy Graphic Objects------------
340if(path!=null)
{path.Dispose();}
341if(BorderBrush!=null)
{BorderBrush.Dispose();}
342if(BorderPen!=null)
{BorderPen.Dispose();}
343if(BackgroundGradientBrush!=null)
{BackgroundGradientBrush.Dispose();}
344if(BackgroundBrush!=null)
{BackgroundBrush.Dispose();}
345if(TextColorBrush!=null)
{TextColorBrush .Dispose();}
346if(ShadowBrush!=null)
{ShadowBrush.Dispose();}
347if(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>
354private void PaintBack(System.Drawing.Graphics g)
355![]()
{
356//Set Graphics smoothing mode to Anit-Alias--
357g.SmoothingMode = SmoothingMode.AntiAlias;
358//-------------------------------------------
359
360//Declare Variables------------------
361int ArcWidth = this.RoundCorners * 2;
362int ArcHeight = this.RoundCorners * 2;
363int ArcX1 = 0;
364int ArcX2 = (this.ShadowControl) ? (this.Width - (ArcWidth + 1))-this.ShadowThickness : this.Width - (ArcWidth + 1);
365int ArcY1 = 10;
366int ArcY2 = (this.ShadowControl) ? (this.Height - (ArcHeight + 1))-this.ShadowThickness : this.Height - (ArcHeight + 1);
367System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
368System.Drawing.Brush BorderBrush = new SolidBrush(this.BorderColor);
369System.Drawing.Pen BorderPen = new Pen(BorderBrush, this.BorderThickness);
370System.Drawing.Drawing2D.LinearGradientBrush BackgroundGradientBrush = null;
371System.Drawing.Brush BackgroundBrush = new SolidBrush(this.BackgroundColor);
372System.Drawing.SolidBrush ShadowBrush = null;
373System.Drawing.Drawing2D.GraphicsPath ShadowPath = null;
374//-----------------------------------
375
376//Check if shadow is needed----------
377if(this.ShadowControl)
378![]()
{
379ShadowBrush = new SolidBrush(this.ShadowColor);
380ShadowPath = new System.Drawing.Drawing2D.GraphicsPath();
381ShadowPath.AddArc(ArcX1+this.ShadowThickness, ArcY1+this.ShadowThickness, ArcWidth, ArcHeight, 180, GroupBoxConstants.SweepAngle); // Top Left
382ShadowPath.AddArc(ArcX2+this.ShadowThickness, ArcY1+this.ShadowThickness, ArcWidth, ArcHeight, 270, GroupBoxConstants.SweepAngle); //Top Right
383ShadowPath.AddArc(ArcX2+this.ShadowThickness, ArcY2+this.ShadowThickness, ArcWidth, ArcHeight, 360, GroupBoxConstants.SweepAngle); //Bottom Right
384ShadowPath.AddArc(ArcX1+this.ShadowThickness, ArcY2+this.ShadowThickness, ArcWidth, ArcHeight, 90, GroupBoxConstants.SweepAngle); //Bottom Left
385ShadowPath.CloseAllFigures();
386
387//Paint Rounded Rectangle------------
388g.FillPath(ShadowBrush, ShadowPath);
389//-----------------------------------
390}
391//-----------------------------------
392
393//Create Rounded Rectangle Path------
394path.AddArc(ArcX1, ArcY1, ArcWidth, ArcHeight, 180, GroupBoxConstants.SweepAngle); // Top Left
395path.AddArc(ArcX2, ArcY1, ArcWidth, ArcHeight, 270, GroupBoxConstants.SweepAngle); //Top Right
396path.AddArc(ArcX2, ArcY2, ArcWidth, ArcHeight, 360, GroupBoxConstants.SweepAngle); //Bottom Right
397path.AddArc(ArcX1, ArcY2, ArcWidth, ArcHeight, 90, GroupBoxConstants.SweepAngle); //Bottom Left
398path.CloseAllFigures();
399//-----------------------------------
400
401//Check if Gradient Mode is enabled--
402if(this.BackgroundGradientMode==GroupBoxGradientMode.None)
403![]()
{
404//Paint Rounded Rectangle------------
405g.FillPath(BackgroundBrush, path);
406//-----------------------------------
407}
408else
409![]()
{
410BackgroundGradientBrush = new LinearGradientBrush(new Rectangle(0,0,this.Width,this.Height), this.BackgroundColor, this.BackgroundGradientColor, (LinearGradientMode)this.BackgroundGradientMode);
411![]()
412//Paint Rounded Rectangle------------
413g.FillPath(BackgroundGradientBrush, path);
414//-----------------------------------
415}
416//-----------------------------------
417
418//Paint Borded-----------------------
419g.DrawPath(BorderPen, path);
420//-----------------------------------
421
422//Destroy Graphic Objects------------
423if(path!=null)
{path.Dispose();}
424if(BorderBrush!=null)
{BorderBrush.Dispose();}
425if(BorderPen!=null)
{BorderPen.Dispose();}
426if(BackgroundGradientBrush!=null)
{BackgroundGradientBrush.Dispose();}
427if(BackgroundBrush!=null)
{BackgroundBrush.Dispose();}
428if(ShadowBrush!=null)
{ShadowBrush.Dispose();}
429if(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>
437private void GroupBox_Resize(object sender, EventArgs e)
438![]()
{
439this.Refresh();
440}
441
442
443#endregion
444}
445}
446