-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDrawRectangle_Foo_2.cs
84 lines (84 loc) · 3.04 KB
/
DrawRectangle_Foo_2.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
/// <summary>
/// 在一个控件上绘制虚线矩形 并返回矩形区域
/// zgke@sina.com.
/// qq:116149
/// </summary>
public class SetControlRectangle
{
/// <summary>
/// 需要绘制的图形
/// </summary>
/// <param name="p_Control">控件</param>
public SetControlRectangle(Control p_Control)
{
m_Control = p_Control;
m_Control.MouseUp+=new MouseEventHandler(MyControl_MouseUp);
m_Control.MouseMove+=new MouseEventHandler(MyControl_MouseMove);
m_Control.MouseDown+=new MouseEventHandler(MyControl_MouseDown);
}
private Control m_Control;
/// <summary>
/// 鼠标状态
/// </summary>
private bool m_MouseIsDown = false;
/// <summary>
/// 绘制区域
/// </summary>
private Rectangle m_MouseRect = Rectangle.Empty;
/// <summary>
/// 刷新绘制
/// </summary>
/// <param name="p"></param>
private void ResizeToRectangle(Point p_Point)
{
DrawRectangle();
m_MouseRect.Width = p_Point.X - m_MouseRect.Left;
m_MouseRect.Height = p_Point.Y - m_MouseRect.Top;
DrawRectangle();
}
/// <summary>
/// 绘制区域
/// </summary>
private void DrawRectangle()
{
Rectangle _Rect = m_Control.RectangleToScreen(m_MouseRect);
ControlPaint.DrawReversibleFrame(_Rect, Color.White, FrameStyle.Dashed);
}
/// <summary>
/// 开始绘制 并且设置鼠标区域
/// </summary>
/// <param name="StartPoint">开始位置</param>
private void DrawStart(Point p_Point)
{
m_Control.Capture = true;
Cursor.Clip = m_Control.RectangleToScreen(new Rectangle(0, 0, m_Control.Width, m_Control.Height));
m_MouseRect = new Rectangle(p_Point.X, p_Point.Y, 0, 0);
}
private void MyControl_MouseDown(object sender, MouseEventArgs e)
{
m_MouseIsDown = true;
DrawStart(new Point(e.X , e.Y));
}
private void MyControl_MouseMove(object sender, MouseEventArgs e)
{
if (m_MouseIsDown) ResizeToRectangle(new Point(e.X , e.Y ));
}
private void MyControl_MouseUp(object sender, MouseEventArgs e)
{
m_Control.Capture = false;
Cursor.Clip = Rectangle.Empty;
m_MouseIsDown = false;
DrawRectangle();
if (m_MouseRect.X == 0 || m_MouseRect.Y == 0 || m_MouseRect.Width == 0 || m_MouseRect.Height == 0)
{
//如果区域没0 就不执行委托
}
else
{
if (SetRectangel != null) SetRectangel(m_Control, m_MouseRect);
}
m_MouseRect = Rectangle.Empty;
}
public delegate void SelectRectangel(object sneder,Rectangle e);
public event SelectRectangel SetRectangel;
}