using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Work.UserComponents { /// /// 可以改变边框颜色的groupbox /// public partial class UcGroupBoxEx : GroupBox { private Color _BorderColor = Color.FromArgb(33, 202, 99); private GroupBoxBorderStyle _BorderStyle = GroupBoxBorderStyle.ROUND; [Browsable(true), Description("边框颜色"), Category("自定义分组")] public Color BorderColor { get { return _BorderColor; } set { _BorderColor = value; this.Invalidate(); } } [Browsable(true), Description("边框样式"), Category("自定义分组")] public GroupBoxBorderStyle BorderStyle { get { return _BorderStyle; } set { _BorderStyle = value; this.Invalidate(); } } public UcGroupBoxEx() { InitializeComponent(); } public UcGroupBoxEx(IContainer container) { container.Add(this); InitializeComponent(); } /// /// 重写OnPaint绘制自定义效果 /// /// protected override void OnPaint(PaintEventArgs e) { e.Graphics.Clear(this.BackColor); // 绘制字体 e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit; // 或者 AntiAliasGridFit // 平滑字体 e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), 10, 1); // 计算文本大小 var vSize = e.Graphics.MeasureString(this.Text, this.Font); Pen vPen = new Pen(this._BorderColor); // 绘制矩形 if (_BorderStyle == GroupBoxBorderStyle.SQUARE) { e.Graphics.DrawLine(vPen, 2, vSize.Height / 2, 8, vSize.Height / 2); // 标题左侧 e.Graphics.DrawLine(vPen, vSize.Width + 8, vSize.Height / 2, this.Width - 3, vSize.Height / 2); // 标题右侧 e.Graphics.DrawLine(vPen, 2, vSize.Height / 2, 2, this.Height - 3); // 左侧 e.Graphics.DrawLine(vPen, 2, this.Height - 3, this.Width - 3, this.Height - 3); // 底部 e.Graphics.DrawLine(vPen, this.Width - 3, vSize.Height / 2, this.Width - 3, this.Height - 3); // 右侧 } // 绘制圆角矩形 else { e.Graphics.DrawLine(vPen, 4, vSize.Height / 2, 8, vSize.Height / 2); // 标题左侧 e.Graphics.DrawLine(vPen, vSize.Width + 8, vSize.Height / 2, this.Width - 5, vSize.Height / 2); // 标题右侧 e.Graphics.DrawLine(vPen, 2, vSize.Height / 2 + 2, 2, this.Height - 5); // 左侧 e.Graphics.DrawLine(vPen, 4, this.Height - 3, this.Width - 5, this.Height - 3); // 底部 e.Graphics.DrawLine(vPen, this.Width - 3, vSize.Height / 2 + 2, this.Width - 3, this.Height - 5); // 右侧 // 边角直线模拟圆角 e.Graphics.DrawLine(vPen, 2, vSize.Height / 2 + 2, 4, vSize.Height / 2); // 左上角 e.Graphics.DrawLine(vPen, this.Width - 5, vSize.Height / 2, this.Width - 3, vSize.Height / 2 + 2); // 右上角 e.Graphics.DrawLine(vPen, this.Width - 3, this.Height - 5, this.Width - 5, this.Height - 3); // 右下角 e.Graphics.DrawLine(vPen, 4, this.Height - 3, 2, this.Height - 5); // 左下角 } } protected override void WndProc(ref Message m) { base.WndProc(ref m); try { // WM_NCCALCSIZE 0x0083 当计算某个窗口的客户区大小和位置时发送此消息 // windows消息值表,可参考:http://hi.baidu.com/dooy/blog/item/0e770a24f70e3b2cd407421b.html if (m.Msg == 0x83) { this.Invalidate(); } } catch { } } public enum GroupBoxBorderStyle { ROUND, SQUARE, } } }