(八十五)c#Winform自定義控件-引用區塊

官網

http://www.hzhcontrols.comhtml

前提

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

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

碼雲:https://gitee.com/kwwwvagaa/net_winform_custom_control.gitide

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

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

來都來了,點個【推薦】再走吧,謝謝code

NuGet

Install-Package HZH_Controls

目錄

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

用處及效果

準備工做

沒什麼可準備的,直接往下看吧htm

開始

添加一個類UCPanelQuote繼承 Panelblog

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

相關文章
相關標籤/搜索