-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWin32.cs
195 lines (159 loc) · 5.92 KB
/
Win32.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
using System;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
namespace NarcoFeeder.Helpers
{
class Win32
{
public struct Rect
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
[StructLayout(LayoutKind.Sequential)]
public struct Point
{
public Int32 x;
public Int32 y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
}
[StructLayout(LayoutKind.Sequential)]
public struct CursorInfo
{
public Int32 cbSize;
public Int32 flags;
public IntPtr hCursor;
public Point ptScreenPos;
}
public enum keyState
{
KEYDOWN = 0,
EXTENDEDKEY = 1,
KEYUP = 2
};
[DllImport("user32.dll", EntryPoint = "FindWindow")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern bool GetWindowRect(IntPtr hwnd, ref Rect rectangle);
[DllImport("user32.dll")]
private static extern bool SetCursorPos(int X, int Y);
[DllImport("user32.dll")]
private static extern bool GetCursorInfo(out CursorInfo pci);
[DllImport("user32.dll")]
private static extern bool DrawIcon(IntPtr hDC, int X, int Y, IntPtr hIcon);
[DllImport("user32.dll")]
private static extern bool keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern bool SendNotifyMessage(IntPtr hWnd, uint Msg, UIntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);
[DllImport("user32.dll")]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
private const uint WM_LBUTTONDOWN = 513;
private const uint WM_LBUTTONUP = 514;
private const uint WM_LBUTTONDBLCLK = 515;
private const uint WM_RBUTTONDOWN = 516;
private const uint WM_RBUTTONUP = 517;
private const string EXENAME = "ShooterGame"; //ShooterGame
public static Rect GetARKRectangle()
{
IntPtr hWnd = ActivateApp(EXENAME);
if (hWnd!=IntPtr.Zero)
{
ARKWindowPtr = hWnd;
Rect Win32ApiRect = new Rect();
GetWindowRect(hWnd, ref Win32ApiRect);
ARKRect = Win32ApiRect;
//Console.Out.WriteLine("ark window found." + Win32ApiRect.Left + " " + Win32ApiRect.Right + " " + Win32ApiRect.Top + " " + Win32ApiRect.Bottom + " ");
//break;
}
return ARKRect;
}
static public IntPtr ActivateApp(string processName)
{
Process[] p = Process.GetProcessesByName(processName);
// Activate the first application we find with this name
if (p.Count() > 0) {
//Console.Out.WriteLine("app found: " + processName+" T:"+p[0].MainWindowTitle);
SetForegroundWindow(p[0].MainWindowHandle);
return p[0].MainWindowHandle;
}
else
{
Console.Out.WriteLine("app not found: "+processName);
return IntPtr.Zero;
}
}
public static void MoveMouse(Point p)
{
SetCursorPos(p.x, p.y);
}
public static CursorInfo GetCurrentCursor()
{
CursorInfo myInfo = new CursorInfo();
myInfo.cbSize = Marshal.SizeOf(myInfo);
GetCursorInfo(out myInfo);
return myInfo;
}
public static Point GetCurrentCursorPoint()
{
return GetCurrentCursor().ptScreenPos;
}
public static void SendKey(string sKeys)
{
if (sKeys != " ")
{
sKeys = "{" + sKeys + "}"; // {X} : Avoid UTF-8 errors (é, è, ...)
}
SendKeys.Send(sKeys);
}
public static void SendMouseClick(Point p, int delay)
{
long dWord = MakeDWord(p.x, p.y);
if (ARKWindowPtr == IntPtr.Zero) return;
SendNotifyMessage(ARKWindowPtr, WM_LBUTTONDOWN, (UIntPtr)1, (IntPtr)dWord);
Thread.Sleep(delay);
SendNotifyMessage(ARKWindowPtr, WM_LBUTTONUP, (UIntPtr)1, (IntPtr)dWord);
}
public static void SendMouseRClick(Point p, int delay)
{
long dWord = MakeDWord(p.x, p.y);
if (ARKWindowPtr == IntPtr.Zero) return;
SendNotifyMessage(ARKWindowPtr, WM_RBUTTONDOWN, (UIntPtr)1, (IntPtr)dWord);
Thread.Sleep(delay);
SendNotifyMessage(ARKWindowPtr, WM_RBUTTONUP, (UIntPtr)1, (IntPtr)dWord);
}
public static void SendMouseDoubleClick(Point p)
{
SendMouseClick(p, 30);
Thread.Sleep(30);
SendMouseClick(p, 30);
}
public static bool SendKeyboardAction(Keys key, keyState state)
{
return SendKeyboardAction((byte)key.GetHashCode(), state);
}
public static bool SendKeyboardAction(byte key, keyState state)
{
return keybd_event(key, 0, (uint)state, (UIntPtr)0);
}
private static long MakeDWord(int LoWord, int HiWord)
{
return (HiWord << 16) | (LoWord & 0xFFFF);
}
static private IntPtr ARKWindowPtr;
static private Rect ARKRect;
}
}