(八十)c#Winform自定义控件-分割线标签
程序员文章站
2023-11-13 11:46:46
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。 GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:https://gitee.com/kwwwvagaa/net_winform_custom_contr ......
前提
入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。
github:https://github.com/kwwwvagaa/netwinformcontrol
码云:
如果觉得写的还行,请点个 star 支持一下吧
欢迎前来交流探讨: 企鹅群568015492
来都来了,点个【推荐】再走吧,谢谢
nuget
install-package hzh_controls
目录
用处及效果
准备工作
这个比较简单,对label扩展,重绘划线即可
开始
添加一个类ucsplitlabel ,继承 label
代码比较少
1 // *********************************************************************** 2 // assembly : hzh_controls 3 // created : 2019-10-09 4 // 5 // *********************************************************************** 6 // <copyright file="ucsplitlabel.cs"> 7 // copyright by huang zhenghui(黄正辉) all, qq group:568015492 qq:623128629 email:623128629@qq.com 8 // </copyright> 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 /// <summary> 27 /// class ucsplitlabel. 28 /// implements the <see cref="system.windows.forms.label" /> 29 /// </summary> 30 /// <seealso cref="system.windows.forms.label" /> 31 public class ucsplitlabel : label 32 { 33 /// <summary> 34 /// gets or sets the text. 35 /// </summary> 36 /// <value>the text.</value> 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 /// <summary> 51 /// 获取或设置控件显示的文字的字体。 52 /// </summary> 53 /// <value>the font.</value> 54 /// <permissionset> 55 /// <ipermission class="system.security.permissions.environmentpermission, mscorlib, version=2.0.3600.0, culture=neutral, publickeytoken=b77a5c561934e089" version="1" unrestricted="true" /> 56 /// <ipermission class="system.security.permissions.fileiopermission, mscorlib, version=2.0.3600.0, culture=neutral, publickeytoken=b77a5c561934e089" version="1" unrestricted="true" /> 57 /// <ipermission class="system.security.permissions.securitypermission, mscorlib, version=2.0.3600.0, culture=neutral, publickeytoken=b77a5c561934e089" version="1" flags="unmanagedcode, controlevidence" /> 58 /// <ipermission class="system.diagnostics.performancecounterpermission, system, version=2.0.3600.0, culture=neutral, publickeytoken=b77a5c561934e089" version="1" unrestricted="true" /> 59 /// </permissionset> 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 /// <summary> 74 /// 获取或设置大小,该大小是 <see cref="m:system.windows.forms.control.getpreferredsize(system.drawing.size)" /> 可以指定的下限。 75 /// </summary> 76 /// <value>the minimum size.</value> 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 /// <summary> 93 /// 获取或设置大小,该大小是 <see cref="m:system.windows.forms.control.getpreferredsize(system.drawing.size)" /> 可以指定的上限。 94 /// </summary> 95 /// <value>the maximum size.</value> 96 [localizable(true)] 97 public override size maximumsize 98 { 99 get 100 { 101 return base.maximumsize; 102 } 103 set 104 { 105 base.maximumsize = value; 106 resetmaxsize(); 107 } 108 } 109 /// <summary> 110 /// 获取或设置一个值,该值指示是否自动调整控件的大小以完整显示其内容。 111 /// </summary> 112 /// <value><c>true</c> if [automatic size]; otherwise, <c>false</c>.</value> 113 /// <permissionset> 114 /// <ipermission class="system.security.permissions.environmentpermission, mscorlib, version=2.0.3600.0, culture=neutral, publickeytoken=b77a5c561934e089" version="1" unrestricted="true" /> 115 /// <ipermission class="system.security.permissions.fileiopermission, mscorlib, version=2.0.3600.0, culture=neutral, publickeytoken=b77a5c561934e089" version="1" unrestricted="true" /> 116 /// <ipermission class="system.security.permissions.securitypermission, mscorlib, version=2.0.3600.0, culture=neutral, publickeytoken=b77a5c561934e089" version="1" flags="unmanagedcode, controlevidence" /> 117 /// <ipermission class="system.diagnostics.performancecounterpermission, system, version=2.0.3600.0, culture=neutral, publickeytoken=b77a5c561934e089" version="1" unrestricted="true" /> 118 /// </permissionset> 119 [editorbrowsable(editorbrowsablestate.never), browsable(false)] 120 public override bool autosize 121 { 122 get 123 { 124 return base.autosize; 125 } 126 set 127 { 128 base.autosize = value; 129 } 130 } 131 132 133 /// <summary> 134 /// the line color 135 /// </summary> 136 private color linecolor = linecolors.light; 137 138 /// <summary> 139 /// gets or sets the color of the line. 140 /// </summary> 141 /// <value>the color of the line.</value> 142 public color linecolor 143 { 144 get { return linecolor; } 145 set 146 { 147 linecolor = value; 148 invalidate(); 149 } 150 } 151 152 /// <summary> 153 /// resets the maximum size. 154 /// </summary> 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 /// <summary> 169 /// initializes a new instance of the <see cref="ucsplitlabel"/> class. 170 /// </summary> 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 /// <summary> 187 /// handles the paddingchanged event of the ucsplitlabel control. 188 /// </summary> 189 /// <param name="sender">the source of the event.</param> 190 /// <param name="e">the <see cref="eventargs"/> instance containing the event data.</param> 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 /// <summary> 200 /// handles the <see cref="e:paint" /> event. 201 /// </summary> 202 /// <param name="e">包含事件数据的 <see cref="t:system.windows.forms.painteventargs" />。</param> 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 }
最后的话
如果你喜欢的话,请到 点个星星吧
上一篇: Java-集合
下一篇: 代码的性能优化,5方面的调优经验