前提
入行已經(jīng)7,8年了,一直想做一套漂亮點(diǎn)的自定義控件,于是就有了本系列文章。
開源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control
<https://gitee.com/kwwwvagaa/net_winform_custom_control>
如果覺得寫的還行,請(qǐng)點(diǎn)個(gè) star 支持一下吧
歡迎前來交流探討: 企鵝群568015492? <https://shang.qq.com/wpa/qunwpa?
idkey=6e08741ef16fe53bf0314c1c9e336c4f626047943a8b76bac062361bab6b4f8d>
目錄
https://www.cnblogs.com/bfyx/p/11364884.html
<https://www.cnblogs.com/bfyx/p/11364884.html>
準(zhǔn)備工作
該控件將繼承基類控件UCControlBase,如果你還對(duì)UCControlBase不了解,請(qǐng)移步?(一)c#Winform自定義控件-基類控件
<https://www.cnblogs.com/bfyx/p/11361809.html>?查看
開始
這個(gè)控件比較簡單,沒有多少東西,看下關(guān)鍵代碼吧
1 private int _value = 0; 2 3 public int Value 4 { 5 get { return this
._value; } 6 set 7 { 8 if (value < 0) 9 return; 10 this._value = value; 11
SetValue();12 } 13 } 14 15 private int maxValue = 100; 16 17 public int
MaxValue18 { 19 get { return maxValue; } 20 set 21 { 22 if (value <= 0) 23
return; 24 maxValue = value; 25 SetValue(); 26 } 27 } 28 29 private void
SetValue()30 { 31 double dbl = (double)_value / (double)maxValue; 32 this
.panel1.Width = (int)(this.Width * dbl); 33 } 34 35 public ProcessExt() 36 {
37 InitializeComponent(); 38 } 39 40 private void ProcessExt_SizeChanged(
object sender, EventArgs e) 41 { 42 SetValue(); 43 } 44 45 public void Step()
46 { 47 Value++; 48 }
然后看下完成代碼吧
1 // 版權(quán)所有 黃正輝 交流群:568015492 QQ:623128629 2 // 文件名稱:ProcessExt.cs 3 //
創(chuàng)建日期:2019-08-15 16:02:44 4 // 功能描述:Process 5 // 項(xiàng)目地址:
https://gitee.com/kwwwvagaa/net_winform_custom_control 6 using System; 7 using
System.Collections.Generic; 8 using System.ComponentModel; 9 using
System.Drawing;10 using System.Data; 11 using System.Linq; 12 using System.Text;
13 using System.Windows.Forms; 14 15 namespace HZH_Controls.Controls 16 { 17
public partial class ProcessExt : UCControlBase 18 { 19 private int _value = 0;
20 21 public int Value 22 { 23 get { return this._value; } 24 set 25 { 26 if
(value <0) 27 return; 28 this._value = value; 29 SetValue(); 30 } 31 } 32 33
private int maxValue = 100; 34 35 public int MaxValue 36 { 37 get { return
maxValue; }38 set 39 { 40 if (value <= 0) 41 return; 42 maxValue = value; 43
SetValue();44 } 45 } 46 47 private void SetValue() 48 { 49 double dbl = (
double)_value / (double)maxValue; 50 this.panel1.Width = (int)(this.Width *
dbl);51 } 52 53 public ProcessExt() 54 { 55 InitializeComponent(); 56 } 57
58 private void ProcessExt_SizeChanged(object sender, EventArgs e) 59 { 60
SetValue();61 } 62 63 public void Step() 64 { 65 Value++; 66 } 67 } 68 }
View Code 1 namespace HZH_Controls.Controls 2 { 3 partial class ProcessExt 4
{ 5 /// <summary> 6 /// 必需的設(shè)計(jì)器變量。 7 /// </summary> 8 private
System.ComponentModel.IContainer components =null; 9 10 /// <summary> 11 ///
清理所有正在使用的資源。12 /// </summary> 13 /// <param name="disposing">如果應(yīng)釋放托管資源,為
true;否則為 false。</param> 14 protected override void Dispose(bool disposing) 15 {
16 if (disposing && (components != null)) 17 { 18 components.Dispose(); 19 }
20 base.Dispose(disposing); 21 } 22 23 #region 組件設(shè)計(jì)器生成的代碼 24 25 /// <summary>
26 /// 設(shè)計(jì)器支持所需的方法 - 不要 27 /// 使用代碼編輯器修改此方法的內(nèi)容。 28 /// </summary> 29 private void
InitializeComponent()30 { 31 this.panel1 = new System.Windows.Forms.Panel();
32 this.SuspendLayout(); 33 // 34 // panel1 35 // 36 this.panel1.BackColor =
System.Drawing.Color.FromArgb(((int)(((byte)(23)))), ((int)(((byte)(127)))), ((
int)(((byte)(203))))); 37 this.panel1.Dock =
System.Windows.Forms.DockStyle.Left;38 this.panel1.Location = new
System.Drawing.Point(0, 0); 39 this.panel1.Name = "panel1"; 40 this.panel1.Size
=new System.Drawing.Size(0, 22); 41 this.panel1.TabIndex = 0; 42 // 43 //
ProcessExt44 // 45 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
46 this.BackColor = System.Drawing.Color.White; 47 this.ConerRadius = 5; 48 this
.Controls.Add(this.panel1); 49 this.IsRadius = true; 50 this.Name = "ProcessExt"
;51 this.Size = new System.Drawing.Size(291, 22); 52 this.SizeChanged += new
System.EventHandler(this.ProcessExt_SizeChanged); 53 this.ResumeLayout(false);
54 55 } 56 57 #endregion 58 59 private System.Windows.Forms.Panel panel1; 60 }
61 } View Code
用處及效果
用處:就是一個(gè)進(jìn)度條
效果:
最后的話
如果你喜歡的話,請(qǐng)到?https://gitee.com/kwwwvagaa/net_winform_custom_control
<https://gitee.com/kwwwvagaa/net_winform_custom_control>?點(diǎn)個(gè)星 星吧
熱門工具 換一換