-
Notifications
You must be signed in to change notification settings - Fork 2
/
WinApi.cs
174 lines (138 loc) · 5.42 KB
/
WinApi.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
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
using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows;
namespace AlphaClicker
{
public class WinApi
{
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int X;
public int Y;
public static implicit operator Point(POINT point)
{
return new Point(point.X, point.Y);
}
}
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
[DllImport("user32.dll")]
public static extern int GetAsyncKeyState(int character);
[DllImport("user32.dll")]
public static extern int SetCursorPos(int x, int y);
// Get custom coords
[DllImport("user32.dll")]
public static extern bool GetCursorPos(out POINT lpPoint);
[DllImport("user32.dll")]
static extern IntPtr WindowFromPoint(POINT Point);
[DllImport("user32.dll")]
static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
// 导入 PostMessage 函数
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[StructLayout(LayoutKind.Sequential)]
struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
// 定义常量
const uint WM_LBUTTONDOWN = 0x0201;
const uint WM_LBUTTONUP = 0x0202;
const uint WM_RBUTTONDOWN = 0x0204;
const uint WM_RBUTTONUP = 0x0205;
const uint WM_MBUTTONDOWN = 0x0207;
const uint WM_MBUTTONUP = 0x0208;
const int MK_LBUTTON = 0x0001;
// 定义辅助函数
static int MAKELONG(int low, int high)
{
return (high << 16) | (low & 0xffff);
}
private static IntPtr hWnd;
/// <summary>
/// 发送窗口的点击位置消息
/// </summary>
private static IntPtr lParam;
private readonly static IntPtr wParam = new IntPtr(MK_LBUTTON);
private static int x, y;
public static Point GetCursorPosition()
{
POINT lpPoint;
GetCursorPos(out lpPoint);
return lpPoint;
}
public static void DoClick(string button, bool useCustomCoords, int X, int Y,bool bg)
{
if (useCustomCoords)
{
if (bg)
{
if (x != X || y != Y)
{
hWnd = WindowFromPoint(new POINT { X = X, Y = Y });
x = X;
y = Y;
RECT rect;
GetWindowRect(hWnd, out rect);
X -= rect.Left;
Y -= rect.Top;
lParam = new IntPtr(MAKELONG(X, Y));
}
switch (button)
{
case "左键":
SendMessage(hWnd, WM_LBUTTONDOWN, IntPtr.Zero, lParam);
SendMessage(hWnd, WM_LBUTTONUP, IntPtr.Zero, lParam);
break;
case "右键":
SendMessage(hWnd, WM_RBUTTONDOWN, IntPtr.Zero, lParam);
SendMessage(hWnd, WM_RBUTTONUP, IntPtr.Zero, lParam);
break;
case "中键":
SendMessage(hWnd, WM_MBUTTONDOWN, IntPtr.Zero, lParam);
SendMessage(hWnd, WM_MBUTTONUP, IntPtr.Zero, lParam);
break;
}
//SendMessage(hWnd, WM_LBUTTONDOWN, IntPtr.Zero, lParam);
//SendMessage(hWnd, WM_LBUTTONUP, IntPtr.Zero, lParam);
}
else
{
SetCursorPos(X, Y);
switch (button)
{
case "左键":
mouse_event((uint)MOUSEEVENTF.LEFTDOWN | (uint)MOUSEEVENTF.LEFTUP, 0, 0, 0, 0);
break;
case "右键":
mouse_event((uint)MOUSEEVENTF.RIGHTDOWN | (uint)MOUSEEVENTF.RIGHTUP, 0, 0, 0, 0);
break;
case "中键":
mouse_event((uint)MOUSEEVENTF.MIDDLEDOWN | (uint)MOUSEEVENTF.MIDDLEUP, 0, 0, 0, 0);
break;
}
}
}
else
{
switch (button)
{
case "左键":
mouse_event((uint)MOUSEEVENTF.LEFTDOWN | (uint)MOUSEEVENTF.LEFTUP, 0, 0, 0, 0);
break;
case "右键":
mouse_event((uint)MOUSEEVENTF.RIGHTDOWN | (uint)MOUSEEVENTF.RIGHTUP, 0, 0, 0, 0);
break;
case "中键":
mouse_event((uint)MOUSEEVENTF.MIDDLEDOWN | (uint)MOUSEEVENTF.MIDDLEUP, 0, 0, 0, 0);
break;
}
}
}
}
}