using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Text; using System.Windows.Forms; using System.Drawing; using System.Drawing.Drawing2D; namespace AMT_SW_GUI { public partial class AMT_GroupBox : GroupBox { #region Enumerations /// A special gradient enumeration. public enum GroupBoxGradientMode { /// Specifies no gradient mode. None = 4, /// Specifies a gradient from upper right to lower left. BackwardDiagonal = 3, /// Specifies a gradient from upper left to lower right. ForwardDiagonal = 2, /// Specifies a gradient from left to right. Horizontal = 0, /// Specifies a gradient from top to bottom. Vertical = 1 } #endregion // parts of the code are copied from code project : http://www.codeproject.com/KB/miscctrl/grouper.aspx?display=Print private int V_RoundCorners = 10; private string V_GroupTitle = "The Grouper"; private System.Drawing.Color V_BorderColor = Color.Black; private float V_BorderThickness = 1; private bool V_ShadowControl = false; private System.Drawing.Color V_BackgroundColor = Color.White; private System.Drawing.Color V_BackgroundGradientColor = Color.White; private GroupBoxGradientMode V_BackgroundGradientMode = GroupBoxGradientMode.None; private System.Drawing.Color V_ShadowColor = Color.DarkGray; private int V_ShadowThickness = 3; private System.Drawing.Image V_GroupImage = null; private System.Drawing.Color V_CustomGroupBoxColor = Color.White; private bool V_PaintGroupBox = false; private System.Drawing.Color V_BackColor = Color.Transparent; #region Enumerations #endregion Color _BorderColor = Color.FromArgb(158, 191, 217); public Color BorderColor { get { return _BorderColor; } set { _BorderColor = value; } } #region Properties /// This feature will paint the background color of the control. [Category("Appearance"), Description("This feature will paint the background color of the control.")] public override System.Drawing.Color BackColor { get { return V_BackColor; } set { V_BackColor = value; this.Refresh(); } } /// This feature will paint the group title background to the specified color if PaintGroupBox is set to true. [Category("Appearance"), Description("This feature will paint the group title background to the specified color if PaintGroupBox is set to true.")] public System.Drawing.Color CustomGroupBoxColor { get { return V_CustomGroupBoxColor; } set { V_CustomGroupBoxColor = value; this.Refresh(); } } /// This feature will paint the group title background to the CustomGroupBoxColor. [Category("Appearance"), Description("This feature will paint the group title background to the CustomGroupBoxColor.")] public bool PaintGroupBox { get { return V_PaintGroupBox; } set { V_PaintGroupBox = value; this.Refresh(); } } /// This feature can add a 16 x 16 image to the group title bar. [Category("Appearance"), Description("This feature can add a 16 x 16 image to the group title bar.")] public System.Drawing.Image GroupImage { get { return V_GroupImage; } set { V_GroupImage = value; this.Refresh(); } } /// This feature will change the control's shadow color. [Category("Appearance"), Description("This feature will change the control's shadow color.")] public System.Drawing.Color ShadowColor { get { return V_ShadowColor; } set { V_ShadowColor = value; this.Refresh(); } } /// This feature will change the size of the shadow border. [Category("Appearance"), Description("This feature will change the size of the shadow border.")] public int ShadowThickness { get { return V_ShadowThickness; } set { if (value > 10) { V_ShadowThickness = 10; } else { if (value < 1) { V_ShadowThickness = 1; } else { V_ShadowThickness = value; } } this.Refresh(); } } /// This feature will change the group control color. This color can also be used in combination with BackgroundGradientColor for a gradient paint. [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.")] public System.Drawing.Color BackgroundColor { get { return V_BackgroundColor; } set { V_BackgroundColor = value; this.Refresh(); } } /// This feature can be used in combination with BackgroundColor to create a gradient background. [Category("Appearance"), Description("This feature can be used in combination with BackgroundColor to create a gradient background.")] public System.Drawing.Color BackgroundGradientColor { get { return V_BackgroundGradientColor; } set { V_BackgroundGradientColor = value; this.Refresh(); } } /// This feature turns on background gradient painting. [Category("Appearance"), Description("This feature turns on background gradient painting.")] public GroupBoxGradientMode BackgroundGradientMode { get { return V_BackgroundGradientMode; } set { V_BackgroundGradientMode = value; this.Refresh(); } } /// This feature will round the corners of the control. [Category("Appearance"), Description("This feature will round the corners of the control.")] public int RoundCorners { get { return V_RoundCorners; } set { if (value > 25) { V_RoundCorners = 25; } else { if (value < 1) { V_RoundCorners = 1; } else { V_RoundCorners = value; } } this.Refresh(); } } /// This feature will add a group title to the control. [Category("Appearance"), Description("This feature will add a group title to the control.")] public string GroupTitle { get { return V_GroupTitle; } set { V_GroupTitle = value; this.Refresh(); } } /// This feature will allow you to set the control's border size. [Category("Appearance"), Description("This feature will allow you to set the control's border size.")] public float BorderThickness { get { return V_BorderThickness; } set { if (value > 3) { V_BorderThickness = 3; } else { if (value < 1) { V_BorderThickness = 1; } else { V_BorderThickness = value; } } this.Refresh(); } } /// This feature will allow you to turn on control shadowing. [Category("Appearance"), Description("This feature will allow you to turn on control shadowing.")] public bool ShadowControl { get { return V_ShadowControl; } set { V_ShadowControl = value; this.Refresh(); } } #endregion public AMT_GroupBox() { InitializeComponent(); } protected override void OnPaint(PaintEventArgs e) { SizeF textsize = e.Graphics.MeasureString(this.Text, this.Font); PaintBack(e.Graphics); Rectangle GBtextRectangle = e.ClipRectangle; GBtextRectangle.Width = (int)textsize.Width + 5; GBtextRectangle.Height = (int)textsize.Height; if (this.RightToLeft == RightToLeft.Yes) { GBtextRectangle.X = this.Width - 10 - GBtextRectangle.Width; } else { GBtextRectangle.X = 10; } GBtextRectangle.Y = 0; if (this.Width < GBtextRectangle.Width + 15) { this.Width = GBtextRectangle.Width + 15; } e.Graphics.FillRectangle(new SolidBrush(this.BackColor), GBtextRectangle); if (this.RightToLeft == RightToLeft.Yes) { StringFormat sf = new StringFormat(StringFormatFlags.DirectionRightToLeft); e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), GBtextRectangle.X + GBtextRectangle.Width, GBtextRectangle.Y, sf); } else { e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), GBtextRectangle.X, GBtextRectangle.Y); } } protected override void OnGotFocus(EventArgs e) { base.OnGotFocus(e); } protected override void OnLostFocus(EventArgs e) { base.OnLostFocus(e); } public AMT_GroupBox(IContainer container) { container.Add(this); InitializeComponent(); } private void PaintBack(System.Drawing.Graphics graphics) { // parts of the code are copied from code project : http://www.codeproject.com/KB/miscctrl/grouper.aspx?display=Print //Set Graphics smoothing mode to Anit-Alias-- graphics.SmoothingMode = SmoothingMode.AntiAlias; //------------------------------------------- //Declare Variables------------------ int ArcWidth = this.RoundCorners * 2; int ArcHeight = this.RoundCorners * 2; int ArcX1 = 0; int ArcX2 = (this.ShadowControl) ? (this.Width - (ArcWidth + 1)) - this.ShadowThickness : this.Width - (ArcWidth + 1); int ArcY1 = 10; int ArcY2 = (this.ShadowControl) ? (this.Height - (ArcHeight + 1)) - this.ShadowThickness : this.Height - (ArcHeight + 1); System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath(); System.Drawing.Brush BorderBrush = new SolidBrush(this.BorderColor); System.Drawing.Pen BorderPen = new Pen(BorderBrush, this.BorderThickness); System.Drawing.Drawing2D.LinearGradientBrush BackgroundGradientBrush = null; System.Drawing.Brush BackgroundBrush = new SolidBrush(this.BackgroundColor); System.Drawing.SolidBrush ShadowBrush = null; System.Drawing.Drawing2D.GraphicsPath ShadowPath = null; //Create Rounded Rectangle Path------ path.AddArc(ArcX1, ArcY1, ArcWidth, ArcHeight, 180, 90); // Top Left path.AddArc(ArcX2, ArcY1, ArcWidth, ArcHeight, 270, 90); //Top Right path.AddArc(ArcX2, ArcY2, ArcWidth, ArcHeight, 360, 90); //Bottom Right path.AddArc(ArcX1, ArcY2, ArcWidth, ArcHeight, 90, 90); //Bottom Left path.CloseAllFigures(); //----------------------------------- //Check if Gradient Mode is enabled-- if (this.BackgroundGradientMode == GroupBoxGradientMode.None) { //Paint Rounded Rectangle------------ graphics.FillPath(BackgroundBrush, path); //----------------------------------- } else { BackgroundGradientBrush = new LinearGradientBrush(new Rectangle(0, 0, this.Width, this.Height), this.BackgroundColor, this.BackgroundGradientColor, (LinearGradientMode)this.BackgroundGradientMode); //Paint Rounded Rectangle------------ graphics.FillPath(BackgroundGradientBrush, path); //----------------------------------- } //----------------------------------- //Paint Borded----------------------- graphics.DrawPath(BorderPen, path); //----------------------------------- //Destroy Graphic Objects------------ if (path != null) { path.Dispose(); } if (BorderBrush != null) { BorderBrush.Dispose(); } if (BorderPen != null) { BorderPen.Dispose(); } if (BackgroundGradientBrush != null) { BackgroundGradientBrush.Dispose(); } if (BackgroundBrush != null) { BackgroundBrush.Dispose(); } if (ShadowBrush != null) { ShadowBrush.Dispose(); } if (ShadowPath != null) { ShadowPath.Dispose(); } //----------------------------------- } } }