using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace SmartCoalApplication.Core.CustomControl
{
///
/// 位置导航的圆形控件
///
public partial class CircleControl : UserControl
{
///
/// 半径
///
private int radius;
private int smallRadius;
private int location;
public int position = 0;
private Pen internalpen1 = new Pen(Color.Pink);
private Pen internalpen2 = new Pen(Color.Pink);
private Pen internalpen3 = new Pen(Color.Pink);
private Pen internalpen4 = new Pen(Color.Pink);
private Pen internalpen5 = new Pen(Color.Pink);
private Pen internalpen6 = new Pen(Color.Pink);
private Pen internalpen7 = new Pen(Color.Pink);
private Pen internalpen8 = new Pen(Color.Pink);
private Pen outpen1 = new Pen(Color.Black);
private Pen outpen2 = new Pen(Color.Black);
private Pen outpen3 = new Pen(Color.Black);
private Pen outpen4 = new Pen(Color.Black);
private Pen outpen5 = new Pen(Color.Black);
private Pen outpen6 = new Pen(Color.Black);
private Pen outpen7 = new Pen(Color.Black);
private Pen outpen8 = new Pen(Color.Black);
private GraphicsPath graphicsPath = new GraphicsPath();
GraphicsPath internalLeft = new GraphicsPath();
GraphicsPath internalLeftTop = new GraphicsPath();
GraphicsPath internalTop = new GraphicsPath();
GraphicsPath internalRightTop = new GraphicsPath();
GraphicsPath internalRight = new GraphicsPath();
GraphicsPath internalRightBottom = new GraphicsPath();
GraphicsPath internalBottom = new GraphicsPath();
GraphicsPath internalLeftBottom = new GraphicsPath();
GraphicsPath outLeft = new GraphicsPath();
GraphicsPath outLeftTop = new GraphicsPath();
GraphicsPath outTop = new GraphicsPath();
GraphicsPath outRightTop = new GraphicsPath();
GraphicsPath outRight = new GraphicsPath();
GraphicsPath outRightBottom = new GraphicsPath();
GraphicsPath outBottom = new GraphicsPath();
GraphicsPath outLeftBottom = new GraphicsPath();
public CircleControl(int radius)
{
SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor, true);
InitializeComponent();
this.Size = new Size(radius*2+2, radius*2+2);
this.radius = radius;
this.smallRadius = (radius - 10)/2;
this.location = (radius * 2 - smallRadius * 2) / 2;
this.Paint += new PaintEventHandler(this.Control_Paint);
this.MouseMove += new MouseEventHandler(this.Control_Move);
this.MouseLeave += new EventHandler(this.Control_Leave);
this.MouseClick += new MouseEventHandler(this.Control_Click);
this.MouseDown += new MouseEventHandler(this.Control_MouseDown);
this.MouseUp += new MouseEventHandler(this.Control_MouseUp);
//double d = smallRadius / Math.Tan(90.0-22.5);
//double x = Math.Sqrt(Math.Pow(d,2) - Math.Pow(smallRadius, 2));
//int x1 = (int)((smallRadius * 2 - x) / 2);
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;
internalLeft.AddPie(location, location, smallRadius*2, smallRadius * 2, 157.5f, 45f);
internalLeftTop.AddPie(location, location, smallRadius*2, smallRadius * 2, 202.5f, 45f);
internalTop.AddPie(location, location, smallRadius * 2, smallRadius * 2, 247.5f, 45f);
internalRightTop.AddPie(location, location, smallRadius * 2, smallRadius * 2, 292.5f, 45f);
internalRight.AddPie(location, location, smallRadius * 2, smallRadius * 2, 337.5f, 45f);
internalRightBottom.AddPie(location, location, smallRadius * 2, smallRadius * 2, 22.5f, 45f);
internalBottom.AddPie(location, location, smallRadius * 2, smallRadius * 2, 67.5f, 45f);
internalLeftBottom.AddPie(location, location, smallRadius * 2, smallRadius * 2, 112.5f, 45f);
outLeft.AddPie(0, 0, radius*2, radius * 2, 157.5f, 45f);
outLeftTop.AddPie(0, 0, radius * 2, radius * 2, 202.5f, 45f);
outTop.AddPie(0, 0, radius * 2, radius * 2, 247.5f, 45f);
outRightTop.AddPie(0, 0, radius * 2, radius * 2, 292.5f, 45f);
outRight.AddPie(0, 0, radius * 2, radius * 2, 337.5f, 45f);
outRightBottom.AddPie(0, 0, radius * 2, radius * 2, 22.5f, 45f);
outBottom.AddPie(0, 0, radius * 2, radius * 2, 67.5f, 45f);
outLeftBottom.AddPie(0, 0, radius * 2, radius * 2, 112.5f, 45f);
graphicsPath.AddEllipse(0, 0, radius * 2, radius * 2);
}
public event EventHandler clicked;
public event EventHandler mouseUp;
public event EventHandler mouseDown;
private void Control_MouseDown(object sender, MouseEventArgs e)
{
OutMouseUpOrDown(e);
if (!graphicsPath.IsVisible(e.Location))
{
this.Refresh();
this.position = 9;
}
else
{
if (mouseDown != null)
{
mouseDown(this, e);
}
Console.WriteLine(position.ToString());
}
}
private void Control_MouseUp(object sender, MouseEventArgs e)
{
OutMouseUpOrDown(e);
if (!graphicsPath.IsVisible(e.Location))
{
this.Refresh();
this.position = 9;
}
else
{
if (mouseUp != null)
{
mouseUp(this, e);
}
Console.WriteLine(position.ToString());
}
}
private void Control_Click(object sender, MouseEventArgs e)
{
InternalClickEvent(e);
OutClickEvent(e);
if (!graphicsPath.IsVisible(e.Location))
{
this.Refresh();
this.position = 0;
}
else
{
if (clicked != null)
{
clicked(this, e);
}
Console.WriteLine(position.ToString());
//MessageBox.Show("" + position);
}
}
private void Control_Leave(object sender, EventArgs e)
{
InitPen();
this.position = 0;
}
private void Control_Move(object sender, MouseEventArgs e)
{
InitPen();
InternalMoveEvent(e);
OutMoveEvent(e);
if(!graphicsPath.IsVisible(e.Location))
{
InitPen();
this.Refresh();
}
}
private void Control_Paint(object sender, PaintEventArgs e)
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.DrawPath(outpen1, outLeft);
e.Graphics.DrawPath(outpen2, outLeftTop);
e.Graphics.DrawPath(outpen3, outTop);
e.Graphics.DrawPath(outpen4, outRightTop);
e.Graphics.DrawPath(outpen5, outRight);
e.Graphics.DrawPath(outpen6, outRightBottom);
e.Graphics.DrawPath(outpen7, outBottom);
e.Graphics.DrawPath(outpen8, outLeftBottom);
e.Graphics.DrawPath(internalpen1, internalLeft);
e.Graphics.DrawPath(internalpen2, internalLeftTop);
e.Graphics.DrawPath(internalpen3, internalTop);
e.Graphics.DrawPath(internalpen4, internalRightTop);
e.Graphics.DrawPath(internalpen5, internalRight);
e.Graphics.DrawPath(internalpen6, internalRightBottom);
e.Graphics.DrawPath(internalpen7, internalBottom);
e.Graphics.DrawPath(internalpen8, internalLeftBottom);
//e.Graphics.DrawEllipse(internalpen, 0, 0, radius*2, radius*2);
//e.Graphics.DrawEllipse(internalpen, location, location, smallRadius*2, smallRadius*2);
}
private void InitPen()
{
internalpen1 = new Pen(Color.Pink);
internalpen2 = new Pen(Color.Pink);
internalpen3 = new Pen(Color.Pink);
internalpen4 = new Pen(Color.Pink);
internalpen5 = new Pen(Color.Pink);
internalpen6 = new Pen(Color.Pink);
internalpen7 = new Pen(Color.Pink);
internalpen8 = new Pen(Color.Pink);
outpen1 = new Pen(Color.Black);
outpen2 = new Pen(Color.Black);
outpen3 = new Pen(Color.Black);
outpen4 = new Pen(Color.Black);
outpen5 = new Pen(Color.Black);
outpen6 = new Pen(Color.Black);
outpen7 = new Pen(Color.Black);
outpen8 = new Pen(Color.Black);
}
private void InternalMoveEvent(MouseEventArgs e)
{
if (internalLeft.IsVisible(e.Location))
{
internalpen1 = new Pen(Color.Blue);
this.Refresh();
return;
}
if (internalLeftTop.IsVisible(e.Location))
{
internalpen2 = new Pen(Color.Blue);
this.Refresh();
return;
}
if (internalTop.IsVisible(e.Location))
{
internalpen3 = new Pen(Color.Blue);
this.Refresh();
return;
}
if (internalRightTop.IsVisible(e.Location))
{
internalpen4 = new Pen(Color.Blue);
this.Refresh();
return;
}
if (internalRight.IsVisible(e.Location))
{
internalpen5 = new Pen(Color.Blue);
this.Refresh();
return;
}
if (internalRightBottom.IsVisible(e.Location))
{
internalpen6 = new Pen(Color.Blue);
this.Refresh();
return;
}
if (internalBottom.IsVisible(e.Location))
{
internalpen7 = new Pen(Color.Blue);
this.Refresh();
return;
}
if (internalLeftBottom.IsVisible(e.Location))
{
internalpen8 = new Pen(Color.Blue);
this.Refresh();
return;
}
}
private void OutMoveEvent(MouseEventArgs e)
{
if (outLeft.IsVisible(e.Location) && !internalLeft.IsVisible(e.Location))
{
outpen1 = new Pen(Color.Red);
this.Refresh();
return;
}
if (outLeftTop.IsVisible(e.Location) && !internalLeftTop.IsVisible(e.Location))
{
outpen2 = new Pen(Color.Red);
this.Refresh();
return;
}
if (outTop.IsVisible(e.Location) && !internalTop.IsVisible(e.Location))
{
outpen3 = new Pen(Color.Red);
this.Refresh();
return;
}
if (outRightTop.IsVisible(e.Location) && !internalRightTop.IsVisible(e.Location))
{
outpen4 = new Pen(Color.Red);
this.Refresh();
return;
}
if (outRight.IsVisible(e.Location) && !internalRight.IsVisible(e.Location))
{
outpen5 = new Pen(Color.Red);
this.Refresh();
return;
}
if (outRightBottom.IsVisible(e.Location) && !internalRightBottom.IsVisible(e.Location))
{
outpen6 = new Pen(Color.Red);
this.Refresh();
return;
}
if (outBottom.IsVisible(e.Location) && !internalBottom.IsVisible(e.Location))
{
outpen7 = new Pen(Color.Red);
this.Refresh();
return;
}
if (outLeftBottom.IsVisible(e.Location) && !internalLeftBottom.IsVisible(e.Location))
{
outpen8 = new Pen(Color.Red);
this.Refresh();
return;
}
}
private void InternalClickEvent(MouseEventArgs e)
{
if (internalLeft.IsVisible(e.Location))
{
internalpen1 = new Pen(Color.Blue);
this.Refresh();
this.position = 1;
return;
}
if (internalLeftTop.IsVisible(e.Location))
{
internalpen2 = new Pen(Color.Blue);
this.Refresh();
this.position = 2;
return;
}
if (internalTop.IsVisible(e.Location))
{
internalpen3 = new Pen(Color.Blue);
this.Refresh();
this.position = 3;
return;
}
if (internalRightTop.IsVisible(e.Location))
{
internalpen4 = new Pen(Color.Blue);
this.Refresh();
this.position = 4;
return;
}
if (internalRight.IsVisible(e.Location))
{
internalpen5 = new Pen(Color.Blue);
this.Refresh();
this.position = 5;
return;
}
if (internalRightBottom.IsVisible(e.Location))
{
internalpen6 = new Pen(Color.Blue);
this.Refresh();
this.position = 6;
return;
}
if (internalBottom.IsVisible(e.Location))
{
internalpen7 = new Pen(Color.Blue);
this.Refresh();
this.position = 7;
return;
}
if (internalLeftBottom.IsVisible(e.Location))
{
internalpen8 = new Pen(Color.Blue);
this.Refresh();
this.position = 8;
return;
}
}
private void OutClickEvent(MouseEventArgs e)
{
if (outLeft.IsVisible(e.Location) && !internalLeft.IsVisible(e.Location))
{
outpen1 = new Pen(Color.Red);
this.Refresh();
this.position = 9;
return;
}
if (outLeftTop.IsVisible(e.Location) && !internalLeftTop.IsVisible(e.Location))
{
outpen2 = new Pen(Color.Red);
this.Refresh();
this.position = 10;
return;
}
if (outTop.IsVisible(e.Location) && !internalTop.IsVisible(e.Location))
{
outpen3 = new Pen(Color.Red);
this.Refresh();
this.position = 11;
return;
}
if (outRightTop.IsVisible(e.Location) && !internalRightTop.IsVisible(e.Location))
{
outpen4 = new Pen(Color.Red);
this.Refresh();
this.position = 12;
return;
}
if (outRight.IsVisible(e.Location) && !internalRight.IsVisible(e.Location))
{
outpen5 = new Pen(Color.Red);
this.Refresh();
this.position = 13;
return;
}
if (outRightBottom.IsVisible(e.Location) && !internalRightBottom.IsVisible(e.Location))
{
outpen6 = new Pen(Color.Red);
this.Refresh();
this.position = 14;
return;
}
if (outBottom.IsVisible(e.Location) && !internalBottom.IsVisible(e.Location))
{
outpen7 = new Pen(Color.Red);
this.Refresh();
this.position = 15;
return;
}
if (outLeftBottom.IsVisible(e.Location) && !internalLeftBottom.IsVisible(e.Location))
{
outpen8 = new Pen(Color.Red);
this.Refresh();
this.position = 16;
return;
}
}
private void OutMouseUpOrDown(MouseEventArgs e)
{
if (outLeft.IsVisible(e.Location) && !internalLeft.IsVisible(e.Location))
{
outpen1 = new Pen(Color.Red);
this.Refresh();
this.position = 9;
return;
}
if (outLeftTop.IsVisible(e.Location) && !internalLeftTop.IsVisible(e.Location))
{
outpen2 = new Pen(Color.Red);
this.Refresh();
this.position = 10;
return;
}
if (outTop.IsVisible(e.Location) && !internalTop.IsVisible(e.Location))
{
outpen3 = new Pen(Color.Red);
this.Refresh();
this.position = 11;
return;
}
if (outRightTop.IsVisible(e.Location) && !internalRightTop.IsVisible(e.Location))
{
outpen4 = new Pen(Color.Red);
this.Refresh();
this.position = 12;
return;
}
if (outRight.IsVisible(e.Location) && !internalRight.IsVisible(e.Location))
{
outpen5 = new Pen(Color.Red);
this.Refresh();
this.position = 13;
return;
}
if (outRightBottom.IsVisible(e.Location) && !internalRightBottom.IsVisible(e.Location))
{
outpen6 = new Pen(Color.Red);
this.Refresh();
this.position = 14;
return;
}
if (outBottom.IsVisible(e.Location) && !internalBottom.IsVisible(e.Location))
{
outpen7 = new Pen(Color.Red);
this.Refresh();
this.position = 15;
return;
}
if (outLeftBottom.IsVisible(e.Location) && !internalLeftBottom.IsVisible(e.Location))
{
outpen8 = new Pen(Color.Red);
this.Refresh();
this.position = 16;
return;
}
}
}
}