前提
入行已經(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,如果你還不了解UCControlBase,請(qǐng)移步?(一)c#Winform自定義控件-基類控件
<https://www.cnblogs.com/bfyx/p/11361809.html>?查看
開始
添加一個(gè)用戶控件,命名UCPanelTitle,繼承自UCControlBase
2個(gè)屬性
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("面板標(biāo)題"), 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.Controls11 { 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("面板標(biāo)題"),
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 /// 必需的設(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.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
用處及效果
?
最后的話
如果你喜歡的話,請(qǐng)到?https://gitee.com/kwwwvagaa/net_winform_custom_control
<https://gitee.com/kwwwvagaa/net_winform_custom_control>?點(diǎn)個(gè)星 星吧
熱門工具 換一換