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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
| using System;
namespace XChess
{
class MouseController
{
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern long SetCursorPos(int x, int y);
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern bool GetCursorPos(ref POINT lpPoint);
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern void mouse_event(UInt32 dwFlags, UInt32 dx, UInt32 dy, UInt32 dwData, IntPtr dwExtraInfo);
private const UInt32 MouseEventLeftDown = 0x0002;
private const UInt32 MouseEventLeftUp = 0x0004;
private const UInt32 MouseEventRightDown = 0x0008;
private const UInt32 MouseEventRightUp = 0x0010;
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern IntPtr GetDC(IntPtr hwnd);
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct POINT
{ public int x; public int y;}
public MouseController()
{
}
/// <summary>
/// Sends a mouse click
/// </summary>
public void SendMouseClick()
{
mouse_event(MouseEventLeftDown, 0, 0, 0, new System.IntPtr());
mouse_event(MouseEventLeftUp, 0, 0, 0, new System.IntPtr());
}
/// <summary>
/// Sends a mouse click to X,Y, but the cursor will remain on the current position
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
public void SendMouseClick(int x, int y)
{
POINT defPnt = new POINT();
GetCursorPos(ref defPnt);
int curX = defPnt.x;
int curY = defPnt.y;
SetCursorPos(x, y);
mouse_event(MouseEventLeftDown, 0, 0, 0, new System.IntPtr());
mouse_event(MouseEventLeftUp, 0, 0, 0, new System.IntPtr());
SetCursorPos(curX, curY);
}
/// <summary>
/// Presses the left mouse button, and holds it
/// </summary>
public void LeftMouseButtonDown()
{
mouse_event(MouseEventLeftDown, 0, 0, 0, new System.IntPtr());
}
/// <summary>
/// Releases the left mouse button
/// </summary>
public void LeftMouseButtonUp()
{
mouse_event(MouseEventLeftUp, 0, 0, 0, new System.IntPtr());
}
/// <summary>
/// Presses the right mouse button and holds it
/// </summary>
public void RightMouseButtonDown()
{
mouse_event(MouseEventRightDown, 0, 0, 0, new System.IntPtr());
}
/// <summary>
/// Releases the right mouse button
/// </summary>
public void RightMouseButtonUp()
{
mouse_event(MouseEventRightUp, 0, 0, 0, new System.IntPtr());
}
/// <summary>
/// Sends a click of the right mouse button
/// </summary>
public void SendRightMouseClick()
{
mouse_event(MouseEventRightDown, 0, 0, 0, new System.IntPtr());
}
/// <summary>
/// Sends a click of the right mouse button to the X,Y coordinate
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
public static void SendRightMouseClick(int x, int y)
{
POINT defPnt = new POINT();
GetCursorPos(ref defPnt);
int curX = defPnt.x;
int curY = defPnt.y;
SetCursorPos(x, y);
mouse_event(MouseEventRightDown, 0, 0, 0, new System.IntPtr());
mouse_event(MouseEventRightUp, 0, 0, 0, new System.IntPtr());
SetCursorPos(curX, curY);
}
/// <summary>
/// Gets a Point object of the X,Y location of the mouse pointer
/// </summary>
/// <returns></returns>
public System.Drawing.Point GetPointerLocation()
{
POINT defPnt = new POINT();
GetCursorPos(ref defPnt);
System.Drawing.Point p = new System.Drawing.Point();
p.X = defPnt.x;
p.Y = defPnt.y;
return p;
}
/// <summary>
/// Sets the X,Y location of the mouse pointer
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
public void SetPointerLocation(int x, int y)
{
SetCursorPos(x, y);
}
/// <summary>
/// Gets or sets the location of the mouse pointer. Returns or requires a Point object.
/// </summary>
public System.Drawing.Point PointerLocation
{
get
{
return GetPointerLocation();
}
set
{
SetCursorPos(value.X, value.Y);
}
}
/// <summary>
/// Gets or sets the X location of the mouse cursor
/// </summary>
public int X
{
get
{
POINT defPnt = new POINT();
GetCursorPos(ref defPnt);
return defPnt.x;
}
set
{
POINT defPnt = new POINT();
GetCursorPos(ref defPnt);
SetCursorPos(value, defPnt.y);
}
}
/// <summary>
/// Gets or sets the Y location of the mouse cursor
/// </summary>
public int Y
{
get
{
POINT defPnt = new POINT();
GetCursorPos(ref defPnt);
return defPnt.y;
}
set
{
POINT defPnt = new POINT();
GetCursorPos(ref defPnt);
SetCursorPos(defPnt.x, value);
}
}
}
} |