using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Lskj.Control { public partial class LabelGroupBox : GroupBox { //边框和标题颜色 private Color GroupBoxColor = Color.Black; [Browsable(true), Description("边框颜色"), Category("自定义分组")] public Color BorderColor { get { return GroupBoxColor; } set { GroupBoxColor = value; } } //标题位置 //left = 1, //Center = 2, //right = 3, private int Position = 1; public int TitlePosition { get { return Position; } set { Position = value; } } public LabelGroupBox() { InitializeComponent(); } public LabelGroupBox(IContainer container) { container.Add(this); InitializeComponent(); } // 重写 把控件重新绘制 protected override void OnPaint(PaintEventArgs e) { var vSize = e.Graphics.MeasureString(this.Text, this.Font); //e.Graphics.Clear(this.BackColor);//这条代码会把给它的颜色设成默认和透明状态的背景样式 Pen vPen = new Pen(this.GroupBoxColor); // 用属性颜色来画边框颜色 string groupBoxColorHtml= ColorTranslator.ToHtml(System.Drawing.Color.FromArgb(this.GroupBoxColor.R, this.GroupBoxColor.G, this.GroupBoxColor.B)); string BlackBoxColorHtml = ColorTranslator.ToHtml(System.Drawing.Color.FromArgb(Color.Black.R, Color.Black.G, Color.Black.B)); if (groupBoxColorHtml == BlackBoxColorHtml) { vPen = new Pen(Color.FromArgb(220,220,220)); } if (Position == 2) { e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.GroupBoxColor), (this.Width - vSize.Width) / 2, 1);//标题 e.Graphics.DrawLine(vPen, 1, vSize.Height / 2, (this.Width - vSize.Width) / 2, vSize.Height / 2);//上左 e.Graphics.DrawLine(vPen, (this.Width - vSize.Width) / 2 + vSize.Width, 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);//右 } else if (Position == 3) { e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.GroupBoxColor), this.Width - vSize.Width - 2, 1);//标题 e.Graphics.DrawLine(vPen, 1, vSize.Height / 2, this.Width - vSize.Width - 2, vSize.Height / 2);//上左 e.Graphics.DrawLine(vPen, this.Width - 4, 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);//右 } else { e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.GroupBoxColor), 10, 1);//标题 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);//右 } } } }