C # modify the border color of the Form Control GroupBox,

Source: Internet
Author: User

C # modify the border color of the Form Control GroupBox,

The default border color of the control Group Box is white, which is usually less prominent. However, the default attribute list does not provide corresponding interfaces. Therefore, you can only redraw events.

The OnPaint event is widely used on the Internet, but it is not found in the event list. It should be hidden too deeply (it needs to be rewritten with the override keyword ). I used the Paint event directly here, and it can also achieve its effect.

Thanks http://blog.csdn.net/haoduo123456789001/article/details/51083223

    public partial class TestForm : Form    {        public TestForm()        {            InitializeComponent();            this.groupBox1.Paint += groupBox_Paint;            this.groupBox2.Paint += groupBox_Paint;        }        void groupBox_Paint(object sender, PaintEventArgs e)        {            GroupBox gBox = (GroupBox)sender;            e.Graphics.Clear(gBox.BackColor);            e.Graphics.DrawString(gBox.Text, gBox.Font, Brushes.Red, 10, 1);            var vSize = e.Graphics.MeasureString(gBox.Text, gBox.Font);            e.Graphics.DrawLine(Pens.Red, 1, vSize.Height / 2, 8, vSize.Height / 2);            e.Graphics.DrawLine(Pens.Red, vSize.Width + 8, vSize.Height / 2, gBox.Width - 2, vSize.Height / 2);            e.Graphics.DrawLine(Pens.Red, 1, vSize.Height / 2, 1, gBox.Height - 2);            e.Graphics.DrawLine(Pens.Red, 1, gBox.Height - 2, gBox.Width - 2, gBox.Height - 2);            e.Graphics.DrawLine(Pens.Red, gBox.Width - 2, vSize.Height / 2, gBox.Width - 2, gBox.Height - 2);         }        private void TestForm_Load(object sender, EventArgs e)        {                    }    }

 

:

 

Of course, if you want to, please refer to the following method using OnPaint.

First, you need to create your own components (similar to custom controls ):

After adding the code, switch to "Code view ".

Modify the inheritance relationship:

Public partial class MyGroupBox: GroupBox // Component {public MyGroupBox () {InitializeComponent ();} public MyGroupBox (IContainer container) {container. Add (this); InitializeComponent ();}
// Rewrite}

Then rewrite the OnPaint () method:

// Override protected override void OnPaint (PaintEventArgs e) {var vSize = e. graphics. measureString (this. text, this. font); e. graphics. clear (this. backColor); e. graphics. drawString (this. text, this. font, new SolidBrush (this. foreColor), 10, 1); e. graphics. drawLine (Pens. black, 1, vSize. height/2, 8, vSize. height/2); e. graphics. drawLine (Pens. black, vSize. width + 8, vSize. height/2, this. width-2, vSize. height/2); e. graphics. drawLine (Pens. black, 1, vSize. height/2, 1, this. height-2); e. graphics. drawLine (Pens. black, 1, this. height-2, this. width-2, this. height-2); e. graphics. drawLine (Pens. black, this. width-2, vSize. height/2, this. width-2, this. height-2 );}

Press F6 to generate it, and you can find it in the toolbar. Then, you don't need to use the previous GroupBox. Once and for all

You can even expose the border color by using the control property.

Public partial class MyGroupBox: GroupBox // Component {private Color mBorderColor = Color. Black;
[Browsable (true), Description ("border Color"), Category ("Custom group")] public Color BorderColor {get {return mBorderColor ;} set {mBorderColor = value ;}} public MyGroupBox () {InitializeComponent ();} public MyGroupBox (IContainer container) {container. add (this); InitializeComponent () ;}// rewrite protected override void OnPaint (PaintEventArgs e) {var vSize = e. graphics. measureString (this. text, this. font); e. graphics. clear (this. backColor); e. graphics. drawString (this. text, this. font, new SolidBrush (this. foreColor), 10, 1); Pen vPen = new Pen (this. mBorderColor); // use the attribute color to draw the border color e. graphics. drawLine (vPen, 1, vSize. height/2, 8, vSize. height/2); e. graphics. drawLine (vPen, vSize. width + 8, vSize. height/2, this. width-2, vSize. height/2); e. graphics. drawLine (vPen, 1, vSize. height/2, 1, this. height-2); e. graphics. drawLine (vPen, 1, this. height-2, this. width-2, this. height-2); e. graphics. drawLine (vPen, this. width-2, vSize. height/2, this. width-2, this. height-2 );}}

 

 

[Http://www.cnblogs.com/CUIT-DX037/]

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.