C#winform之自定义按钮形状(初级版)
一、前情回顾
小猿人在2020-08-13 00:59:53发过一篇没写完的博客,
如今突然想起,
想码码
也好不留遗憾,
就像爱一个姑娘,
总感觉爱得不够
总感觉爱得不深
时间很短
等待很长
岁月不饶程序人
啊~~~~
也罢,开始码代码:
二、实现效果
注:蓝色的roundButton1和红色showButton1
三、实现过程
1:具体实现如下:
原理:通过继承Button类,重写OnPaint()方法,使用画笔工具重绘控件样式。
public class myButton : System.Windows.Forms.Button{private Color enterForeColor = Color.White;private Color leftForeColor = Color.Black;private bool Isleft = true;public bool IsLEFT{get { return Isleft; }set{this.Isleft = value;}}public Color EnterForeColor{get { return enterForeColor; }set{this.enterForeColor = value;this.ForeColor = value;}}public Color LeftForeColor{get { return leftForeColor; }set{this.leftForeColor = value;this.ForeColor = value;}}[DefaultValue(typeof(Color), "51, 161, 224")]// [DefaultValue(typeof(Color), "220, 80, 80")]// [DefaultValue(typeof(Color), "251, 161, 0")]protected override void OnMouseEnter(EventArgs e)//鼠标进入时{base.OnMouseEnter(e);this.ForeColor = this.EnterForeColor;}protected override void OnMouseLeave(EventArgs e)//鼠标离开{base.OnMouseLeave(e);this.ForeColor = this.LeftForeColor;}protected override void OnPaint(System.Windows.Forms.PaintEventArgs e){base.OnPaint(e);base.OnPaintBackground(e);e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;e.Graphics.CompositingQuality = CompositingQuality.HighQuality;e.Graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);var path = GetRoundedRectPath(rect);this.Region = new Region(path);//var pa = RectPath(rect);//this.Region = new Region(pa);Color baseColor = this.BackColor;using (SolidBrush b = new SolidBrush(baseColor)){e.Graphics.FillPath(b, path);//e.Graphics.FillPath(b, pa);System.Drawing.Font fo = new System.Drawing.Font(this.Font.Name, this.Font.Size);Brush brush = new SolidBrush(this.ForeColor);Pen penn = new Pen(brush, 3);StringFormat gs = new StringFormat();gs.Alignment = StringAlignment.Center; //居中gs.LineAlignment = StringAlignment.Center;//垂直居中e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;e.Graphics.DrawString(this.Text, fo, brush, rect, gs);}}private GraphicsPath RectPath(Rectangle re){GraphicsPath path = new GraphicsPath();Point[] ps = new Point[4];ps[0] = new Point(this.Width / 5, this.Height / 5);ps[1] = new Point(4 * this.Width / 5, this.Height / 5);ps[2] = new Point(this.Width / 5, 4 * this.Height / 5);ps[3] = new Point(4 * this.Width / 5, 4 * this.Height / 5);path.AddLines(ps);path.CloseFigure();return path;}private GraphicsPath GetRoundedRectPath(Rectangle rect){Rectangle arcRect = new Rectangle(rect.Location, new System.Drawing.Size(this.Height, this.Height));GraphicsPath path = new GraphicsPath();Point[] p = new Point[12];if (Isleft == true){p[0] = new Point(2 * this.Width / 5, 0);p[1] = new Point(0, this.Height / 2);p[2] = new Point(2 * this.Width / 5, this.Height);p[3] = new Point(this.Width, 0);p[4] = new Point(4 * this.Width / 5, this.Height / 4);p[5] = new Point(4 * this.Width / 5, 3 * this.Height / 4);p[6] = new Point(this.Width, this.Height);}else{p[0] = new Point(3 * this.Width / 5, 0);p[1] = new Point(this.Width, this.Height / 2);p[2] = new Point(3 * this.Width / 5, this.Height);p[3] = new Point(0, 0);p[4] = new Point(1 * this.Width / 5, this.Height / 4);p[5] = new Point(1 * this.Width / 5, 3 * this.Height / 4);p[6] = new Point(0, this.Height);}path.AddLine(p[0], p[1]);path.AddLine(p[1], p[2]);path.AddBezier(p[6], p[5], p[4], p[3]);path.CloseFigure();return path;}}
注意:以上是实现如图showButton5所示形状的控件,以下是实现roundButton1圆角按钮
2::同样,我们可以更改OnPaint()的内容,将控件改成圆角形状,这里我们需要给自定义控件添加一个新属性radius,表示圆角曲度,曲度越大控件越趋近于圆形。
属性设置:
private int radius;//半径
public int Radius{set{radius = value;this.Invalidate();}get{return radius;}}
3:重新更改的OnPaint():
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e){base.OnPaint(e);base.OnPaintBackground(e);e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;e.Graphics.CompositingQuality = CompositingQuality.HighQuality;e.Graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);var path = GetRoundedRectPath(rect, radius);this.Region = new Region(path);using (SolidBrush b = new SolidBrush(this.backColor)){e.Graphics.FillPath(b, path);System.Drawing.Font fo = new System.Drawing.Font(this.Font.Name,this.Font.Size);Brush brush = new SolidBrush(this.ForeColor);StringFormat gs = new StringFormat();gs.Alignment = StringAlignment.Center; //居中gs.LineAlignment = StringAlignment.Center;//垂直居中e.Graphics.DrawString(this.Text, fo, brush, rect, gs);}}private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius){int diameter = radius;Rectangle arcRect = new Rectangle(rect.Location, new System.Drawing.Size(diameter, diameter));GraphicsPath path = new GraphicsPath();path.AddArc(arcRect, 180, 90);arcRect.X = rect.Right - diameter;path.AddArc(arcRect, 270, 90);arcRect.Y = rect.Bottom - diameter;path.AddArc(arcRect, 0, 90);arcRect.X = rect.Left;path.AddArc(arcRect, 90, 90);path.CloseFigure();return path;}
程序写到这样,也算是到此为止了吧!
everybody,明天见!
C#winform之自定义按钮形状(初级版)相关推荐
- 视频教程-Word2016经典视频教程-初级版-Office/WPS
Word2016经典视频教程-初级版 Office培训讲师,51CTO金牌讲师,从2005开始从事Office培训至今.擅长Excel.Word.PowerPoint等软件的应用,著有<Powe ...
- Word2016经典视频教程-初级版-曾贤志-专题视频课程
Word2016经典视频教程-初级版-2108人已学习 课程介绍 Microsoft Office Word 从来都是流行的文字处理程序.本课程详细讲解了Word的文字排版功能,表格排 ...
- Winform中自定义xml配置文件后对节点进行读取与写入
场景 Winform中自定义xml配置文件,并配置获取文件路径: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100522648 ...
- WPF 自定义 MessageBox (相对完善版 v1.0.0.6)
基于WPF的自定义 MessageBox. 众所周知WPF界面美观.大多数WPF元素都可以简单的修改其样式,从而达到程序的风格统一.可是当你不得不弹出一个消息框通知用户消息时(虽然很不建议在程序中频繁 ...
- tkinter模块高级操作(一)—— 透明按钮、透明文本框、自定义按钮及自定义文本框
[写在前面] -- 众所周知,tkinter模块中自带的Button类是无法做到使其透明的(至少我无法做到) [tip:透明是指让背景颜色或图片穿过按钮而显示出来] -- 找遍了Button类的所有参 ...
- Android按钮设计,自定义按钮,圆形按钮,颜色
In this tutorial, we'll be customizing the Buttons in our Android Application. If you aren't aware o ...
- 【iOS开发-8】UIButton类型属性简单归纳以及自定义按钮的设置
(1)UIButton类继承自UIControl,而UIControl继承自UIView,因为UIView就是个矩形区域,所以UIButton实例化的对象其实都是一个矩形,虽然有各种圆角.增加联系人. ...
- 【实用】表维护视图SM30增加自定义按钮的实现
表维护视图SM30增加自定义按钮的实现 1.新建Table并通过"表维护生成器"生成SM30维护视图. 正常的创建表维护视图到这一步也就结束了,最多是进函数组调整字段名称和显示效果 ...
- 如何在 SAP Fiori Elements List Report 表格工具栏里增添新的自定义按钮
如下图所示,这是 SAP Fiori Elements List Report 一个例子,我们想在表格工具栏里,新增一个自定义按钮: 实现方式 在 SAP Fiori Elements 项目工程里,修 ...
- 按钮配置之自定义按钮使用(一)——JEPLUS软件快速开发平台
为什么80%的码农都做不了架构师?>>> JEPLUS按钮配置之自定义按钮使用(一) 系统开发过程中无论是表单的默认按钮或是列表的默认按钮以及Action的默认按钮有时候并不能 ...
最新文章
- oracle报错ora-12162,ORA-12162: TNS: 指定的 Net 服务名不正确
- http://m.blog.csdn.net/article/details?id=2630620
- bim推荐计算机配置,BIM建模推荐电脑配置清单 适合Revit软件的BIM建模电脑主机配置(2)...
- “三农”谋定金融 中国农民丰收节交易会金融服务研讨会
- c#往结构体里面读数据_结构体内存对齐,这回给你彻底搞会!
- matlab的一些关于块分类的函数~~~
- error C2248: “CObject::CObject”: 无法访问 private 成员(在“CObject”类中声明) [问题点数
- 怎样使用pyinstaller打包
- 关于mac突然无法链接Wi-Fi的问题
- oneno浏览器插件_谷歌浏览器插件Send to OneNote 让Chrome也拥有OneNote的快捷保存功能...
- 安装CAD显示计算机丢失SETUPUI,彻底解决CAD2012不能安装问题.docx
- 裁判文书网2019年9月份最新爬虫
- java pgm_用Java读取pgm文件
- 向 3D 城市模型添加外观
- 揭秘;抖音美妆账号如何做?如何玩转抖音美妆类运营:国仁楠哥
- C++八股文分享---数据结构其二---哈希表
- 【HQL】HQL汇总
- 获取Google PR值的代码!
- 分享 tsc 条码打印机 打印控件 调用方法 , c# silverlight
- 京东短网址高可用提升最佳实践 | 京东云技术团队