官網 http://www.hzhcontrols.com/ 前提 入行已經7,8年了,一直想做一套漂亮點的自定義控制項,於是就有了本系列文章。 GitHub:https://github.com/kwwwvagaa/NetWinformControl 碼雲:https://gitee.com/kww ...
官網
前提
入行已經7,8年了,一直想做一套漂亮點的自定義控制項,於是就有了本系列文章。
GitHub:https://github.com/kwwwvagaa/NetWinformControl
碼雲:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
如果覺得寫的還行,請點個 star 支持一下吧
來都來了,點個【推薦】再走吧,謝謝
NuGet
Install-Package HZH_Controls
目錄
http://www.hzhcontrols.com/blog-63.html
用處及效果
準備工作
也沒什麼準備的,開擼
開始
添加一個用戶控制項UCRotor
添加一下屬性
1 private Color rotorColor = Color.Black; 2 3 public Color RotorColor 4 { 5 get { return rotorColor; } 6 set 7 { 8 rotorColor = value; 9 Refresh(); 10 } 11 } 12 13 RotorAround rotorAround = RotorAround.None; 14 int jiaodu = 0; 15 public RotorAround RotorAround 16 { 17 get { return rotorAround; } 18 set 19 { 20 rotorAround = value; 21 if (value == RotorAround.None) 22 { 23 timer1.Enabled = false; 24 jiaodu = 0; 25 Refresh(); 26 } 27 else 28 timer1.Enabled = true; 29 } 30 } 31 private int speed = 100; 32 33 [Description("旋轉速度,100-1000,值越小 速度越快"), Category("自定義")] 34 public int Speed 35 { 36 get { return speed; } 37 set 38 { 39 if (value < 100 || value > 1000) 40 return; 41 speed = value; 42 timer1.Interval = value; 43 } 44 }
大小改變事件處理一下
1 void UCRotor_SizeChanged(object sender, EventArgs e) 2 { 3 maxWidth = Math.Min(this.Width, this.Height); 4 one = maxWidth / 10; 5 ResetPathCache(); 6 7 }
然後就是重繪了
1 protected override void OnPaint(PaintEventArgs e) 2 { 3 base.OnPaint(e); 4 var g = e.Graphics; 5 this.Region = new System.Drawing.Region(lstCachePath[jiaodu]); 6 g.TranslateTransform(Width / 2, Height / 2); 7 // 旋轉畫板 8 g.RotateTransform(jiaodu); 9 // 回退畫板x,y軸移動過的距離 10 g.TranslateTransform(-(Width / 2), -(Height / 2)); 11 g.FillEllipse(new SolidBrush(rotorColor), new Rectangle((this.Width - maxWidth) / 2+5, (this.Height - maxWidth) / 2 + maxWidth / 4 + maxWidth / 8+2, maxWidth / 2-5, maxWidth / 2 - maxWidth / 4-4)); 12 g.FillEllipse(new SolidBrush(rotorColor), new Rectangle(this.Width / 2, (this.Height - maxWidth) / 2 + maxWidth / 4 + maxWidth / 8+2, maxWidth / 2-5, maxWidth / 2 - maxWidth / 4-4)); 13 g.FillEllipse(new SolidBrush(rotorColor), new Rectangle((this.Width - maxWidth) / 2 + maxWidth / 2 - maxWidth / 8, (this.Height - maxWidth) / 2 + maxWidth / 2 - maxWidth / 8, maxWidth / 4, maxWidth / 4)); 14 g.FillEllipse(new SolidBrush(Color.FromArgb(10, Color.White)), new Rectangle((this.Width - maxWidth) / 2 + maxWidth / 2 - maxWidth / 8, (this.Height - maxWidth) / 2 + maxWidth / 2 - maxWidth / 8, maxWidth / 4, maxWidth / 4)); 15 16 }
添加一個Timer用以旋轉
1 private void timer1_Tick(object sender, EventArgs e) 2 { 3 if (rotorAround == RotorAround.Clockwise) 4 { 5 jiaodu += 15; 6 if (jiaodu == 180) 7 jiaodu = 0; 8 } 9 else if (rotorAround == RotorAround.Counterclockwise) 10 { 11 jiaodu -= 15; 12 if (jiaodu < 0) 13 jiaodu = 165; 14 } 15 16 Refresh(); 17 }
最後的話
如果你喜歡的話,請到 https://gitee.com/kwwwvagaa/net_winform_custom_control 點個星星吧