前提

入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。

开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control

如果觉得写的还行,请点个 star 支持一下吧

欢迎前来交流探讨: 企鹅群568015492 企鹅群568015492

目录

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 点个星 星吧

内容来源于网络如有侵权请私信删除
你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!