http://www.hzhcontrols.comhtml
入行已經7,8年了,一直想作一套漂亮點的自定義控件,因而就有了本系列文章。git
GitHub:https://github.com/kwwwvagaa/NetWinformControlgithub
碼雲:https://gitee.com/kwwwvagaa/net_winform_custom_control.gitide
若是以爲寫的還行,請點個 star 支持一下吧this
來都來了,點個【推薦】再走吧,謝謝code
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 點個星星吧