-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWin32.cs
More file actions
226 lines (182 loc) · 7.77 KB
/
Win32.cs
File metadata and controls
226 lines (182 loc) · 7.77 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
using System;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
namespace LolLogin
{
/// <summary>
/// Summary description for Win32.
/// </summary>
public class Win32
{
// The WM_COMMAND message is sent when the user selects a command item from
// a menu, when a control sends a notification message to its parent window,
// or when an accelerator keystroke is translated.
public const int WM_KEYDOWN = 0x100;
public const int WM_KEYUP = 0x101;
public const int WM_COMMAND = 0x111;
public const int WM_LBUTTONDOWN = 0x201;
public const int WM_LBUTTONUP = 0x202;
public const int WM_LBUTTONDBLCLK = 0x203;
public const int WM_RBUTTONDOWN = 0x204;
public const int WM_RBUTTONUP = 0x205;
public const int WM_RBUTTONDBLCLK = 0x206;
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int X;
public int Y;
public POINT(int x, int y)
{
this.X = x;
this.Y = y;
}
public static implicit operator System.Drawing.Point(POINT p)
{
return new System.Drawing.Point(p.X, p.Y);
}
public static implicit operator POINT(System.Drawing.Point p)
{
return new POINT(p.X, p.Y);
}
}
[FlagsAttribute]
public enum EXECUTION_STATE : uint
{
ES_AWAYMODE_REQUIRED = 0x00000040,
ES_CONTINUOUS = 0x80000000,
ES_DISPLAY_REQUIRED = 0x00000002,
ES_SYSTEM_REQUIRED = 0x00000001
// Legacy flag, should not be used.
// ES_USER_PRESENT = 0x00000004
}
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool SetProcessDPIAware();
[DllImport("user32.dll")]
public static extern bool ClientToScreen(IntPtr hwnd, ref POINT lpPoint);
[DllImport("user32.dll")]
public static extern bool ScreenToClient(IntPtr hWnd, ref POINT lpPoint);
[DllImport("user32.dll")]
public static extern bool SetCursorPos(int X, int Y);
// The FindWindow function retrieves a handle to the top-level window whose
// class name and window name match the specified strings.
// This function does not search child windows.
// This function does not perform a case-sensitive search.
[DllImport("User32.dll")]
public static extern int FindWindow(string strClassName, string strWindowName);
// The FindWindowEx function retrieves a handle to a window whose class name
// and window name match the specified strings.
// The function searches child windows, beginning with the one following the
// specified child window.
// This function does not perform a case-sensitive search.
[DllImport("User32.dll")]
public static extern int FindWindowEx(
int hwndParent,
int hwndChildAfter,
string strClassName,
string strWindowName);
// The SendMessage function sends the specified message to a window or windows.
// It calls the window procedure for the specified window and does not return
// until the window procedure has processed the message.
[DllImport("User32.dll")]
public static extern Int32 SendMessage(
IntPtr hWnd, // handle to destination window
uint Msg, // message
IntPtr wParam, // first message parameter
IntPtr lParam); // second message parameter
[DllImport("user32.dll")]
static extern void mouse_event(uint dwFlags, int dx, int dy, uint dwData, int dwExtraInfo);
// https://docs.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-mouseinput
public const int MOUSEEVENTF_MOVE = 0x0001;
public const int MOUSEEVENTF_LEFTDOWN = 0x0002;
public const int MOUSEEVENTF_LEFTUP = 0x0004;
public const int MOUSEEVENTF_ABSOLUTE = 0x8000;
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[DllImport("user32.dll", SetLastError = true)]
public static extern bool GetWindowRect(IntPtr hWnd, ref RECT Rect);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int Width, int Height, bool Repaint);
//Import the SetForeground API to activate it
[DllImport("User32.dll", EntryPoint = "SetForegroundWindow")]
public static extern IntPtr SetForegroundWindowNative(IntPtr hWnd);
[DllImport("shell32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern uint ShellExecute(IntPtr hwnd, string strOperation, string strFile, string strParameters, string strDirectory, Int32 nShowCmd);
public static void SendLeftClick(Point point)
{
// Set Cursor
SetCursorPos(point.X, point.Y);
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
Thread.Sleep(300);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}
public static void SendLeftDragAndHold(int startX, int startY, int amountToMoveX, int amountToMoveY)
{
SendLeftDragAndHold(startX, startY, amountToMoveX, amountToMoveY, 0, null);
}
private static void SendLeftDragAndHold(int startX, int startY, int amountToMoveX, int amountToMoveY, int releaseDelayMS, CancellationTokenSource cancellationToken)
{
Stopwatch sw = new Stopwatch();
sw.Start();
Stopwatch delaySW = new Stopwatch();
delaySW.Start();
// Set Cursor
SetCursorPos(startX, startY);
// Click once
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
Thread.Sleep(100);
int currentX = 0;
int currentY = 0;
int xDirection = 1;
if (amountToMoveX < 0)
xDirection = -1;
amountToMoveX = Math.Abs(amountToMoveX);
int yDirection = 1;
if (amountToMoveY < 0)
yDirection = -1;
amountToMoveY = Math.Abs(amountToMoveY);
while (currentX < amountToMoveX - 5 || currentY < amountToMoveY - 5)
{
int offsetX = 0;
int offsetY = 0;
if (currentX < amountToMoveX - 5)
{
offsetX = 5;
currentX += 5;
}
else if (currentX != amountToMoveX)
{
offsetX = amountToMoveX - currentX;
currentX = amountToMoveX;
}
if (currentY < amountToMoveY - 5)
{
offsetY = 5;
currentY += 5;
}
else if (currentY != amountToMoveY)
{
offsetY = amountToMoveY - currentY;
currentY = amountToMoveY;
}
mouse_event(MOUSEEVENTF_MOVE, offsetX * xDirection, offsetY * yDirection, 0, 0);
Thread.Sleep(10);
}
}
public static void ReleaseLeftDragHold()
{
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}
}
}