前提

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

github:https://github.com/kwwwvagaa/netwinformcontrol

码云:

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

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

来都来了,点个【推荐】再走吧,谢谢

nuget

install-package hzh_controls

目录

用处及效果

准备工作

没什么可准备的,直接往下看吧

开始

添加一个类ucpanelquote继承 panel

添加2个属性

 1 /// <summary>
 2         /// the border color
 3         /// </summary>
 4         private color bordercolor = linecolors.light;
 5 
 6         /// <summary>
 7         /// gets or sets the color of the border.
 8         /// </summary>
 9         /// <value>the color of the border.</value>
10         [description("边框颜色"), category("自定义")]
11         public color bordercolor
12         {
13             get { return bordercolor; }
14             set
15             {
16                 bordercolor = value;
17                 this.invalidate();
18             }
19         }
20 
21         /// <summary>
22         /// the left color
23         /// </summary>
24         private color leftcolor = statuscolors.danger;
25 
26         /// <summary>
27         /// gets or sets the color of the left.
28         /// </summary>
29         /// <value>the color of the left.</value>
30         [description("左侧颜色"), category("自定义")]
31         public color leftcolor
32         {
33             get { return leftcolor; }
34             set
35             {
36                 leftcolor = value;
37                 this.invalidate();
38             }
39         }

为了画边框和左边的颜色,设置一下padding

1    public ucpanelquote()
2             : base()
3         {
4             padding = new padding(5, 1, 1, 1);
5         }

重绘

 1 protected override void onpaint(painteventargs e)
 2         {
 3             base.onpaint(e);
 4             e.graphics.setgdihigh();
 5 
 6             e.graphics.drawlines(new pen(bordercolor), new point[] 
 7             { 
 8                 new point(e.cliprectangle.left,e.cliprectangle.top),
 9                 new point(e.cliprectangle.right-1,e.cliprectangle.top),
10                 new point(e.cliprectangle.right-1,e.cliprectangle.bottom-1),
11                 new point(e.cliprectangle.left,e.cliprectangle.bottom-1),
12                 new point(e.cliprectangle.left,e.cliprectangle.top)
13             });
14 
15             e.graphics.fillrectangle(new solidbrush(leftcolor), new rectangle(0, 0, 5, this.height));
16         }

 

最后的话

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