123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Drawing;
- using System.Data;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Drawing.Drawing2D;
- namespace PaintDotNet.CustomControl
- {
- /// <summary>
- /// 位置导航的圆形控件
- /// </summary>
- public partial class CircleControl : UserControl
- {
- public int _position = -1;
- public List<int[]> positionMap;
- List<Pen> BtnPens = new List<Pen>();
- private GraphicsPath graphicsPath = new GraphicsPath();
- List<GraphicsPath> BtnPaths = new List<GraphicsPath>();
- public CircleControl(int radius)
- {
- SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor, true);
- InitializeComponent();
- Size = new Size(radius * 2 + 2, radius * 2 + 2);
- Paint += new PaintEventHandler(Control_Paint);
- MouseMove += new MouseEventHandler(Control_Move);
- MouseDown += new MouseEventHandler(Control_MouseDown);
- MouseUp += new MouseEventHandler(Control_MouseUp);
- MouseLeave += CircleControl_Leave;
- var smallRadius = (int)(radius * 1.1 / 2);
- var location = radius - smallRadius;
- double d = smallRadius * 2 * Math.Sin(22.5 * Math.PI / 180);
- double x1 = (smallRadius * 2 - d * 2) / 2;
- float d1 = (float)d;
- float x2 = (float)x1;
- for (int i = 0; i < 16; i++)
- {
- var path = new GraphicsPath();
- var start = 157.5f + i * 45;
- if (i < 8)
- {
- BtnPens.Add(new Pen(Color.Black, 1));
- path.AddPie(location, location, smallRadius * 2, smallRadius * 2, start, 45f);
- }
- else
- {
- BtnPens.Add(new Pen(Color.Black, 1));
- path.AddPie(0, 0, radius * 2, radius * 2, start, 45f);
- }
- BtnPaths.Add(path);
- }
- graphicsPath.AddEllipse(0, 0, radius * 2, radius * 2);
- positionMap = new List<int[]>();
- positionMap.Add(new int[] { -1, 0 });
- positionMap.Add(new int[] { -1, -1 });
- positionMap.Add(new int[] { 0, -1 });
- positionMap.Add(new int[] { 1, -1 });
- positionMap.Add(new int[] { 1, 0 });
- positionMap.Add(new int[] { 1, 1 });
- positionMap.Add(new int[] { 0, 1 });
- positionMap.Add(new int[] { -1, 1 });
- }
- private void CircleControl_Leave(object sender, EventArgs e)
- {
- InitPen();
- Refresh();
- }
- public event EventHandler mouseUp;
- public event EventHandler mouseDown;
- private void Control_MouseDown(object sender, MouseEventArgs e)
- {
- mouseDown?.Invoke(this, e);
- }
- private void Control_MouseUp(object sender, MouseEventArgs e)
- {
- mouseUp?.Invoke(this, e);
- }
- private void Control_Move(object sender, MouseEventArgs e)
- {
- InitPen();
- MoveEvent(e);
- Refresh();
- }
- private void Control_Paint(object sender, PaintEventArgs e)
- {
- e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
- var current = _position;
- for (int i = 15; i > -1; i--)
- {
- if (i != current)
- e.Graphics.DrawPath(BtnPens[i], BtnPaths[i]);
- }
- if (current > -1)
- e.Graphics.DrawPath(BtnPens[current], BtnPaths[current]);
- }
- private void InitPen()
- {
- if (_position > -1)
- {
- BtnPens[_position] = new Pen(Color.Black, 1);
- }
- }
- private void MoveEvent(MouseEventArgs e)
- {
- _position = -1;
- for (int i = 0; i < 16; i++)
- {
- if (BtnPaths[i].IsVisible(e.Location))
- {
- BtnPens[i] = new Pen(Color.Blue, 2);
- _position = i;
- return;
- }
- }
- }
- }
- }
|