博客
关于我
(八十)c#Winform自定义控件-分割线标签-HZHControls
阅读量:410 次
发布时间:2019-03-06

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

官网

前提

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

GitHub:

码云:

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

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

来都来了,点个【推荐】再走吧,谢谢

NuGet

Install-Package HZH_Controls

目录

用处及效果

准备工作

这个比较简单,对label扩展,重绘划线即可

开始

添加一个类UCSplitLabel ,继承 Label

代码比较少

1 // ***********************************************************************  2 // Assembly         : HZH_Controls  3 // Created          : 2019-10-09  4 //  5 // ***********************************************************************  6 // 
7 // Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com 8 //
9 // 10 // Blog: https://www.cnblogs.com/bfyx 11 // GitHub:https://github.com/kwwwvagaa/NetWinformControl 12 // gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git 13 // 14 // If you use this code, please keep this note. 15 // *********************************************************************** 16 using System; 17 using System.Collections.Generic; 18 using System.Linq; 19 using System.Text; 20 using System.Windows.Forms; 21 using System.Drawing; 22 using System.ComponentModel; 23 24 namespace HZH_Controls.Controls 25 { 26 /// 27 /// Class UCSplitLabel. 28 /// Implements the
29 ///
30 ///
31 public class UCSplitLabel : Label 32 { 33 /// 34 /// Gets or sets the text. 35 /// 36 ///
The text.
37 [Localizable(true)] 38 public override string Text 39 { 40 get 41 { 42 return base.Text; 43 } 44 set 45 { 46 base.Text = value; 47 ResetMaxSize(); 48 } 49 } 50 /// 51 /// 获取或设置控件显示的文字的字体。 52 /// 53 ///
The font.
54 ///
55 ///
56 ///
57 ///
58 ///
59 ///
60 [Localizable(true)] 61 public override Font Font 62 { 63 get 64 { 65 return base.Font; 66 } 67 set 68 { 69 base.Font = value; 70 ResetMaxSize(); 71 } 72 } 73 /// 74 /// 获取或设置大小,该大小是
可以指定的下限。 75 ///
76 ///
The minimum size.
77 [Localizable(true)] 78 public override Size MinimumSize 79 { 80 get 81 { 82 return base.MinimumSize; 83 } 84 set 85 { 86 base.MinimumSize = value; 87 ResetMaxSize(); 88 } 89 } 90 91 92 /// 93 /// 获取或设置大小,该大小是
可以指定的上限。 94 ///
95 ///
The maximum size.
96 [Localizable(true)] 97 public override Size MaximumSize 98 { 99 get100 {101 return base.MaximumSize;102 }103 set104 {105 base.MaximumSize = value;106 ResetMaxSize();107 }108 }109 /// 110 /// 获取或设置一个值,该值指示是否自动调整控件的大小以完整显示其内容。111 /// 112 ///
true
if [automatic size]; otherwise,
false
.
113 ///
114 ///
115 ///
116 ///
117 ///
118 ///
119 [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)]120 public override bool AutoSize121 {122 get123 {124 return base.AutoSize;125 }126 set127 {128 base.AutoSize = value;129 }130 }131 132 133 /// 134 /// The line color135 /// 136 private Color lineColor = LineColors.Light;137 138 /// 139 /// Gets or sets the color of the line.140 /// 141 ///
The color of the line.
142 public Color LineColor143 {144 get { return lineColor; }145 set146 {147 lineColor = value;148 Invalidate();149 }150 }151 152 /// 153 /// Resets the maximum size.154 /// 155 private void ResetMaxSize()156 {157 using (var g = this.CreateGraphics())158 {159 var _width = Width;160 var size = g.MeasureString(string.IsNullOrEmpty(Text) ? "A" : Text, Font);161 if (MinimumSize.Height != (int)size.Height)162 MinimumSize = new Size(base.MinimumSize.Width, (int)size.Height);163 if (MaximumSize.Height != (int)size.Height)164 MaximumSize = new Size(base.MaximumSize.Width, (int)size.Height);165 this.Width = _width;166 }167 }168 /// 169 /// Initializes a new instance of the
class.170 ///
171 public UCSplitLabel()172 : base()173 {174 if (ControlHelper.IsDesignMode())175 {176 Text = "分割线";177 Font = new Font("微软雅黑", 8f);178 }179 this.AutoSize = false;180 Padding = new Padding(20, 0, 0, 0);181 MinimumSize = new System.Drawing.Size(150, 10);182 PaddingChanged += UCSplitLabel_PaddingChanged;183 this.Width = 200;184 }185 186 /// 187 /// Handles the PaddingChanged event of the UCSplitLabel control.188 /// 189 /// The source of the event.190 /// The
instance containing the event data.191 void UCSplitLabel_PaddingChanged(object sender, EventArgs e)192 {193 if (Padding.Left < 20)194 {195 Padding = new Padding(20, Padding.Top, Padding.Right, Padding.Bottom);196 }197 }198 199 /// 200 /// Handles the
event.201 ///
202 /// 包含事件数据的
。203 protected override void OnPaint(PaintEventArgs e)204 {205 base.OnPaint(e);206 var g = e.Graphics;207 g.SetGDIHigh();208 209 var size = g.MeasureString(Text, Font);210 g.DrawLine(new Pen(new SolidBrush(lineColor)), new PointF(1, Padding.Top + (this.Height - Padding.Top - Padding.Bottom) / 2), new PointF(Padding.Left - 2, Padding.Top + (this.Height - Padding.Top - Padding.Bottom) / 2));211 g.DrawLine(new Pen(new SolidBrush(lineColor)), new PointF(Padding.Left + size.Width + 1, Padding.Top + (this.Height - Padding.Top - Padding.Bottom) / 2), new PointF(Width - Padding.Right, Padding.Top + (this.Height - Padding.Top - Padding.Bottom) / 2));212 213 }214 }215 }

 

最后的话

如果你喜欢的话,请到  点个星星吧

你可能感兴趣的文章
Nginx之二:nginx.conf简单配置(参数详解)
查看>>
vue中各模块加载和渲染的过程
查看>>
Nginx从入门到精通
查看>>
Nginx从入门到精通(全)
查看>>
Nginx从安装到高可用,一篇搞定!
查看>>
Nginx代理websocket配置(解决websocket异常断开连接tcp连接不断问题)
查看>>
Nginx代理初探
查看>>
nginx代理地图服务--离线部署地图服务(地图数据篇.4)
查看>>
Nginx代理外网映射
查看>>
Nginx代理模式下 log-format 获取客户端真实IP
查看>>
Nginx代理解决跨域问题(导致图片只能预览不能下载)
查看>>
Nginx代理访问提示ERR_CONTENT_LENGTH_MISMATCH
查看>>
Nginx代理配置详解
查看>>
Nginx代理静态资源(gis瓦片图片)实现非固定ip的url适配网络环境映射ip下的资源请求解决方案
查看>>
Nginx代理静态资源(gis瓦片图片)实现非固定ip的url适配网络环境映射ip下的资源请求解决方案
查看>>
nginx优化日志拒绝特定404请求写入
查看>>
Nginx优化解析
查看>>
Nginx使用proxy_cache指令设置反向代理缓存静态资源
查看>>
Nginx做反向代理时访问端口被自动去除
查看>>
Nginx入门教程-简介、安装、反向代理、负载均衡、动静分离使用实例
查看>>