前提
入行已經(jīng)7,8年了,一直想做一套漂亮點的自定義控件,于是就有了本系列文章。
GitHub:https://github.com/kwwwvagaa/NetWinformControl
<https://github.com/kwwwvagaa/NetWinformControl>
碼云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
<https://gitee.com/kwwwvagaa/net_winform_custom_control.git>
如果覺得寫的還行,請點個 star 支持一下吧
歡迎前來交流探討: 企鵝群568015492?
<https://shang.qq.com/wpa/qunwpa?idkey=6e08741ef16fe53bf0314c1c9e336c4f626047943a8b76bac062361bab6b4f8d>
麻煩博客下方點個【推薦】,謝謝
NuGet
Install-Package HZH_Controls
目錄
https://www.cnblogs.com/bfyx/p/11364884.html
<https://www.cnblogs.com/bfyx/p/11364884.html>
用處及效果
準備工作
這個用的GDI+畫的,如果不了解,可以百度一下。
開始
添加一個類?UCWave ,繼承Control
定義屬性
1 private Color m_waveColor = Color.FromArgb(73, 119, 232); 2 3
[Description("波紋顏色"), Category("自定義")] 4 public Color WaveColor 5 { 6 get {
return m_waveColor; } 7 set { m_waveColor = value; } 8 } 9 10 private int
m_waveWidth =200; 11 /// <summary> 12 /// 為方便計算,強制使用10的倍數(shù) 13 /// </summary> 14
[Description("波紋寬度(為方便計算,強制使用10的倍數(shù))"), Category("自定義")] 15 public int WaveWidth
16 { 17 get { return m_waveWidth; } 18 set 19 { 20 m_waveWidth = value; 21
m_waveWidth = m_waveWidth /10 * 10; 22 intLeftX = value * -1; 23 } 24 } 25 26
private int m_waveHeight = 30; 27 /// <summary> 28 /// 波高 29 /// </summary> 30
[Description("波高"), Category("自定義")] 31 public int WaveHeight 32 { 33 get {
return m_waveHeight; } 34 set { m_waveHeight = value; } 35 } 36 37 private int
m_waveSleep =50; 38 /// <summary> 39 /// 波運行速度(運行時間間隔,毫秒) 40 /// </summary> 41
[Description("波運行速度(運行時間間隔,毫秒)"), Category("自定義")] 42 public int WaveSleep 43 {
44 get { return m_waveSleep; } 45 set 46 { 47 if (value <= 0) 48 return; 49
m_waveSleep = value; 50 if (timer != null) 51 { 52 timer.Enabled = false; 53
timer.Interval = value; 54 timer.Enabled = true; 55 } 56 } 57 } 58 59 Timer
timer =new Timer(); 60 int intLeftX = -200;
重繪
1 protected override void OnPaint(PaintEventArgs e) 2 { 3 base.OnPaint(e);
4 var g = e.Graphics; 5 g.SetGDIHigh(); 6 List<Point> lst1 = new List<Point>
(); 7 List<Point> lst2 = new List<Point>(); 8 int m_intX = intLeftX; 9 while (
true) 10 { 11 lst1.Add(new Point(m_intX, 1)); 12 lst1.Add(new Point(m_intX +
m_waveWidth /2, m_waveHeight)); 13 14 lst2.Add(new Point(m_intX + m_waveWidth /
4, 1)); 15 lst2.Add(new Point(m_intX + m_waveWidth / 4 + m_waveWidth / 2,
m_waveHeight));16 m_intX += m_waveWidth; 17 if (m_intX > this.Width +
m_waveWidth)18 break; 19 } 20 21 GraphicsPath path1 = new GraphicsPath(); 22
path1.AddCurve(lst1.ToArray(),0.5F); 23 path1.AddLine(this.Width + 1, -1, this
.Width +1, this.Height); 24 path1.AddLine(this.Width + 1, this.Height, -1, this
.Height);25 path1.AddLine(-1, this.Height, -1, -1); 26 27 GraphicsPath path2 =
new GraphicsPath(); 28 path2.AddCurve(lst2.ToArray(), 0.5F); 29 path2.AddLine(
this.Width + 1, -1, this.Width + 1, this.Height); 30 path2.AddLine(this.Width +
1, this.Height, -1, this.Height); 31 path2.AddLine(-1, this.Height, -1, -1); 32
33 g.FillPath(new SolidBrush(Color.FromArgb(220, m_waveColor.R, m_waveColor.G,
m_waveColor.B)), path1);34 g.FillPath(new SolidBrush(Color.FromArgb(220,
m_waveColor.R, m_waveColor.G, m_waveColor.B)), path2);35 }
為了“波動”,添加定時器,定時器事件如下
1 void timer_Tick(object sender, EventArgs e) 2 { 3 intLeftX -= 10; 4 if
(intLeftX == m_waveWidth * -2) 5 intLeftX = m_waveWidth * -1; 6 this.Refresh();
7 }
完整代碼
1 using System; 2 using System.Collections.Generic; 3 using
System.ComponentModel; 4 using System.Drawing; 5 using
System.Drawing.Drawing2D; 6 using System.Linq; 7 using System.Text; 8 using
System.Windows.Forms; 9 10 namespace HZH_Controls.Controls 11 { 12 public
class UCWave : Control 13 { 14 private Color m_waveColor = Color.FromArgb(73,
119, 232); 15 16 [Description("波紋顏色"), Category("自定義")] 17 public Color
WaveColor 18 { 19 get { return m_waveColor; } 20 set { m_waveColor = value; }
21 } 22 23 private int m_waveWidth = 200; 24 /// <summary> 25 ///
為方便計算,強制使用10的倍數(shù) 26 /// </summary> 27 [Description("波紋寬度(為方便計算,強制使用10的倍數(shù))"),
Category("自定義")] 28 public int WaveWidth 29 { 30 get { return m_waveWidth; }
31 set 32 { 33 m_waveWidth = value; 34 m_waveWidth = m_waveWidth / 10 * 10;
35 intLeftX = value * -1; 36 } 37 } 38 39 private int m_waveHeight = 30;
40 /// <summary> 41 /// 波高 42 /// </summary> 43 [Description("波高"),
Category("自定義")] 44 public int WaveHeight 45 { 46 get { return
m_waveHeight; } 47 set { m_waveHeight = value; } 48 } 49 50 private int
m_waveSleep =50; 51 /// <summary> 52 /// 波運行速度(運行時間間隔,毫秒) 53 /// </summary>
54 [Description("波運行速度(運行時間間隔,毫秒)"), Category("自定義")] 55 public int WaveSleep
56 { 57 get { return m_waveSleep; } 58 set 59 { 60 if (value <= 0) 61
return; 62 m_waveSleep = value; 63 if (timer != null) 64 { 65
timer.Enabled =false; 66 timer.Interval = value; 67 timer.Enabled = true; 68
} 69 } 70 } 71 72 Timer timer = new Timer(); 73 int intLeftX = -200; 74
public UCWave() 75 { 76 this.Size = new Size(600, 100); 77 this
.SetStyle(ControlStyles.AllPaintingInWmPaint,true); 78 this
.SetStyle(ControlStyles.DoubleBuffer,true); 79 this
.SetStyle(ControlStyles.ResizeRedraw,true); 80 this
.SetStyle(ControlStyles.Selectable,true); 81 this
.SetStyle(ControlStyles.SupportsTransparentBackColor,true); 82 this
.SetStyle(ControlStyles.UserPaint,true); 83 timer.Interval = m_waveSleep; 84
timer.Tick += timer_Tick; 85 this.VisibleChanged += UCWave_VisibleChanged; 86
} 87 88 void UCWave_VisibleChanged(object sender, EventArgs e) 89 { 90
timer.Enabled =this.Visible; 91 } 92 93 void timer_Tick(object sender,
EventArgs e) 94 { 95 intLeftX -= 10; 96 if (intLeftX == m_waveWidth * -2) 97
intLeftX = m_waveWidth * -1; 98 this.Refresh(); 99 } 100 protected override
void OnPaint(PaintEventArgs e) 101 { 102 base.OnPaint(e); 103 var g =
e.Graphics;104 g.SetGDIHigh(); 105 List<Point> lst1 = new List<Point>(); 106
List<Point> lst2 =new List<Point>(); 107 int m_intX = intLeftX; 108 while (true)
109 { 110 lst1.Add(new Point(m_intX, 1)); 111 lst1.Add(new Point(m_intX +
m_waveWidth /2, m_waveHeight)); 112 113 lst2.Add(new Point(m_intX + m_waveWidth
/4, 1)); 114 lst2.Add(new Point(m_intX + m_waveWidth / 4 + m_waveWidth / 2,
m_waveHeight));115 m_intX += m_waveWidth; 116 if (m_intX > this.Width +
m_waveWidth)117 break; 118 } 119 120 GraphicsPath path1 = new GraphicsPath();
121 path1.AddCurve(lst1.ToArray(), 0.5F); 122 path1.AddLine(this.Width + 1, -1,
this.Width + 1, this.Height); 123 path1.AddLine(this.Width + 1, this.Height, -1,
this.Height); 124 path1.AddLine(-1, this.Height, -1, -1); 125 126 GraphicsPath
path2 =new GraphicsPath(); 127 path2.AddCurve(lst2.ToArray(), 0.5F); 128
path2.AddLine(this.Width + 1, -1, this.Width + 1, this.Height); 129
path2.AddLine(this.Width + 1, this.Height, -1, this.Height); 130 path2.AddLine(-
1, this.Height, -1, -1); 131 132 g.FillPath(new SolidBrush(Color.FromArgb(220,
m_waveColor.R, m_waveColor.G, m_waveColor.B)), path1);133 g.FillPath(new
SolidBrush(Color.FromArgb(220, m_waveColor.R, m_waveColor.G, m_waveColor.B)),
path2);134 } 135 } 136 } View Code
?
最后的話
如果你喜歡的話,請到?https://gitee.com/kwwwvagaa/net_winform_custom_control
<https://gitee.com/kwwwvagaa/net_winform_custom_control>?點個星星吧
熱門工具 換一換
感谢您访问我们的网站,您可能还对以下资源感兴趣:
调教肉文小说-国产成本人片免费av-空姐av种子无码-在线观看免费午夜视频-综合久久精品激情-国产成人丝袜视频在线观看软件-大芭区三区四区无码-啊啊好爽啊啊插啊用力啊啊-wanch视频网-国产精品成人a免费观看