CircleControl.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Drawing.Drawing2D;
  11. namespace PaintDotNet.CustomControl
  12. {
  13. /// <summary>
  14. /// 位置导航的圆形控件
  15. /// </summary>
  16. public partial class CircleControl : UserControl
  17. {
  18. public int _position = -1;
  19. public List<int[]> positionMap;
  20. List<Pen> BtnPens = new List<Pen>();
  21. private GraphicsPath graphicsPath = new GraphicsPath();
  22. List<GraphicsPath> BtnPaths = new List<GraphicsPath>();
  23. public CircleControl(int radius)
  24. {
  25. SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor, true);
  26. InitializeComponent();
  27. Size = new Size(radius * 2 + 2, radius * 2 + 2);
  28. Paint += new PaintEventHandler(Control_Paint);
  29. MouseMove += new MouseEventHandler(Control_Move);
  30. MouseDown += new MouseEventHandler(Control_MouseDown);
  31. MouseUp += new MouseEventHandler(Control_MouseUp);
  32. MouseLeave += CircleControl_Leave;
  33. var smallRadius = (int)(radius * 1.1 / 2);
  34. var location = radius - smallRadius;
  35. double d = smallRadius * 2 * Math.Sin(22.5 * Math.PI / 180);
  36. double x1 = (smallRadius * 2 - d * 2) / 2;
  37. float d1 = (float)d;
  38. float x2 = (float)x1;
  39. for (int i = 0; i < 16; i++)
  40. {
  41. var path = new GraphicsPath();
  42. var start = 157.5f + i * 45;
  43. if (i < 8)
  44. {
  45. BtnPens.Add(new Pen(Color.Black, 1));
  46. path.AddPie(location, location, smallRadius * 2, smallRadius * 2, start, 45f);
  47. }
  48. else
  49. {
  50. BtnPens.Add(new Pen(Color.Black, 1));
  51. path.AddPie(0, 0, radius * 2, radius * 2, start, 45f);
  52. }
  53. BtnPaths.Add(path);
  54. }
  55. graphicsPath.AddEllipse(0, 0, radius * 2, radius * 2);
  56. positionMap = new List<int[]>();
  57. positionMap.Add(new int[] { -1, 0 });
  58. positionMap.Add(new int[] { -1, -1 });
  59. positionMap.Add(new int[] { 0, -1 });
  60. positionMap.Add(new int[] { 1, -1 });
  61. positionMap.Add(new int[] { 1, 0 });
  62. positionMap.Add(new int[] { 1, 1 });
  63. positionMap.Add(new int[] { 0, 1 });
  64. positionMap.Add(new int[] { -1, 1 });
  65. }
  66. private void CircleControl_Leave(object sender, EventArgs e)
  67. {
  68. InitPen();
  69. Refresh();
  70. }
  71. public event EventHandler mouseUp;
  72. public event EventHandler mouseDown;
  73. private void Control_MouseDown(object sender, MouseEventArgs e)
  74. {
  75. mouseDown?.Invoke(this, e);
  76. }
  77. private void Control_MouseUp(object sender, MouseEventArgs e)
  78. {
  79. mouseUp?.Invoke(this, e);
  80. }
  81. private void Control_Move(object sender, MouseEventArgs e)
  82. {
  83. InitPen();
  84. MoveEvent(e);
  85. Refresh();
  86. }
  87. private void Control_Paint(object sender, PaintEventArgs e)
  88. {
  89. e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
  90. var current = _position;
  91. for (int i = 15; i > -1; i--)
  92. {
  93. if (i != current)
  94. e.Graphics.DrawPath(BtnPens[i], BtnPaths[i]);
  95. }
  96. if (current > -1)
  97. e.Graphics.DrawPath(BtnPens[current], BtnPaths[current]);
  98. }
  99. private void InitPen()
  100. {
  101. if (_position > -1)
  102. {
  103. BtnPens[_position] = new Pen(Color.Black, 1);
  104. }
  105. }
  106. private void MoveEvent(MouseEventArgs e)
  107. {
  108. _position = -1;
  109. for (int i = 0; i < 16; i++)
  110. {
  111. if (BtnPaths[i].IsVisible(e.Location))
  112. {
  113. BtnPens[i] = new Pen(Color.Blue, 2);
  114. _position = i;
  115. return;
  116. }
  117. }
  118. }
  119. }
  120. }