使用Paint事件重画GroupBox来修改边框颜色
发布日期:2021-10-03 22:21:33 浏览次数:11 分类:技术文章

本文共 2202 字,大约阅读时间需要 7 分钟。

.Net下进行WinForm开发,GroupBox是经常要用到的一个控件。但是GroupBox自身的边框是灰白色的,其样式很难令开发者满意。在不借用第三方控件的情况下,通过其的Paint事件对GroupBox进行重画,也可以很方便的修改其边框颜色/样式。

  简要说一下实现思路。首先用Clear方法清除GroupBox的显示,接着再用合适的样式把GroupBox画出来。把GroupBox拆分为4个圆角,一行标题文字和数段直线等元素,把这些元素摆放在合适的位置,拼凑出GroupBox的外框。定义4Rectangle(用于后面的画圆角),确定它们的大小和位置;然后在确切的位置上画4个弧段,按圆角在GroupBox上的位置确定其角度;接着在对应的位置画出GroupBox标题和几条直线,就大功告成了。只需简单修改代码中的设置颜色语句,便可以完成GroupBox边框颜色的更换了。

using System; 

usingSystem.Collections.Generic; 

usingSystem.ComponentModel; 

usingSystem.Data; 

usingSystem.Drawing; 

usingSystem.Drawing.Drawing2D; 

usingSystem.Text; 

usingSystem.Windows.Forms; 

 

namespaceWindowsApplication2 

     public partial class Form1 : Form 

     { 

         public Form1() 

         { 

             InitializeComponent(); 

         } 

         private void groupBox1_Paint(objectsender, PaintEventArgs e

         { 

            e.Graphics.Clear(groupBox1.BackColor); 

             Rectangle Rtg_LT = newRectangle(); 

             Rectangle Rtg_RT = newRectangle(); 

             Rectangle Rtg_LB = newRectangle(); 

             Rectangle Rtg_RB = newRectangle(); 

             Rtg_LT.X = 0; Rtg_LT.Y = 7; Rtg_LT.Width= 10; Rtg_LT.Height = 10; 

             Rtg_RT.X = e.ClipRectangle.Width -11; Rtg_RT.Y = 7; Rtg_RT.Width = 10; Rtg_RT.Height = 10; 

             Rtg_LB.X = 0; Rtg_LB.Y =e.ClipRectangle.Height - 11; Rtg_LB.Width = 10; Rtg_LB.Height = 10; 

            Rtg_RB.X = e.ClipRectangle.Width - 11;Rtg_RB.Y = e.ClipRectangle.Height - 11; Rtg_RB.Width = 10; Rtg_RB.Height =10; 

             Color color = Color.FromArgb(51,94, 168); 

            Pen Pen_AL = new Pen(color,1); 

             Pen_AL.Color = color; 

             Brush brush = newHatchBrush(HatchStyle.Divot, color); 

             e.Graphics.DrawString(groupBox1.Text, groupBox1.Font, brush, 6, 0); 

             e.Graphics.DrawArc(Pen_AL, Rtg_LT,180, 90); 

             e.Graphics.DrawArc(Pen_AL, Rtg_RT,270, 90); 

             e.Graphics.DrawArc(Pen_AL, Rtg_LB,90, 90); 

             e.Graphics.DrawArc(Pen_AL, Rtg_RB,0, 90); 

             e.Graphics.DrawLine(Pen_AL, 5, 7,6, 7); 

             e.Graphics.DrawLine(Pen_AL,e.Graphics.MeasureString(groupBox1.Text, groupBox1.Font).Width + 3, 7,e.ClipRectangle.Width - 7, 7); 

             e.Graphics.DrawLine(Pen_AL, 0, 13,0, e.ClipRectangle.Height - 7); 

             e.Graphics.DrawLine(Pen_AL, 6,e.ClipRectangle.Height - 1, e.ClipRectangle.Width - 7, e.ClipRectangle.Height -1); 

             e.Graphics.DrawLine(Pen_AL,e.ClipRectangle.Width - 1, e.ClipRectangle.Height - 7, e.ClipRectangle.Width -1, 13); 

         } 

     } 

 }  运行效果图如下:

  代码稍显繁琐,但也很好的实现了修改GroupBox边框样式/颜色的效果。

转载地址:https://blog.csdn.net/lc547913923/article/details/17753639 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:C# StringBuilder用法
下一篇:C#读取Excel表格中的数据

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2024年03月19日 14时11分21秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

linux 中文utf-8转gbk编码,Linux平台下 GBK编码转UTF-8编码 2019-04-21
linux安装软件在boot,在Linux系统上安装Spring boot应用的教程详解 2019-04-21
linux进入用户user1主目录,Linux系统命令提示符为[user1@localhost root]当前用户所在目录为( )... 2019-04-21
取消linux自动登录,linuxdeepin 如何取消自动登录啊? 2019-04-21
linux线程存储,Linux系统编程手册:线程:线程安全和每线程存储 2019-04-21
linux以root账号登陆gnome,CentOS 7 - 以root身份登入Gnome 2019-04-21
linux crontab 备份数据库 空文件,Linux下使用crontab自动备份数据库 2019-04-21
linux批处理模式,巧用linux-top的批处理模式 2019-04-21
linux信号量机制例题,第二章 信号量机制及几个经典例题 2019-04-21
linux ba 模拟,在你的 Python 游戏中模拟引力 | Linux 中国 2019-04-21
c语言表达式3649的值是,535个C语言经典实例目录.doc 2019-04-21
c语言Wndproc未定义,小弟我用c语言写了一个windows窗口,为什么有提示未定义的变量类型... 2019-04-21
c语言中malloc数组,如何在C中对malloc()数组进行一行赋值? 2019-04-21
c语言调存储过程,写留言板–调用存储过程出问题 2019-04-21
c语言编程max,C语言编程题及答案.doc 2019-04-21
android测试页面,自动执行界面测试 | Android 开发者 | Android Developers 2019-04-21
android 图片点击变色,Android开发实现ListView点击item改变颜色功能示例 2019-04-21
android增删改查布局,Android之父_增删改查 2019-04-21
vowifi android开关,如何配置VoLTE, ViLTE and VoWifi(IMS config for VoLTE, ViLTE and VoWifi) 2019-04-21
电脑端的mafsvr服务关掉_网吧才是电脑优化的精髓!学会3招你也不用羡慕网吧的流畅了... 2019-04-21