前提
入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。
GitHub:https://github.com/kwwwvagaa/NetWinformControl
码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
如果觉得写的还行,请点个 star 支持一下吧
麻烦博客下方点个【推荐】,谢谢
NuGet
Install-Package HZH_Controls
目录
https://www.cnblogs.com/bfyx/p/11364884.html
用处及效果
准备工作
依然使用GDI+画的,不懂的话就百度一下吧
另外主要用到了三角函数,如果不懂,可以向初中的数学老师再问问(你也可以百度一下)
开始
添加一个类UCMeter 继承 UserControl
首先添加一个需要控制的属性
1 private int splitCount = 10; 2 /// <summary> 3 /// Gets or sets the split count. 4 /// </summary> 5 /// <value>The split count.</value> 6 [Description("分隔刻度数量,>1"), Category("自定义")] 7 public int SplitCount 8 { 9 get { return splitCount; } 10 set 11 { 12 if (value < 1) 13 return; 14 splitCount = value; 15 Refresh(); 16 } 17 } 18 19 private int meterDegrees = 150; 20 /// <summary> 21 /// Gets or sets the meter degrees. 22 /// </summary> 23 /// <value>The meter degrees.</value> 24 [Description("表盘跨度角度,0-360"), Category("自定义")] 25 public int MeterDegrees 26 { 27 get { return meterDegrees; } 28 set 29 { 30 if (value > 360 || value <= 0) 31 return; 32 meterDegrees = value; 33 Refresh(); 34 } 35 } 36 37 private decimal minValue = 0; 38 /// <summary> 39 /// Gets or sets the minimum value. 40 /// </summary> 41 /// <value>The minimum value.</value> 42 [Description("最小值,<MaxValue"), Category("自定义")] 43 public decimal MinValue 44 { 45 get { return minValue; } 46 set 47 { 48 if (value >= maxValue) 49 return; 50 minValue = value; 51 Refresh(); 52 } 53 } 54 55 private decimal maxValue = 100; 56 /// <summary> 57 /// Gets or sets the maximum value. 58 /// </summary> 59 /// <value>The maximum value.</value> 60 [Description("最大值,>MinValue"), Category("自定义")] 61 public decimal MaxValue 62 { 63 get { return maxValue; } 64 set 65 { 66 if (value <= minValue) 67 return; 68 maxValue = value; 69 Refresh(); 70 } 71 } 72 /// <summary> 73 /// 获取或设置控件显示的文字的字体。 74 /// </summary> 75 /// <value>The font.</value> 76 /// <PermissionSet> 77 /// <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" /> 78 /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" /> 79 /// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" /> 80 /// <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" /> 81 /// </PermissionSet> 82 [Description("刻度字体"), Category("自定义")] 83 public override Font Font 84 { 85 get 86 { 87 return base.Font; 88 } 89 set 90 { 91 base.Font = value; 92 Refresh(); 93 } 94 } 95 96 private decimal m_value = 0; 97 /// <summary> 98 /// Gets or sets the value. 99 /// </summary> 100 /// <value>The value.</value> 101 [Description("值,>=MinValue并且<=MaxValue"), Category("自定义")] 102 public decimal Value 103 { 104 get { return m_value; } 105 set 106 { 107 if (value < minValue || value > maxValue) 108 return; 109 m_value = value; 110 Refresh(); 111 } 112 } 113 114 private MeterTextLocation textLocation = MeterTextLocation.None; 115 /// <summary> 116 /// Gets or sets the text location. 117 /// </summary> 118 /// <value>The text location.</value> 119 [Description("值和固定文字显示位置"), Category("自定义")] 120 public MeterTextLocation TextLocation 121 { 122 get { return textLocation; } 123 set 124 { 125 textLocation = value; 126 Refresh(); 127 } 128 } 129 130 private string fixedText; 131 /// <summary> 132 /// Gets or sets the fixed text. 133 /// </summary> 134 /// <value>The fixed text.</value> 135 [Description("固定文字"), Category("自定义")] 136 public string FixedText 137 { 138 get { return fixedText; } 139 set 140 { 141 fixedText = value; 142 Refresh(); 143 } 144 } 145 146 private Font textFont = DefaultFont; 147 /// <summary> 148 /// Gets or sets the text font. 149 /// </summary> 150 /// <value>The text font.</value> 151 [Description("值和固定文字字体"), Category("自定义")] 152 public Font TextFont 153 { 154 get { return textFont; } 155 set 156 { 157 textFont = value; 158 Refresh(); 159 } 160 } 161 162 private Color externalRoundColor = Color.FromArgb(255, 77, 59); 163 /// <summary> 164 /// Gets or sets the color of the external round. 165 /// </summary> 166 /// <value>The color of the external round.</value> 167 [Description("外圆颜色"), Category("自定义")] 168 public Color ExternalRoundColor 169 { 170 get { return externalRoundColor; } 171 set 172 { 173 externalRoundColor =