前提 入行已經7,8年了,一直想做一套漂亮點的自定義控制項,於是就有了本系列文章。 GitHub:https://github.com/kwwwvagaa/NetWinformControl 碼雲:https://gitee.com/kwwwvagaa/net_winform_custom_contr ...
前提
入行已經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+相關知識
開始
添加一個類UCCurve 繼承自UserControl
添加一些控制屬性
1 /// <summary> 2 /// The value count maximum 3 /// </summary> 4 private const int value_count_max = 4096; 5 6 /// <summary> 7 /// The value maximum left 8 /// </summary> 9 private float value_max_left = 100f; 10 11 /// <summary> 12 /// The value minimum left 13 /// </summary> 14 private float value_min_left = 0f; 15 16 /// <summary> 17 /// The value maximum right 18 /// </summary> 19 private float value_max_right = 100f; 20 21 /// <summary> 22 /// The value minimum right 23 /// </summary> 24 private float value_min_right = 0f; 25 26 /// <summary> 27 /// The value segment 28 /// </summary> 29 private int value_Segment = 5; 30 31 /// <summary> 32 /// The value is abscissa strech 33 /// </summary> 34 private bool value_IsAbscissaStrech = false; 35 36 /// <summary> 37 /// The value strech data count maximum 38 /// </summary> 39 private int value_StrechDataCountMax = 300; 40 41 /// <summary> 42 /// The value is render dash line 43 /// </summary> 44 private bool value_IsRenderDashLine = true; 45 46 /// <summary> 47 /// The text format 48 /// </summary> 49 private string textFormat = "HH:mm"; 50 51 /// <summary> 52 /// The value interval abscissa text 53 /// </summary> 54 private int value_IntervalAbscissaText = 100; 55 56 /// <summary> 57 /// The random 58 /// </summary> 59 private Random random = null; 60 61 /// <summary> 62 /// The value title 63 /// </summary> 64 private string value_title = ""; 65 66 /// <summary> 67 /// The left right 68 /// </summary> 69 private int leftRight = 50; 70 71 /// <summary> 72 /// Up dowm 73 /// </summary> 74 private int upDowm = 50; 75 76 /// <summary> 77 /// The data list 78 /// </summary> 79 private Dictionary<string, CurveItem> data_list = null; 80 81 /// <summary> 82 /// The data text 83 /// </summary> 84 private string[] data_text = null; 85 86 /// <summary> 87 /// The auxiliary lines 88 /// </summary> 89 private List<AuxiliaryLine> auxiliary_lines; 90 91 /// <summary> 92 /// The auxiliary labels 93 /// </summary> 94 private List<AuxiliaryLable> auxiliary_Labels; 95 96 /// <summary> 97 /// The mark texts 98 /// </summary> 99 private List<MarkText> MarkTexts; 100 101 /// <summary> 102 /// The font size9 103 /// </summary> 104 private Font font_size9 = null; 105 106 /// <summary> 107 /// The font size12 108 /// </summary> 109 private Font font_size12 = null; 110 111 /// <summary> 112 /// The brush deep 113 /// </summary> 114 private Brush brush_deep = null; 115 116 /// <summary> 117 /// The pen normal 118 /// </summary> 119 private Pen pen_normal = null; 120 121 /// <summary> 122 /// The pen dash 123 /// </summary> 124 private Pen pen_dash = null; 125 126 /// <summary> 127 /// The color normal 128 /// </summary> 129 private Color color_normal = Color.DeepPink; 130 131 /// <summary> 132 /// The color deep 133 /// </summary> 134 private Color color_deep = Color.DimGray; 135 136 /// <summary> 137 /// The color dash 138 /// </summary> 139 private Color color_dash = Color.FromArgb(232, 232, 232); 140 141 /// <summary> 142 /// The color mark font 143 /// </summary> 144 private Color color_mark_font = Color.DodgerBlue; 145 146 /// <summary> 147 /// The brush mark font 148 /// </summary> 149 private Brush brush_mark_font = Brushes.DodgerBlue; 150 151 /// <summary> 152 /// The format left 153 /// </summary> 154 private StringFormat format_left = null; 155 156 /// <summary> 157 /// The format right 158 /// </summary> 159 private StringFormat format_right = null; 160 161 /// <summary> 162 /// The format center 163 /// </summary> 164 private StringFormat format_center = null; 165 166 /// <summary> 167 /// The is render right coordinate 168 /// </summary> 169 private bool isRenderRightCoordinate = true; 170 171 /// <summary> 172 /// The curve name width 173 /// </summary> 174 private int curveNameWidth = 100; 175 176 /// <summary> 177 /// The components 178 /// </summary> 179 private IContainer components = null; 180 181 /// <summary> 182 /// 獲取或設置控制項的背景色。 183 /// </summary> 184 /// <value>The color of the back.</value> 185 /// <PermissionSet> 186 /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" /> 187 /// </PermissionSet> 188 [Browsable(true)] 189 [Description("獲取或設置控制項的背景色")] 190 [Category("自定義")] 191 [DefaultValue(typeof(Color), "Transparent")] 192 [EditorBrowsable(EditorBrowsableState.Always)] 193 public override Color BackColor 194 { 195 get 196 { 197 return base.BackColor; 198 } 199 set 200 { 201 base.BackColor = value; 202 } 203 } 204 205 /// <summary> 206 /// Gets or sets the value maximum left. 207 /// </summary> 208 /// <value>The value maximum left.</value> 209 [Category("自定義")] 210 [Description("獲取或設置圖形的左縱坐標的最大值,該值必須大於最小值")] 211 [Browsable(true)] 212 [DefaultValue(100f)] 213 public float ValueMaxLeft 214 { 215 get 216 { 217 return value_max_left; 218 } 219 set 220 { 221 value_max_left = value; 222 Invalidate(); 223 } 224 } 225 226 /// <summary> 227 /// Gets or sets the value minimum left. 228 /// </summary> 229 /// <value>The value minimum left.</value> 230 [Category("自定義")] 231 [Description("獲取或設置圖形的左縱坐標的最小值,該值必須小於最大值")] 232 [Browsable(true)] 233 [DefaultValue(0f)] 234 public float ValueMinLeft 235 { 236 get 237 { 238 return value_min_left; 239 } 240 set 241 { 242 value_min_left = value; 243 Invalidate(); 244 } 245 } 246 247 /// <summary> 248 /// Gets or sets the value maximum right. 249 /// </summary> 250 /// <value>The value maximum right.</value> 251 [Category("自定義")] 252 [Description("獲取或設置圖形的右縱坐標的最大值,該值必須大於最小值")] 253 [Browsable(true)] 254 [DefaultValue(100f)] 255 public float ValueMaxRight 256 { 257 get 258 { 259 return value_max_right; 260 } 261 set 262 { 263 value_max_right = value; 264 Invalidate(); 265 } 266 } 267 268 /// <summary> 269 /// Gets or sets the value minimum right. 270 /// </summary> 271 /// <value>The value minimum right.</value> 272 [Category("自定義")] 273 [Description("獲取或設置圖形的右縱坐標的最小值,該值必須小於最大值")] 274 [Browsable(true)] 275 [DefaultValue(0f)] 276 public float ValueMinRight 277 { 278 get 279 { 280 return value_min_right; 281 } 282 set 283 { 284 value_min_right = value; 285 Invalidate(); 286 } 287 } 288 289 /// <summary> 290 /// Gets or sets the value segment. 291 /// </summary> 292 /// <value>The value segment.</value> 293 [Category("自定義")] 294 [Description("獲取或設置圖形的縱軸分段數")] 295 [Browsable(true)] 296 [DefaultValue(5)] 297 public int ValueSegment 298 { 299 get 300 { 301 return value_Segment; 302 } 303 set 304 { 305 value_Segment = value; 306 Invalidate(); 307 } 308 } 309 310 /// <summary> 311 /// Gets or sets a value indicating whether this instance is abscissa strech. 312 /// </summary> 313 /// <value><c>true</c> if this instance is abscissa strech; otherwise, <c>false</c>.</value> 314 [Category("自定義")] 315 [Description("獲取或設置所有的數據是否強制在一個界面里顯示")] 316 [Browsable(true)] 317 [DefaultValue(false)] 318 public bool IsAbscissaStrech 319 { 320 get 321 { 322 return value_IsAbscissaStrech; 323 } 324 set 325 { 326 value_IsAbscissaStrech = value; 327 Invalidate(); 328 } 329 } 330 331 /// <summary> 332 /// Gets or sets the strech data count maximum. 333 /// </summary> 334 /// <value>The strech data count maximum.</value> 335 [Category("自定義")] 336 [Description("獲取或設置拉伸模式下的最大數據量")] 337 [Browsable(true)] 338 [DefaultValue(300)] 339 public int StrechDataCountMax 340 { 341 get 342 { 343 return value_StrechDataCountMax; 344 } 345 set 346 { 347 value_StrechDataCountMax = value; 348 Invalidate(); 349 } 350 } 351 352 /// <summary> 353 /// Gets or sets a value indicating whether this instance is render dash line. 354 /// </summary> 355 /// <value><c>true</c> if this instance is render dash line; otherwise, <c>false</c>.</value> 356 [Category("自定義")] 357 [Description("獲取或設置虛線是否進行顯示")] 358 [Browsable(true)] 359 [DefaultValue(true)] 360 public bool IsRenderDashLine 361 { 362 get 363 { 364 return value_IsRenderDashLine; 365 } 366 set 367 { 368 value_IsRenderDashLine = value; 369 Invalidate(); 370 } 371 } 372 373 /// <summary> 374 /// Gets or sets the color lines and text. 375 /// </summary> 376 /// <value>The color lines and text.</value> 377 [Category("自定義")] 378 [Description("獲取或設置坐標軸及相關信息文本的顏色")] 379 [Browsable(true)] 380 [DefaultValue(typeof(Color), "DimGray")] 381 public Color ColorLinesAndText 382 { 383 get 384 { 385 return color_deep; 386 } 387 set 388 { 389 color_deep = value; 390 InitializationColor(); 391 Invalidate(); 392 } 393 } 394 395 /// <summary> 396 /// Gets or sets the color dash lines. 397 /// </summary> 398 /// <value>The color dash lines.</value> 399 [Category("自定義")] 400 [Description("獲取或設置虛線的顏色")] 401 [Browsable(true)] 402 public Color ColorDashLines 403 { 404 get 405 { 406 return color_dash; 407 } 408 set 409 { 410 color_dash = value; 411 if (pen_dash != null) 412 pen_dash.Dispose(); 413 pen_dash = new Pen(color_dash); 414 pen_dash.DashStyle = DashStyle.Custom; 415 pen_dash.DashPattern = new float[2] 416 { 417 5f, 418 5f 419 }; 420 Invalidate(); 421 } 422 } 423 424 /// <summary> 425 /// Gets or sets the interval abscissa text. 426 /// </summary> 427 /// <value>The interval abscissa text.</value> 428 [Category("自定義")] 429 [Description("獲取或設置縱向虛線的分隔情況,單位為多少個數據")] 430 [Browsable(true)] 431 [DefaultValue(100)] 432 public int IntervalAbscissaText 433 { 434 get 435 { 436 return value_IntervalAbscissaText; 437 } 438 set 439 { 440 value_IntervalAbscissaText = value; 441 Invalidate(); 442 } 443 } 444 445 /// <summary> 446 /// Gets or sets the text add format. 447 /// </summary> 448 /// <value>The text add format.</value> 449 [Category("自定義")] 450 [Description("獲取或設置實時數據新增時文本相對應於時間的格式化字元串,預設HH:mm")] 451 [Browsable(true)] 452 [DefaultValue("HH:mm")] 453 public string TextAddFormat 454 { 455 get 456 { 457