
自绘GroupBox控件
发布日期:2021-05-14 04:37:15
浏览次数:12
分类:博客文章
本文共 6365 字,大约阅读时间需要 21 分钟。
下面显示的是效果图:
然后是代码,其中有一些解释:
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 scyGroupBox { public partial class scyGroupBox : GroupBox { private GraphicsPath gpRegion = null; private bool _mouseHover = false; private bool _mouseDown = false; private int textHeight = 0; //文本高度 public scyGroupBox() { InitializeComponent(); BackColor = Color.YellowGreen; gpRegion = CreateRadiusRect(this.ClientRectangle, radius); if (gpRegion != null) { this.Region = new Region(gpRegion); } this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer | ControlStyles.UserPaint, true); } #region 圆角大小 Property private int radius = 3; public int Radius { get { return radius; } set { if (value < 0) { return; } else { radius = value; } } } #endregion #region 边线颜色 private Color borderColor = Color.DarkGreen; public Color BorderColor { get { return borderColor; } set { borderColor = value; } } #endregion #region 上部渐变颜色 private Color uShineColor = Color.FromArgb(140, Color.White); public Color UShineColor { get { return uShineColor; } set { uShineColor = value; } } #endregion ////// 描绘具有圆角的button原型路径 /// protected virtual GraphicsPath CreateRadiusRect(Rectangle rectangle, int radius) { GraphicsPath path = new GraphicsPath(); int l = rectangle.Left; //左边 int t = rectangle.Top; //上边 int w = rectangle.Width; //宽度 int h = rectangle.Height;// 高度 int d = radius / 2; //除以2,得到半径 path.AddArc(l,t,d,d,180,90); //左上角 path.AddLine(l + radius, t, l + w - radius, t);//上边线 path.AddArc(l + w - d, t, d, d, 270, 90);//右上角 path.AddLine(l + w, t + radius, l + w, t + h - radius); //右边线 path.AddArc(l + w - d, t + h - d, d, d, 0, 90);//右下角 path.AddLine(l + w - radius, t + h, l + radius, t + h); //下边线 path.AddArc(l, t + h - d, d, d, 90, 90); //左下角 path.AddLine(l, t + h - radius, l, t + radius); //左边线 path.CloseFigure(); return path; } protected override void OnSizeChanged(EventArgs e) { gpRegion = CreateRadiusRect(this.ClientRectangle, radius); if (gpRegion != null) { this.Region = new Region(gpRegion); } base.OnSizeChanged(e); } //按照button路径进行border绘制 protected virtual void DrawBorder(Graphics g, Pen pen) { if (gpRegion != null) { g.SmoothingMode = SmoothingMode.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.DrawPath(pen, CreateRadiusRect(new Rectangle(this.ClientRectangle.Left, this.ClientRectangle.Top, this.ClientRectangle.Width - 1, this.ClientRectangle.Height - 1), radius)); g.SmoothingMode = SmoothingMode.Default; g.InterpolationMode = InterpolationMode.Default; } pen.Dispose(); pen = null; } //绘制上部渐变色 protected virtual void DrawUpperShine(Graphics g) { Color c1 = Color.FromArgb(200,uShineColor); Color c2 = Color.FromArgb(100,Color.White); Rectangle rc = new Rectangle(this.ClientRectangle.Left, this.ClientRectangle.Top, Width - 1, Height / 2); if (_mouseDown) { rc = new Rectangle(this.ClientRectangle.Left, this.ClientRectangle.Top, Width - 1, (Height - 10) / 2); } using (LinearGradientBrush lbrush = new LinearGradientBrush(rc, c1, c2, LinearGradientMode.Vertical)) { g.FillRectangle(lbrush,rc); } } //设置字体的大小,宽高,位置信息 protected virtual void DrawText(Graphics g) { if (!String.IsNullOrEmpty(this.Text.Trim())) { int x = 0; int y = 0; int w = 0; int h = 0; Size size = g.MeasureString(Text, Font).ToSize(); w = size.Width > Width ? Width : size.Width; h = size.Height > Height ? Height : size.Height; Pen pen = new Pen(ForeColor); g.DrawString(Text, Font, pen.Brush, new RectangleF((float)x + 10, (float)y + 3, (float)w + 3, (float)h)); pen.Dispose(); pen = null; textHeight = w; } } //绘制内部的方块 protected virtual void DrawInner(Graphics g, Rectangle rectangle, Pen p) { int l = rectangle.Left; //左边 int t = rectangle.Top;//上边 int w = rectangle.Width;//宽度 int h = rectangle.Height;//高度 g.DrawLine(p, l + 10, t + 20, l + w - 10, t + 20);//上边线 g.DrawLine(p, l + w - 10, t + 20, l + w - 10, t + h - 10); //右边线 g.DrawLine(p, l + w - 10, t + h - 10, l + 10, t + h - 10); //下边线 g.DrawLine(p, l + 10, t + h - 10, l + 10, t + 20); //左边线 Rectangle r = new Rectangle(l + 10, t + 20, w - 18, h - 28); g.FillRectangle(new SolidBrush(Color.Wheat), r); } protected override void OnPaint(PaintEventArgs e) { Pen pen = new Pen(borderColor); Brush brush = new SolidBrush(BackColor); e.Graphics.FillPath(brush, gpRegion); DrawText(e.Graphics); DrawBorder(e.Graphics,pen); DrawInner(e.Graphics,this.ClientRectangle,new Pen(Color.Red)); DrawUpperShine(e.Graphics); } } }
后续做详细的解释。
发表评论
最新留言
留言是一种美德,欢迎回访!
[***.207.175.100]2025年04月07日 15时23分46秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Adding Powers
2019-03-12
ideal 下创建springboot项目
2019-03-12
Linux操作系统的安装与使用
2019-03-12
ajax请求出现/[object%20Object]错误的解决办法
2019-03-12
01背包(小偷的概率)
2019-03-12
流体运动估计光流算法研究
2019-03-12
如何转载博客
2019-03-12
Burpsuite工具的证书安装
2019-03-12
C++ 继承 详解
2019-03-12
OSPF多区域
2019-03-12
Grafana导入 Promethus node模板
2019-03-12
MySQL的操作
2019-03-12
算术运算符
2019-03-12
MySQL学习之《查询数据》
2019-03-12
如何提高SQL查询的效率?
2019-03-12
Docker入门之-镜像(二)
2019-03-12
设置canvas图作为背景图?亲测有效
2019-03-12
搭建Docker本地 Registry
2019-03-12
数据结构——链表(3)
2019-03-12
32位机器与64位机器在编程方面的差别
2019-03-12