(三十七)c#Winform自定義控件-有標題的面板

官網

http://www.hzhcontrols.comhtml

前提

入行已經7,8年了,一直想作一套漂亮點的自定義控件,因而就有了本系列文章。git

GitHub:https://github.com/kwwwvagaa/NetWinformControlgithub

碼雲:https://gitee.com/kwwwvagaa/net_winform_custom_control.gitc#

若是以爲寫的還行,請點個 star 支持一下吧編輯器

歡迎前來交流探討: 企鵝羣568015492 企鵝羣568015492ide

目錄

http://www.javashuo.com/article/p-hacmmtru-mw.htmlpost

準備工做

用到了控件基類 UCControlBase,若是你還不瞭解UCControlBase,請移步 (一)c#Winform自定義控件-基類控件 查看this

開始

添加一個用戶控件,命名UCPanelTitle,繼承自UCControlBasespa

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 點個星 星吧

相關文章
相關標籤/搜索