UCSplitLabel.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. namespace HZH_Controls.Controls
  24. {
  25. /// <summary>
  26. /// Class UCSplitLabel.
  27. /// Implements the <see cref="System.Windows.Forms.Label" />
  28. /// </summary>
  29. /// <seealso cref="System.Windows.Forms.Label" />
  30. public class UCSplitLabel : Label
  31. {
  32. /// <summary>
  33. /// Gets or sets the text.
  34. /// </summary>
  35. /// <value>The text.</value>
  36. [Localizable(true)]
  37. public override string Text
  38. {
  39. get
  40. {
  41. return base.Text;
  42. }
  43. set
  44. {
  45. base.Text = value;
  46. ResetMaxSize();
  47. }
  48. }
  49. /// <summary>
  50. /// 获取或设置控件显示的文字的字体。
  51. /// </summary>
  52. /// <value>The font.</value>
  53. /// <PermissionSet>
  54. /// <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  55. /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  56. /// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />
  57. /// <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  58. /// </PermissionSet>
  59. [Localizable(true)]
  60. public override Font Font
  61. {
  62. get
  63. {
  64. return base.Font;
  65. }
  66. set
  67. {
  68. base.Font = value;
  69. ResetMaxSize();
  70. }
  71. }
  72. /// <summary>
  73. /// 获取或设置大小,该大小是 <see cref="M:System.Windows.Forms.Control.GetPreferredSize(System.Drawing.Size)" /> 可以指定的下限。
  74. /// </summary>
  75. /// <value>The minimum size.</value>
  76. [Localizable(true)]
  77. public override Size MinimumSize
  78. {
  79. get
  80. {
  81. return base.MinimumSize;
  82. }
  83. set
  84. {
  85. base.MinimumSize = value;
  86. ResetMaxSize();
  87. }
  88. }
  89. /// <summary>
  90. /// 获取或设置大小,该大小是 <see cref="M:System.Windows.Forms.Control.GetPreferredSize(System.Drawing.Size)" /> 可以指定的上限。
  91. /// </summary>
  92. /// <value>The maximum size.</value>
  93. [Localizable(true)]
  94. public override Size MaximumSize
  95. {
  96. get
  97. {
  98. return base.MaximumSize;
  99. }
  100. set
  101. {
  102. base.MaximumSize = value;
  103. ResetMaxSize();
  104. }
  105. }
  106. /// <summary>
  107. /// 获取或设置一个值,该值指示是否自动调整控件的大小以完整显示其内容。
  108. /// </summary>
  109. /// <value><c>true</c> if [automatic size]; otherwise, <c>false</c>.</value>
  110. /// <PermissionSet>
  111. /// <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  112. /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  113. /// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />
  114. /// <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  115. /// </PermissionSet>
  116. [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)]
  117. public override bool AutoSize
  118. {
  119. get
  120. {
  121. return base.AutoSize;
  122. }
  123. set
  124. {
  125. base.AutoSize = value;
  126. }
  127. }
  128. /// <summary>
  129. /// The line color
  130. /// </summary>
  131. private Color lineColor = LineColors.Light;
  132. /// <summary>
  133. /// Gets or sets the color of the line.
  134. /// </summary>
  135. /// <value>The color of the line.</value>
  136. public Color LineColor
  137. {
  138. get { return lineColor; }
  139. set
  140. {
  141. lineColor = value;
  142. Invalidate();
  143. }
  144. }
  145. /// <summary>
  146. /// Resets the maximum size.
  147. /// </summary>
  148. private void ResetMaxSize()
  149. {
  150. using (var g = this.CreateGraphics())
  151. {
  152. var _width = Width;
  153. var size = g.MeasureString(string.IsNullOrEmpty(Text) ? "A" : Text, Font);
  154. if (MinimumSize.Height != (int)size.Height)
  155. MinimumSize = new Size(base.MinimumSize.Width, (int)size.Height);
  156. if (MaximumSize.Height != (int)size.Height)
  157. MaximumSize = new Size(base.MaximumSize.Width, (int)size.Height);
  158. this.Width = _width;
  159. }
  160. }
  161. /// <summary>
  162. /// Initializes a new instance of the <see cref="UCSplitLabel"/> class.
  163. /// </summary>
  164. public UCSplitLabel()
  165. : base()
  166. {
  167. if (ControlHelper.IsDesignMode())
  168. {
  169. Text = "分割线";
  170. Font = new Font("微软雅黑", 8f);
  171. }
  172. this.AutoSize = false;
  173. Padding = new Padding(20, 0, 0, 0);
  174. MinimumSize = new System.Drawing.Size(150, 10);
  175. PaddingChanged += UCSplitLabel_PaddingChanged;
  176. this.Width = 200;
  177. }
  178. /// <summary>
  179. /// Handles the PaddingChanged event of the UCSplitLabel control.
  180. /// </summary>
  181. /// <param name="sender">The source of the event.</param>
  182. /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  183. void UCSplitLabel_PaddingChanged(object sender, EventArgs e)
  184. {
  185. if (Padding.Left < 20)
  186. {
  187. Padding = new Padding(20, Padding.Top, Padding.Right, Padding.Bottom);
  188. }
  189. }
  190. /// <summary>
  191. /// Handles the <see cref="E:Paint" /> event.
  192. /// </summary>
  193. /// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.PaintEventArgs" />。</param>
  194. protected override void OnPaint(PaintEventArgs e)
  195. {
  196. base.OnPaint(e);
  197. var g = e.Graphics;
  198. g.SetGDIHigh();
  199. var size = g.MeasureString(Text, Font);
  200. 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));
  201. 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));
  202. }
  203. }
  204. }