-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRoundControl.cs
119 lines (104 loc) · 3.39 KB
/
RoundControl.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using System;
using System.Drawing;
using System.Windows.Forms;
using MathUtils = SF.Space.MathUtils;
using MouseEventType = SF.Space.MouseEventType;
namespace SF.Controls
{
public class RoundControl : UserControl
{
public bool ReadOnly { get; set; }
public StringFormat CenteredLayout;
public StringFormat VerticalLayout;
public RoundControl()
{
DoubleBuffered = true;
Size = new Size(200, 200);
CenteredLayout = new StringFormat
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center,
Trimming = StringTrimming.None,
};
VerticalLayout = new StringFormat
{
FormatFlags = StringFormatFlags.DirectionVertical,
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center,
Trimming = StringTrimming.None,
};
}
protected int m_size
{
get { return Math.Min(ClientRectangle.Width, ClientRectangle.Height) - 1; }
}
protected Point m_center
{
get
{
return new Point
{
X = ClientRectangle.Top + ClientRectangle.Width / 2,
Y = ClientRectangle.Y + ClientRectangle.Height / 2
};
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if (ReadOnly || e.Button != System.Windows.Forms.MouseButtons.Left)
return;
MouseHit(e.Location, MouseEventType.MouseDown);
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
if (ReadOnly || e.Button != System.Windows.Forms.MouseButtons.Left)
return;
MouseHit(e.Location, MouseEventType.MouseUp);
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (ReadOnly || e.Button != System.Windows.Forms.MouseButtons.Left)
return;
MouseHit(e.Location, MouseEventType.MouseMove);
}
protected override void OnPaintBackground(PaintEventArgs e)
{
base.OnPaintBackground(e);
DrawBackgroound(e.Graphics);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
DrawContents(e.Graphics);
}
protected static Point GetXY(Point center, int radius, double angle)
{
return new Point
{
X = (int)(center.X + Math.Sin(angle) * radius),
Y = (int)(center.Y - Math.Cos(angle) * radius)
};
}
private void MouseHit(Point point, MouseEventType t)
{
int x = point.X - m_center.X;
int y = point.Y - m_center.Y;
if (x == 0 && y == 0)
return;
var alpha = Math.Atan2(x, -y);
MouseHit(point, alpha, t);
}
protected virtual void MouseHit(Point point, double alpha, MouseEventType t)
{
}
protected virtual void DrawContents(Graphics g)
{
}
protected virtual void DrawBackgroound(Graphics g)
{
}
}
}