天天看點

winform==>GroupBox 自定義控件

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Drawing;

using System.Data;

using System.Text;

using System.Windows.Forms;

using System.Drawing.Drawing2D;

namespace WindowsApplication1

{

    public partial class GroupBox : UserControl

    {

        public GroupBox()

        {

            InitializeComponent();

            InitializeStyles();

            InitializeGroupBox();

        }

        /// <summary>This method will initialize the controls custom styles.</summary>

        private void InitializeStyles()

        {

            //Set the control styles----------------------------------

            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);

        }

         protected override void OnPaint(PaintEventArgs e)

        {

            //PaintBack(e.Graphics);

            Test(e.Graphics);

        }

        public void Test(Graphics g)

        {

            g.SmoothingMode = SmoothingMode.AntiAlias;

            int ArcX2 = (this.ShadowControl) ? (this.Width - (15)) - this.ShadowThickness : this.Width - (15);

            int ArcY2 = (this.ShadowControl) ? (this.Height - (15)) - this.ShadowThickness : this.Height - (15);

            System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();

            System.Drawing.Brush BorderBrush = new SolidBrush(Color.FromArgb(224, 224, 224));

            System.Drawing.Pen BorderPen = new Pen(BorderBrush, 1);

           // 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(0, 10, 14, 14, 180, GroupBoxConstants.SweepAngle); // Top Left

            path.AddArc(ArcX2, 10, 14, 14, 270, GroupBoxConstants.SweepAngle); //Top Right

            path.AddArc(ArcX2, ArcY2, 14, 14, 360, GroupBoxConstants.SweepAngle); //Bottom Right

            path.AddArc(0, ArcY2, 14, 14, 90, GroupBoxConstants.SweepAngle); //Bottom Left

            path.CloseAllFigures();

            //-----------------------------------

            Check if Gradient Mode is enabled--

            //if (this.BackgroundGradientMode == GroupBoxGradientMode.None)

            //{

            //    //Paint Rounded Rectangle------------

            //    g.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------------

            //    g.FillPath(BackgroundGradientBrush, path);

            //    //-----------------------------------

            //}

            -----------------------------------

            //Paint Borded-----------------------

            g.DrawPath(BorderPen, path);

            //-----------------------------------

        }

        private int V_RoundCorners = 10;

        /// <summary>This feature will round the corners of the control.</summary>

        [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();

            }

        }

        /// <summary>This feature will allow you to turn on control shadowing.</summary>

        [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(); } }

        private bool V_ShadowControl = false;

        /// <summary>This feature will change the size of the shadow border.</summary>

        [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();

            }

        }

        private int V_ShadowThickness = 3;

        private System.Drawing.Color V_BorderColor = Color.Black;

        /// <summary>This feature will change the group control color. This color can also be used in combination with BackgroundGradientColor for a gradient paint.</summary>

        [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(); } }

        private System.Drawing.Color V_BackgroundColor = Color.White;

        /// <summary>This class holds all GroupBox constants.</summary>

        public class GroupBoxConstants

        {

            #region Constants

            /// <summary>The sweep angle of the arc.</summary>

            public const int SweepAngle = 90;

            /// <summary>The minimum control height.</summary>

            public const int MinControlHeight = 32;

            /// <summary>The minimum control width.</summary>

            public const int MinControlWidth = 96;

            #endregion

        }

        /// <summary>This feature turns on background gradient painting.</summary>

        [Category("Appearance"), Description("This feature turns on background gradient painting.")]

        public GroupBoxGradientMode BackgroundGradientMode { get { return V_BackgroundGradientMode; } set { V_BackgroundGradientMode = value; this.Refresh(); } }

        private GroupBoxGradientMode V_BackgroundGradientMode = GroupBoxGradientMode.None;

        /// <summary>A special gradient enumeration.</summary>

        public enum GroupBoxGradientMode

        {

            /// <summary>Specifies no gradient mode.</summary>

            None = 4,

            /// <summary>Specifies a gradient from upper right to lower left.</summary>

            BackwardDiagonal = 3,

            /// <summary>Specifies a gradient from upper left to lower right.</summary>

            ForwardDiagonal = 2,

            /// <summary>Specifies a gradient from left to right.</summary>

            Horizontal = 0,

            /// <summary>Specifies a gradient from top to bottom.</summary>

            Vertical = 1

        }

        /// <summary>This feature will allow you to change the color of the control's border.</summary>

        [Category("Appearance"), Description("This feature will allow you to change the color of the control's border.")]

        public System.Drawing.Color BorderColor { get { return V_BorderColor; } set { V_BorderColor = value; this.Refresh(); } }

        /// <summary>This feature will allow you to set the control's border size.</summary>

        [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();

            }

        }

        private float V_BorderThickness = 1;

        /// <summary>This feature will change the control's shadow color.</summary>

        [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(); } }

        private System.Drawing.Color V_ShadowColor = Color.DarkGray;

        /// <summary>This feature can be used in combination with BackgroundColor to create a gradient background.</summary>

        [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(); } }

        private System.Drawing.Color V_BackgroundGradientColor = Color.White;

        /// <summary>This method will paint the control.</summary>

        /// <param name="g">The paint event graphics object.</param>

        private void PaintBack(System.Drawing.Graphics g)

        {

            //Set Graphics smoothing mode to Anit-Alias--

            g.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;

            //-----------------------------------

            //Check if shadow is needed----------

            if (this.ShadowControl)

            {

                ShadowBrush = new SolidBrush(this.ShadowColor);

                ShadowPath = new System.Drawing.Drawing2D.GraphicsPath();

                ShadowPath.AddArc(ArcX1 + this.ShadowThickness, ArcY1 + this.ShadowThickness, ArcWidth, ArcHeight, 180, GroupBoxConstants.SweepAngle); // Top Left

                ShadowPath.AddArc(ArcX2 + this.ShadowThickness, ArcY1 + this.ShadowThickness, ArcWidth, ArcHeight, 270, GroupBoxConstants.SweepAngle); //Top Right

                ShadowPath.AddArc(ArcX2 + this.ShadowThickness, ArcY2 + this.ShadowThickness, ArcWidth, ArcHeight, 360, GroupBoxConstants.SweepAngle); //Bottom Right

                ShadowPath.AddArc(ArcX1 + this.ShadowThickness, ArcY2 + this.ShadowThickness, ArcWidth, ArcHeight, 90, GroupBoxConstants.SweepAngle); //Bottom Left

                ShadowPath.CloseAllFigures();

                //Paint Rounded Rectangle------------

                g.FillPath(ShadowBrush, ShadowPath);

                //-----------------------------------

            }

            //-----------------------------------

            //Create Rounded Rectangle Path------

            path.AddArc(ArcX1, ArcY1, ArcWidth, ArcHeight, 180, GroupBoxConstants.SweepAngle); // Top Left

            path.AddArc(ArcX2, ArcY1, ArcWidth, ArcHeight, 270, GroupBoxConstants.SweepAngle); //Top Right

            path.AddArc(ArcX2, ArcY2, ArcWidth, ArcHeight, 360, GroupBoxConstants.SweepAngle); //Bottom Right

            path.AddArc(ArcX1, ArcY2, ArcWidth, ArcHeight, 90, GroupBoxConstants.SweepAngle); //Bottom Left

            path.CloseAllFigures();

            //-----------------------------------

            //Check if Gradient Mode is enabled--

            if (this.BackgroundGradientMode == GroupBoxGradientMode.None)

            {

                //Paint Rounded Rectangle------------

                g.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------------

                g.FillPath(BackgroundGradientBrush, path);

                //-----------------------------------

            }

            //-----------------------------------

            //Paint Borded-----------------------

            g.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(); }

            //-----------------------------------

        }

        private void GroupBox_Resize(object sender, EventArgs e)

        {

            this.Invalidate(false);

        }

    }

}

編譯後在工具箱生成控件直接拖到窗體上使用就行。

忘了修改哪裡的代碼了。應該是codeproject上面的、

另外一種:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Drawing;

using System.Data;

using System.Text;

using System.Windows.Forms;

using System.Drawing.Drawing2D;

namespace WindowsApplication1

{

    public partial class UserControl2 : UserControl

    {

        public UserControl2()

        {

            InitializeComponent();

            InitializeStyles();

        }

        /// <summary>This method will initialize the controls custom styles.</summary>

        private void InitializeStyles()

        {

            //Set the control styles----------------------------------

            this.SetStyle(ControlStyles.DoubleBuffer, true);

            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);

            this.SetStyle(ControlStyles.UserPaint, true);

            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);

            //--------------------------------------------------------

        }

        protected override void OnPaint(PaintEventArgs e)

        {

            //PaintBack(e.Graphics);

            Test(e.Graphics);

        }

        public void Test(Graphics g)

        {

            g.SmoothingMode = SmoothingMode.AntiAlias;

            System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();

            System.Drawing.Brush BorderBrush = new SolidBrush(Color.FromArgb(224, 224, 224));

            System.Drawing.Pen BorderPen = new Pen(BorderBrush, 1);

            //Create Rounded Rectangle Path------

            path.AddArc(0, 10, 14, 14, 180, 90); // Top Left

            path.AddArc(this.Width - (15), 10, 14, 14, 270, 90); //Top Right

            path.AddArc(this.Width - (15), this.Height - (15), 14, 14, 360, 90); //Bottom Right

            path.AddArc(0, this.Height - (15), 14, 14, 90, 90); //Bottom Left

            path.CloseAllFigures();

            //-----------------------------------

            //Paint Borded-----------------------

            g.DrawPath(BorderPen, path);

            //-----------------------------------

            //Destroy Graphic Objects------------

            if (path != null) { path.Dispose(); }

            if (BorderBrush != null) { BorderBrush.Dispose(); }

            if (BorderPen != null) { BorderPen.Dispose(); }

             //-----------------------------------

        }

        private void UserControl2_Resize(object sender, EventArgs e)

        {

            this.Invalidate(false);

        }

    }

}