-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNativeMethods.cs
More file actions
296 lines (234 loc) · 11.4 KB
/
Copy pathNativeMethods.cs
File metadata and controls
296 lines (234 loc) · 11.4 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
//Download by http://www.codefans.net
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace QQAutoSend
{
/// <summary>
/// API声明类
/// </summary>
internal static class NativeMethods
{
#region Callback Delegate Section
public delegate bool EnumDesktopWindowsDelegate(IntPtr hWnd, uint lParam);
public delegate bool EnumChildWindowsDelegate(IntPtr hwnd, IntPtr lParam);
public delegate IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
#endregion
internal struct POINTAPI
{
internal int x;
internal int y;
}
internal struct RECT
{
internal int left;
internal int top;
internal int right;
internal int bottom;
internal bool IsEmpty()
{
return left == 0 && top == 0 && right == 0 && bottom == 0;
}
}
#region Windows API Import Section
[DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
internal static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "GetWindowTextLength", SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("user32.dll", EntryPoint = "GetWindowText", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("kernel32.dll ", CharSet = CharSet.Auto)]
public extern static IntPtr GetModuleHandle(string lpModuleName);
[DllImport("user32.dll", EntryPoint = "GetClassName", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern int GetClassName(IntPtr hWnd, StringBuilder buf, int nMaxCount);
[DllImport("user32.dll", EntryPoint = "EnumDesktopWindows", ExactSpelling = false,
CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool EnumDesktopWindows(IntPtr hDesktop,
EnumDesktopWindowsDelegate lpEnumCallbackFunction, IntPtr lParam);
[DllImport("user32.dll", EntryPoint = "EnumChildWindows", ExactSpelling = false,
CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool EnumChildWindows(IntPtr parentHandle, EnumChildWindowsDelegate callback, IntPtr lParam);
[DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern IntPtr SendMessage(IntPtr hWnd, uint wMsg, IntPtr wParam, string lParam);
[DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto)]
internal static extern IntPtr SendMessage(IntPtr hWnd, uint wMsg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", EntryPoint = "GetWindow", SetLastError = true)]
internal static extern IntPtr GetWindow(IntPtr hWnd, uint wCmd);
[DllImport("user32.dll", EntryPoint = "IsWindow", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool IsWindow(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out int dwProcessId);
[DllImport("Psapi.dll")]
public static extern int EnumProcessModules(IntPtr hProcess, out IntPtr hMoudle, int cb, out int cbNeed);
[DllImport("psapi.dll")]
public static extern uint GetModuleBaseName(IntPtr hProcess, IntPtr hModule, out StringBuilder lpBaseName, uint nSize);
[DllImport("kernel32.dll")]
public static extern bool CloseHandle(IntPtr handle);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool BringWindowToTop(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, ShowWindowCommands nCmdShow);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern bool SetWindowText(IntPtr hwnd, String lpString);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumThreadWindows(IntPtr dwThreadId, EnumDesktopWindowsDelegate callback, IntPtr lParam);
#region Mouse
[DllImport("User32")]
public extern static void mouse_event(int dwFlags, int dx, int dy, int dwData, IntPtr dwExtraInfo);
[DllImport("user32.dll", EntryPoint = "SwapMouseButton")]
internal extern static int SwapMouseButton(int bSwap);
[DllImport("user32", EntryPoint = "ClipCursor")]
internal extern static int ClipCursor(ref RECT lpRect);
[DllImport("user32.dll", EntryPoint = "GetCursorPos")]
internal extern static int GetCursorPos(ref POINTAPI lpPoint);
[DllImport("user32.dll", EntryPoint = "ShowCursor")]
internal extern static bool ShowCursor(bool bShow);
[DllImport("user32.dll", EntryPoint = "EnableWindow")]
internal extern static int EnableWindow(int hwnd, int fEnable);
[DllImport("user32.dll", EntryPoint = "GetWindowRect")]
internal extern static int GetWindowRect(IntPtr hwnd, ref RECT lpRect);
[DllImport("user32.dll", EntryPoint = "SetCursorPos")]
internal extern static int SetCursorPos(int x, int y);
[DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]
internal extern static int GetSystemMetrics(int nIndex);
[DllImport("user32.dll", EntryPoint = "SetDoubleClickTime")]
internal extern static int SetDoubleClickTime(int wCount);
[DllImport("user32.dll", EntryPoint = "GetDoubleClickTime")]
internal extern static int GetDoubleClickTime();
[DllImport("kernel32.DLL", EntryPoint = "Sleep")]
internal extern static void Sleep(int dwMilliseconds);
#endregion
#endregion
#region Windows Message Declaration Section
internal const uint BM_CLICK = 0xF5;
internal const uint GW_HWNDNEXT = 0x02;
internal const uint EM_REPLACESEL = 0xC2;
internal const int WM_VSCROLL = 0x115;
internal const int SB_LINEDOWN = 0x1;
internal const int WM_LBUTTONDOWN = 0x201;
internal const int MK_LBUTTON = 0x1;
internal const int WM_LBUTTONUP = 0x202;
internal const int WM_LBUTTONDBLCLK = 0x203;
internal const int WM_CHAR = 0x102;
internal const int WM_CLOSE = 0x10;
internal const int GWL_ID = -12;
internal const int GWL_STYLE = -16;
internal const int GWL_EXSTYLE = -20;
internal const int WM_IME_CHAR = 0x286;
internal const int WM_KEYDOWN = 0x0100;
public enum ShowWindowCommands : int
{
/// <summary>
/// Hides the window and activates another window.
/// </summary>
Hide = 0,
/// <summary>
/// Activates and displays a window. If the window is minimized or
/// maximized, the system restores it to its original size and position.
/// An application should specify this flag when displaying the window
/// for the first time.
/// </summary>
Normal = 1,
/// <summary>
/// Activates the window and displays it as a minimized window.
/// </summary>
ShowMinimized = 2,
/// <summary>
/// Maximizes the specified window.
/// </summary>
Maximize = 3, // is this the right value?
/// <summary>
/// Activates the window and displays it as a maximized window.
/// </summary>
ShowMaximized = 3,
/// <summary>
/// Displays a window in its most recent size and position. This value
/// is similar to <see cref="Win32.ShowWindowCommand.Normal"/>, except
/// the window is not actived.
/// </summary>
ShowNoActivate = 4,
/// <summary>
/// Activates the window and displays it in its current size and position.
/// </summary>
Show = 5,
/// <summary>
/// Minimizes the specified window and activates the next top-level
/// window in the Z order.
/// </summary>
Minimize = 6,
/// <summary>
/// Displays the window as a minimized window. This value is similar to
/// <see cref="Win32.ShowWindowCommand.ShowMinimized"/>, except the
/// window is not activated.
/// </summary>
ShowMinNoActive = 7,
/// <summary>
/// Displays the window in its current size and position. This value is
/// similar to <see cref="Win32.ShowWindowCommand.Show"/>, except the
/// window is not activated.
/// </summary>
ShowNA = 8,
/// <summary>
/// Activates and displays the window. If the window is minimized or
/// maximized, the system restores it to its original size and position.
/// An application should specify this flag when restoring a minimized window.
/// </summary>
Restore = 9,
/// <summary>
/// Sets the show state based on the SW_* value specified in the
/// STARTUPINFO structure passed to the CreateProcess function by the
/// program that started the application.
/// </summary>
ShowDefault = 10,
/// <summary>
/// <b>Windows 2000/XP:</b> Minimizes a window, even if the thread
/// that owns the window is not responding. This flag should only be
/// used when minimizing windows from a different thread.
/// </summary>
ForceMinimize = 11
}
#endregion
}
public class Consts
{
/// <summary>
/// 移动鼠标
/// </summary>
public const int MOUSEEVENTF_MOVE = 0x0001;
/// <summary>
/// 模拟鼠标左键按下
/// </summary>
public const int MOUSEEVENTF_LEFTDOWN = 0x0002;
/// <summary>
/// 模拟鼠标左键抬起
/// </summary>
public const int MOUSEEVENTF_LEFTUP = 0x0004;
/// <summary>
/// 模拟鼠标右键按下
/// </summary>
public const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
/// <summary>
/// 模拟鼠标右键抬起
/// </summary>
public const int MOUSEEVENTF_RIGHTUP = 0x0010;
/// <summary>
/// 模拟鼠标中键按下
/// </summary>
public const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
/// <summary>
/// 模拟鼠标中键抬起
/// </summary>
public const int MOUSEEVENTF_MIDDLEUP = 0x0040;
/// <summary>
/// 标示是否采用绝对坐标
/// </summary>
public const int MOUSEEVENTF_ABSOLUTE = 0x8000;
}
}