官网

http://www.hzhcontrols.com

前提

入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。

GitHub:https://github.com/kwwwvagaa/NetWinformControl

码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

如果觉得写的还行,请点个 star 支持一下吧

欢迎前来交流探讨: 企鹅群568015492

目录

c#Winform自定义控件-目录-HZHControls - 冰封一夏 - 博客园

准备工作

自定义的分为控件和窗体2种类型,分别都有一个基类,基类实现公共的大部分工作

开始

首先从基类控件开始吧,

主要实现功能:

  1. 圆角
  2. 边框
  3. 填充颜色

添加一个用户控件,命名为UCControlBase,写入相关属性,包含圆角角度,边框颜色,边框宽度,填充颜色,背景色等

  1  private bool _isRadius = false;2 3         private int _cornerRadius = 24;4 5 6         private bool _isShowRect = false;7 8         private Color _rectColor = Color.FromArgb(220, 220, 220);9 10         private int _rectWidth = 1;11 12         private Color _fillColor = Color.Transparent;13         /// <summary>14         /// 是否圆角15         /// </summary>16         [Description("是否圆角"), Category("自定义")]17         public bool IsRadius18         {19             get20             {21                 return this._isRadius;22             }23             set24             {25                 this._isRadius = value;26             }27         }28         //圆角角度29         [Description("圆角角度"), Category("自定义")]30         public int ConerRadius31         {32             get33             {34                 return this._cornerRadius;35             }36             set37             {38                 this._cornerRadius = value;39             }40         }41 42         /// <summary>43         /// 是否显示边框44         /// </summary>45         [Description("是否显示边框"), Category("自定义")]46         public bool IsShowRect47         {48             get49             {50                 return this._isShowRect;51             }52             set53             {54                 this._isShowRect = value;55             }56         }57         /// <summary>58         /// 边框颜色59         /// </summary>60         [Description("边框颜色"), Category("自定义")]61         public Color RectColor62         {63             get64             {65                 return this._rectColor;66             }67             set68             {69                 this._rectColor = value;70                 this.Refresh();71             }72         }73         /// <summary>74         /// 边框宽度75         /// </summary>76         [Description("边框宽度"), Category("自定义")]77         public int RectWidth78         {79             get80             {81                 return this._rectWidth;82             }83             set84             {85                 this._rectWidth = value;86             }87         }88         /// <summary>89         /// 当使用边框时填充颜色,当值为背景色或透明色或空值则不填充90         /// </summary>91         [Description("当使用边框时填充颜色,当值为背景色或透明色或空值则不填充"), Category("自定义")]92         public Color FillColor93         {94             get95             {96                 return this._fillColor;97             }98             set99             {
100                 this._fillColor = value;
101             }
102         }

需要做的就是重写OnPaint,来画边框以及填充颜色

 1 protected override void OnPaint(PaintEventArgs e)2         {3             if (this.Visible)4             {5                 if (this._isRadius)6                 {7                     this.SetWindowRegion();8                 }9                 if (this._isShowRect)
10                 {
11                     Color rectColor = this._rectColor;
12                     Pen pen = new Pen(rectColor, (float)this._rectWidth);
13                     Rectangle clientRectangle = base.ClientRectangle;
14                     GraphicsPath graphicsPath = new GraphicsPath();
15                     graphicsPath.AddArc(0, 0, _cornerRadius, _cornerRadius, 180f, 90f);
16                     graphicsPath.AddArc(clientRectangle.Width - _cornerRadius - 1, 0, _cornerRadius, _cornerRadius, 270f, 90f);
17                     graphicsPath.AddArc(clientRectangle.Width - _cornerRadius - 1, clientRectangle.Height - _cornerRadius - 1, _cornerRadius, _cornerRadius, 0f, 90f);
18                     graphicsPath.AddArc(0, clientRectangle.Height - _cornerRadius - 1, _cornerRadius, _cornerRadius, 90f, 90f);
19                     graphicsPath.CloseFigure();
20                     e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
21                     if (_fillColor != Color.Empty && _fillColor != Color.Transparent && _fillColor != this.BackColor)
22                         e.Graphics.FillPath(new SolidBrush(this._fillColor), graphicsPath);
23                     e.Graphics.DrawPath(pen, graphicsPath);
24                 }
25             }
26             base.OnPaint(e);
27         }
28
29         private void SetWindowRegion()
30         {
31             GraphicsPath path = new GraphicsPath();
32             Rectangle rect = new Rectangle(-1, -1, base.Width + 1, base.Height);
33             path = this.GetRoundedRectPath(rect, this._cornerRadius);
34             base.Region = new Region(path);
35         }
36
37         private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius)
38         {
39             Rectangle rect2 = new Rectangle(rect.Location, new Size(radius, radius));
40             GraphicsPath graphicsPath = new GraphicsPath();
41             graphicsPath.AddArc(rect2, 180f, 90f);//左上角
42             rect2.X = rect.Right - radius;
43             graphicsPath.AddArc(rect2, 270f, 90f);//右上角
44             rect2.Y = rect.Bottom - radius;
45             rect2.Width += 1;
46             rect2.Height += 1;
47             graphicsPath.AddArc(rect2, 360f, 90f);//右下角
48             rect2.X = rect.Left;
49             graphicsPath.AddArc(rect2, 90f, 90f);//左下角
50             graphicsPath.CloseFigure();
51             return graphicsPath;
52         }

至此基类控件就完成了,下面是完成代码

View Code

View Code

用处及效果

用处:你可以把它当作一个panel来用,比如需要包裹一些控件并显示一个圆角边框的时候,你应该想到用这个控件

效果图:其实就是一个圆角边框的面板

最后的话

如果你喜欢的话,请到 HZHControls控件库: HZHControls控件库,c#的winform自定义控件,对触屏具有更好的操作支持,项目是基于framework4.0,完全原生控件开发,没有使用任何第三方控件,你可以放心的用在你的项目中(winfromcontrol/winformcontrol/.net)。还有更丰富的工业控件持续增加中~~~ 点个星星吧

(一)c#Winform自定义控件-基类控件-HZHControls相关推荐

  1. Labview自定义控件-布尔类控件

    LabVIEW中自定义控件(.ctl控件)的方法 ​​​​​[LabVIEW小技巧]LabVIEW自定义系统按钮时图片项设置说明_Enjoy Coding With LabVIEW-CSDN博客 La ...

  2. 【自定义控件】c#winform自定义控件实现标签控件

    介绍 首先我们设计这个控件的时候要明白控件是怎样交互的, 熟悉b站的小伙伴应该知道 ,我们上传视频的时候会去选择标签 ,我们输入标签文本 按下回车就代表该标签已经添加成功了,效果图如下! 控件拆分 我 ...

  3. winform中自定义控件里面的控件随着自定义控件的改变而改变

    刚开始实习,项目经历经理要求我做一个自定义控件,才发现,在学校学的东西有多基础(还是大专的).简单的理解了自定义控件后,才发现,当使用自定义控件的时候,一个很大的问题,里面的控件不会随着自定义控件的改 ...

  4. C#自定义控件VS用户控件

    C#自定义控件VS用户控件 1.C#中自定义控件VS用户控件大比拼 2.为自定义控件(或类)的方法属性添加注解 2.1.Description:在属性窗口中添加属性及属性说明 2.2.Browsabl ...

  5. java 用户控件_C#自定义控件VS用户控件

    C#中自定义控件VS用户控件大比拼 1 自定义控件与用户控件区别 WinForm中, 用户控件(User Control):继承自 UserControl,主要用于开发 Container 控件,Co ...

  6. 八、pyqt5按钮类控件——QPushButton、QRadioButton、QCheckBox

    pyqt5中常用的按钮类控件有QPushButton.QRadioButton.QCheckBox.QToolButton等.这些按钮类的基类都是QAbstracButton类.所以这些类有部分方法是 ...

  7. kettle中java组件_kettle系列-[KettleUtil]kettle插件,类似kettle的自定义java类控件

    该kettle插件功能类似kettle现有的定义java类插件,自定java类插件主要是支持在kettle中直接编写java代码实现自定特殊功能,而本控件主要是将自定义代码转移到jar包,就是说自定义 ...

  8. c#在WinForm中重写ProgressBar控件(带%的显示)

    c#在WinForm中重写ProgressBar控件(带%的显示) 2009-05-14 13:13 #region 定义textProgressBar控件的类 namespace csPublish ...

  9. Chip类控件(Chip、InputChip、ChoiceChip、FilterChip、ActionChip)

    RawChip Material风格标签控件,此控件是其他标签控件的基类,通常情况下,不会直接创建此控件,而是使用如下控件: Chip InputChip ChoiceChip FilterChip ...

最新文章

  1. XenApp_XenDesktop_7.6实战篇之八:申请及导入许可证
  2. C语言字母和数字数量,请问这个用c怎么做:输入一串字符,分别统计其中数字和字母的个数...
  3. Chromium:编译,运行
  4. boost::python::indirect_traits相关的测试程序
  5. git提取和拉取的区别_git fetch和git pull的区别
  6. 移动端引导页UI设计临摹模板,ui设计师进阶必备
  7. android实机闪退,安卓模拟器,安卓真机,进入播放界面就会闪退
  8. winpe 服务器 维护,无垠PE维护系统v2017.04.30 ISO维护/网络版无广告
  9. vue表格信息进行空值填充,并使用 *** 修饰隐私信息
  10. 手把手教你快速掌握 ABD调试工具+19条常用命令
  11. 米勒拉宾算法(素性测试)
  12. mysql 锁住一行数据_MySQL-锁
  13. hypervisor详解
  14. css font html里写,HTML,CSS,font
  15. Excel解析easyexcel工具类
  16. 基于微信小程序的校园互助平台
  17. C#,Winform 声音、音量控制操作
  18. PyQt5-QPainter-绘图类
  19. 使用PlantUML快速绘制流程图/时序图/类图/用例图…
  20. 参与社团活动的意义_开展社团活动的意义

热门文章

  1. 数据处理取对数的作用
  2. ROS入门——胡春旭老师《机器人开发实践》在ROS-Melodic下的编译
  3. 程序猿解决BUG之总结
  4. i18n(国际化)地域标识码
  5. objection 基础知识
  6. iOS 游戏app的开发
  7. 【kafka实战】分区重分配可能出现的问题和排查问题思路(生产环境实战,附视频)
  8. 老师就是学生的天-- 恩人意识,青天意识从娃娃抓起
  9. Bert—SST-2
  10. 怎样避免无意识偏见_精神病学意识到大数据和人工智能的价值和偏见