先看效果:
如图,上面使用了LinearGradientBrush。从而让GroupBox显得更为个性化。
再看源码:
using CommonUtils.Controls.GroupControls;
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Collections.Generic;
namespace CommonUtils.Controls.GroupBoxControls
{
/// <summary>A special custom rounding GroupBox with many painting features.</summary>
[ToolboxBitmap(typeof(LinearGradientGrouper), "MyControls.Grouper.bmp")]
[Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]
public class LinearGradientGrouper : UserControl
{
#region Variables
private System.ComponentModel.Container components = null;
private int _RoundCorners = 10;
private string _GroupTitle = "The Grouper";
private Color _BorderColor = Color.Black;
private float _BorderThickness = 1;
private bool _ShadowControl = false;
private Color _BackgroundColor = Color.White;
private Color _BackgroundGradientColor = Color.White;
private GroupBoxGradientMode _BackgroundGradientMode = GroupBoxGradientMode.None;
private Color _ShadowColor = Color.DarkGray;
private int _ShadowThickness = 3;
private Image _GroupImage = null;
private Color _CustomGroupBoxColor = Color.White;
private bool _PaintGroupBox = false;
private Color _BackColor = Color.Transparent;
Color[] colors = new Color[] { Color.Red, Color.OrangeRed, Color.DarkRed, Color.DarkGoldenrod, Color.Orange, Color.Yellow, Color.White };
#endregion
#region Constructor
/// <summary>This method will construct a new GroupBox control.</summary>
public LinearGradientGrouper()
{
InitializeStyles();
InitializeGroupBox();
}
#region Initialization
/// <summary>This method will initialize the controls custom styles.</summary>
private void InitializeStyles()
{
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
}
/// <summary>This method will initialize the GroupBox control.</summary>
private void InitializeGroupBox()
{
components = new System.ComponentModel.Container();
this.Resize += new EventHandler(GroupBox_Resize);
this.DockPadding.All = 20;
this.Name = "GroupBox";
this.Size = new System.Drawing.Size(368, 288);
}
#endregion Initialization
#endregion Constructor
#region Properties
/// <summary>This feature will paint the background color of the control.</summary>
[Category("Appearance"), Description("This feature will paint the background color of the control.")]
public override Color BackColor
{
get { return _BackColor; }
set { _BackColor = value; this.Refresh(); }
}
/// <summary>This feature will paint the group title background to the specified color if PaintGroupBox is set to true.</summary>
[Category("Appearance"), Description("This feature will paint the group title background to the specified color if PaintGroupBox is set to true.")]
public Color CustomGroupBoxColor
{
get { return _CustomGroupBoxColor; }
set { _CustomGroupBoxColor = value; this.Refresh(); }
}
/// <summary>This feature will paint the group title background to the CustomGroupBoxColor.</summary>
[Ca