前提

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

开源地址:

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

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

目录

准备工作

用到了控件基类 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 }
 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 }

用处及效果

 

最后的话

如果你喜欢的话,请到  点个星 星吧