C × WinForm custom control - rotor

Keywords: github git

;

Official website

http://www.hzhcontrols.com/

premise

It's been 7 or 8 years since I started, I always want to make a set of beautiful custom controls, so I have this series of articles.

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

Code cloud: https://gitee.com/kwwwvagaa/net_winform_custom_control.git

If you think it's OK, please click star to support it

Welcome to discuss: Penguins 568015492

Here we are. Please order a recommendation before you leave. Thank you

NuGet

Install-Package HZH_Controls

catalog

http://www.hzhcontrols.com/blog-63.html

Use and effect

 

Preparations

I don't have any preparation. Open it

start

Add a user control UCRotor

Add the following properties

 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("Rotation speed, 100-1000,The smaller the value, the faster the speed"), Category("custom")]
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         }

Size change event handling

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         }

And then it's redrawn

 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             // Rotate Sketchpad
 8             g.RotateTransform(jiaodu);
 9             // Back to Sketchpad x,y Distance of axis movement
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         }

Add a Timer to rotate

 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         }

Last words

If you like, please https://gitee.com/kwwwvagaa/net_winform_custom_control Point a star

Posted by kaeRock on Sun, 17 May 2020 08:38:32 -0700