(Eighty) c # Winform custom control - the dividing line label

premise

The bank has 7, 8 years, and always wanted to do a nice custom control points, so there is this series of articles.

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

Cloud code: https://gitee.com/kwwwvagaa/net_winform_custom_control.git

If you feel that writing was okay, please support the point of a star about it

Come and explore the exchange: Penguins 568 015 492 Penguins 568 015 492

To have come, a point [Recommended] and then go, thank you

nuGet

Install-Package HZH_Controls

table of Contents

https://www.cnblogs.com/bfyx/p/11364884.html

Usefulness and effectiveness

Ready to work

This is relatively simple, the expansion of the label, you can redraw crossed

Start

Add a class UCSplitLabel, inherited Label

Less code

  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:[email protected]
  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 is              }
 72          }
 73 is          ///  <Summary> 
74          /// Gets or sets the size that is <see cref = "M: System . Windows.Forms.Control.GetPreferredSize (System.Drawing.Size) "/> You can specify a lower limit.
75          ///  </ Summary> 
76          ///  <value> of The size Minimum. </ Value> 
77          [the Localizable ( to true )]
 78          public the override Size the MinimumSize
 79          {
 80              GET 
81              {
 82                  return  Base .MinimumSize;
 83              }
 84              SET 
85              {
 86                  Base .MinimumSize = value;
 87                  ResetMaxSize ();
 88              }
 89          }
 90  
91 is  
92          ///  <Summary> 
93          /// Gets or sets the size that is <see cref = "M: System.Windows.Forms.Control.GetPreferredSize (System.Drawing.Size)" /> may specify an upper limit.
 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          /// Gets or sets a value indicating whether to automatically resize the control display its contents in full.
111          ///  </ Summary> 
112          ///  <value> <C> to true </ C> IF [Automatic size]; otherwise, <C> to false </ C> . </ Value> 
113          ///  <the PermissionSet > 
114          ///    <IPermission class = "System.Security.Permissions.EnvironmentPermission, mscorlib, Version = 2.0.3600.0, Culture = Neutral, b77a5c561934e089 the PublicKeyToken =" Version = ". 1", Unrestricted = "to 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 }

 

Last words

If you like it, please  https://gitee.com/kwwwvagaa/net_winform_custom_control  point stars right

Guess you like

Origin www.cnblogs.com/bfyx/p/11640479.html