前提

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

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

码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

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

欢迎前来交流探讨: 企鹅群568015492 (八十五)c#Winform自定义控件-引用区块-LMLPHP

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

NuGet

Install-Package HZH_Controls

目录

https://www.cnblogs.com/bfyx/p/11364884.html

用处及效果

(八十五)c#Winform自定义控件-引用区块-LMLPHP

准备工作

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

开始

添加一个类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         }

最后的话

如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星星吧

10-15 19:42