前提 入行已經7,8年了,一直想做一套漂亮點的自定義控制項,於是就有了本系列文章。 開源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control 如果覺得寫的還行,請點個 star 支持一下吧 歡迎前來交流探討: 企鵝群568015492 目錄 ...
前提
入行已經7,8年了,一直想做一套漂亮點的自定義控制項,於是就有了本系列文章。
開源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control
如果覺得寫的還行,請點個 star 支持一下吧
目錄
https://www.cnblogs.com/bfyx/p/11364884.html
準備工作
用到了控制項基類 UCControlBase,如果你還不瞭解UCControlBase,請移步 (一)c#Winform自定義控制項-基類控制項 查看
開始
添加一個用戶控制項,命名UCPanelTitle,繼承自UCControlBase
2個屬性
1 [Description("邊框顏色"), Category("自定義")] 2 public Color BorderColor 3 { 4 get { return this.RectColor; } 5 set 6 { 7 this.RectColor = value; 8 this.lblTitle.BackColor = value; 9 } 10 } 11 12 [Description("面板標題"), Category("自定義")] 13 public string Title 14 { 15 get { return lblTitle.Text; } 16 set { lblTitle.Text = value; } 17 }
全部代碼
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Drawing; 5 using System.Data; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 10 namespace HZH_Controls.Controls 11 { 12 public partial class UCPanelTitle : UCControlBase 13 { 14 [Description("邊框顏色"), Category("自定義")] 15 public Color BorderColor 16 { 17 get { return this.RectColor; } 18 set 19 { 20 this.RectColor = value; 21 this.lblTitle.BackColor = value; 22 } 23 } 24 25 [Description("面板標題"), Category("自定義")] 26 public string Title 27 { 28 get { return lblTitle.Text; } 29 set { lblTitle.Text = value; } 30 } 31 public UCPanelTitle() 32 { 33 InitializeComponent(); 34 } 35 } 36 }View Code
1 namespace HZH_Controls.Controls 2 { 3 partial class UCPanelTitle 4 { 5 /// <summary> 6 /// 必需的設計器變數。 7 /// </summary> 8 private System.ComponentModel.IContainer components = null; 9 10 /// <summary> 11 /// 清理所有正在使用的資源。 12 /// </summary> 13 /// <param name="disposing">如果應釋放托管資源,為 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 組件設計器生成的代碼 24 25 /// <summary> 26 /// 設計器支持所需的方法 - 不要 27 /// 使用代碼編輯器修改此方法的內容。 28 /// </summary> 29 private void InitializeComponent() 30 { 31 this.lblTitle = new System.Windows.Forms.Label(); 32 this.SuspendLayout(); 33 // 34 // lblTitle 35 // 36 this.lblTitle.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(22)))), ((int)(((byte)(160)))), ((int)(((byte)(133))))); 37 this.lblTitle.Dock = System.Windows.Forms.DockStyle.Top; 38 this.lblTitle.ForeColor = System.Drawing.Color.White; 39 this.lblTitle.Location = new System.Drawing.Point(0, 0); 40 this.lblTitle.Name = "lblTitle"; 41 this.lblTitle.Size = new System.Drawing.Size(432, 34); 42 this.lblTitle.TabIndex = 0; 43 this.lblTitle.Text = "面板"; 44 this.lblTitle.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; 45 // 46 // UCPanelTitle 47 // 48 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None; 49 this.BackColor = System.Drawing.Color.Transparent; 50 this.ConerRadius = 10; 51 this.Controls.Add(this.lblTitle); 52 this.FillColor = System.Drawing.Color.White; 53 this.IsRadius = true; 54 this.IsShowRect = true; 55 this.Name = "UCPanelTitle"; 56 this.RectColor = System.Drawing.Color.FromArgb(((int)(((byte)(22)))), ((int)(((byte)(160)))), ((int)(((byte)(133))))); 57 this.Size = new System.Drawing.Size(432, 301); 58 this.ResumeLayout(false); 59 60 } 61 62 #endregion 63 64 private System.Windows.Forms.Label lblTitle; 65 } 66 }View Code
用處及效果
最後的話
如果你喜歡的話,請到 https://gitee.com/kwwwvagaa/net_winform_custom_control 點個星 星吧