-
Notifications
You must be signed in to change notification settings - Fork 1
/
Utils.cs
240 lines (186 loc) · 5.57 KB
/
Utils.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows;
using System.Diagnostics;
using System.Windows.Forms;
using OpenCvSharp;
using System.Drawing;
using System.Windows.Input;
using System.Windows.Media;
namespace SamplesCS
{
public static class Utils
{
public const UInt32 MouseLeftDown = 0x0002;
public const UInt32 MouseLeftUp = 0x0004;
public const UInt32 MouseRightDown = 0x0008;
public const UInt32 MouseRightUp = 0x0010;
public const UInt32 MouseMove = 0x0001;
public static Random random = new Random();
[DllImport("user32.dll")]
private static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData, uint dwExtraInf);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr FindWindow(string strClassName, string strWindowName);
[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
public struct Rect
{
public int Left { get; set; }
public int Top { get; set; }
public int Right { get; set; }
public int Bottom { get; set; }
public int Width { get { return Right - Left; } }
public int Height { get { return Bottom - Top; } }
public int X { get { return Left; } }
public int Y { get { return Top; } }
}
[DllImport("user32.dll")]
public static extern bool GetWindowRect(IntPtr hwnd, ref Rect rectangle);
public static void MoveMouse(int x, int y)
{
System.Windows.Forms.Cursor.Position = new System.Drawing.Point(x, y);
}
public static void PressMouse(IntPtr hWnd, UInt32 how)
{
SetForegroundWindow(hWnd);
mouse_event(how, 0, 0, 0, 0);
}
public static void SendKeyboard(string text)
{
System.Windows.Forms.SendKeys.Send(text);
}
public static void Example()
{
System.Windows.Forms.Cursor c =
new System.Windows.Forms.Cursor(System.Windows.Forms.Cursor.Current.Handle);
System.Windows.Forms.Cursor.Position = new System.Drawing.Point(3200, 400);
int moveY = -100;
mouse_event(MouseLeftDown, 0, 0, 0, 0);//make left button down
Thread.Sleep(1000);
mouse_event(MouseMove, 0, (uint)moveY, 0, 0);
}
public static Process GetProcessByName(string name)
{
Process[] processes = Process.GetProcesses();
foreach (var p in processes)
{
//Console.WriteLine(p.ProcessName);
if (p.ProcessName == name)
{
//Console.WriteLine("["+p+"]");
return p;
}
}
return null;
}
public static bool KillProcess(string name)
{
var process = GetProcessByName(name);
if (process != null)
{
process.Kill();
return true;
}
return false;
}
public static bool ExecuteProcess(string name)
{
const string NoxPath = "C:/Program Files (x86)/Nox/bin/Nox.exe";
var process = Process.Start(NoxPath);
return process != null;
}
public static void ProcessExample()
{
Process[] processes = Process.GetProcesses();
foreach (var p in processes)
{
IntPtr ptr = p.MainWindowHandle;
Utils.Rect rect = new Utils.Rect();
Utils.GetWindowRect(ptr, ref rect);
Console.WriteLine(
p.ProcessName + " " +
(int)rect.Left + " " + (int)rect.Right + " " + (int)rect.Top + " " + (int)rect.Bottom);
}
}
static public string GetFileNameInFullEntry(string fileEntry, bool isDirectory = false)
{
string[] stringSeparators = new string[] { "/", ".", "\\" };
string[] result;
result = fileEntry.Split(stringSeparators, StringSplitOptions.None);
if (isDirectory)
{
if (result.Length < 1)
return "";
return result[result.Length - 1];
}
else
{
if (result.Length < 2)
return "";
return result[result.Length - 2];
}
}
static public void CaptureScreen(int screenX, int screenY, int width, int height, out Bitmap printscreen)
{
if (width <= 0 || height <= 0 || screenX < 0 || screenY < 0)
{
printscreen = null;
return;
}
//Create the Bitmap
printscreen = new Bitmap(
width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
//Create the Graphic Variable with screen Dimensions
Graphics graphics = Graphics.FromImage(printscreen as Image);
//Copy Image from the screen
graphics.CopyFromScreen(
screenX, screenY, 0, 0, printscreen.Size, CopyPixelOperation.SourceCopy);
}
static public void RenderKeyPoints(string wndName, Mat mat, KeyPoint[] keypoints)
{
Mat view = mat.Clone();
foreach (KeyPoint kp in keypoints)
{
view.Circle(kp.Pt, 3, Scalar.Red, -1, LineTypes.AntiAlias, 0);
}
Cv2.ImShow(wndName, view);
}
public static Bitmap MatToBitmap(Mat image)
{
return OpenCvSharp.Extensions.BitmapConverter.ToBitmap(image);
}
public static Mat BitmapToMat(Bitmap bitmap)
{
return OpenCvSharp.Extensions.BitmapConverter.ToMat(bitmap);
}
public static System.Drawing.Point RectToPos(int x, int y, int width, int height)
{
System.Drawing.Point pos = new System.Drawing.Point(x, y);
pos.X += (int)((double)width * random.NextDouble());
pos.Y += (int)((double)height * random.NextDouble());
return pos;
}
public static Point2f[] GetPointsFromKeyPoints(KeyPoint[] keyPoints)
{
Point2f[] points = new Point2f[keyPoints.Length];
for (int i = 0; i< points.Length; ++i)
{
points[i] = keyPoints[i].Pt;
}
return points;
}
public static bool CheckIsPNG(string file)
{
return file.Contains(".png") || file.Contains(".PNG");
}
public static bool CheckIsTXT(string file)
{
return file.Contains(".txt") || file.Contains(".TXT");
}
}
}