forked from Half1900/AlphaClicker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
401 lines (348 loc) · 13 KB
/
MainWindow.xaml.cs
File metadata and controls
401 lines (348 loc) · 13 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
using System;
using System.Diagnostics;
using System.Globalization;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Resources;
using ContextMenu = System.Windows.Forms.ContextMenu;
using MenuItem = System.Windows.Forms.MenuItem;
namespace AlphaClicker
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
NotifyIcon notifyIcon = new NotifyIcon();
private bool notifyiconHasInitialzed = false;//此变量用于控制避免重复执行后面的初始化函数
Process[] myprocess = Process.GetProcessesByName("AlphaClicker.exe");
public MainWindow()
{
bool isAppRunning = false;
Mutex mutex = new Mutex(true,Process.GetCurrentProcess().ProcessName,out isAppRunning);
if (!isAppRunning)
{
Environment.Exit(1);
return;
}
InitializeComponent();
this.ShowInTaskbar = false;
}
public void LoadKeybind()
{
startBtn.Content = $"开始 ({Keybinds.keyBinding})";
stopBtn.Content = $"停止 ({Keybinds.keyBinding})";
}
private void Cerror(string errormessage)
{
ToggleClick();
System.Windows.MessageBox.Show(errormessage, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
private int ToInt(string number)
{
// Return int | Replace Empty With 0
return Int32.Parse((number == "") ? "0" : number);
}
public void InitialTray()
{
//隐藏主窗体
Visibility = Visibility.Hidden;
notifyIcon = new System.Windows.Forms.NotifyIcon();
notifyIcon.Text = "鼠标点击器By Forty";
notifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(System.Windows.Forms.Application.ExecutablePath);
var exit = new MenuItem("退出(Eixt)");
exit.Click += Exit_Click;
notifyIcon.ContextMenu = new ContextMenu(new MenuItem[] { exit });
notifyIcon.Visible = true;
notifyIcon.MouseClick += notifyIcon_MouseDoubleClick;
//StateChanged += MainWindow_StateChanged; //窗体状态改变时候触发
notifyiconHasInitialzed = true;
}
void Exit_Click(object sender, EventArgs e)
{
notifyIcon.Visible = false;
this.Close();//关闭系统
}
void notifyIcon_MouseDoubleClick(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (this.WindowState == WindowState.Normal)
{
Visibility = Visibility.Hidden;
this.Hide();
this.WindowState = WindowState.Minimized;
}
else if (this.WindowState == WindowState.Minimized)
{
Visibility = Visibility.Visible;
this.Show();
this.WindowState = WindowState.Normal;
}
}
}
private void FadeButtonColor(System.Windows.Controls.Button btn, string hex)
{
ColorAnimation animation =
new ColorAnimation(
(Color)ColorConverter.ConvertFromString(hex),
new Duration(TimeSpan.FromSeconds(0.2))
);
btn.Background.BeginAnimation(SolidColorBrush.ColorProperty, animation);
}
private void ToggleClick()
{
string startEnabled = "#1494e3";
string startDisabled = "#084466";
string stopEnabled = "#FF605C";
string stopDisabled = "#c43c35";
if (startBtn.IsEnabled)
{
FadeButtonColor(startBtn, startDisabled);
FadeButtonColor(stopBtn, stopEnabled);
startBtn.IsEnabled = false;
stopBtn.IsEnabled = true;
Thread clickhandler = new Thread(ClickHandler);
clickhandler.Start();
}
else
{
FadeButtonColor(startBtn, startEnabled);
FadeButtonColor(stopBtn, stopDisabled);
startBtn.IsEnabled = true;
stopBtn.IsEnabled = false;
}
}
public bool keyEnabled = true;
void KeyHandler()
{
while (true)
{
if (keyEnabled)
{
if (Keybinds.key1 == -1)
{
if (WinApi.GetAsyncKeyState(Keybinds.key2) > 0)
{
Dispatcher.Invoke((Action)(() =>
{
ToggleClick();
}));
}
}
else
{
if ((WinApi.GetAsyncKeyState(Keybinds.key1) & 0x8000) == 0x8000 &&
WinApi.GetAsyncKeyState(Keybinds.key2) > 0)
{
Dispatcher.Invoke((Action)(() =>
{
ToggleClick();
}));
}
}
}
Thread.Sleep(200);
}
}
void ClickHandler()
{
int sleep = 0;
bool useRandomSleep = false;
int randnum1 = 0;
int randnum2 = 0;
string mouseBtn = "";
string clickType = "";
bool repeatTimesChecked = false;
int repeatTimes = 0;
bool customCoordsChecked = false;
bool bgModel = false;
int customCoordsX = 0, customCoordsY = 0;
Dispatcher.Invoke((Action)(() =>
{
/* Grab Click Interval */
try
{
useRandomSleep = (bool)randomIntervalMode.IsChecked;
if (useRandomSleep)
{
randnum1 = (int)(float.Parse(randomSecs1Box.Text,
CultureInfo.InvariantCulture.NumberFormat) * 1000);
randnum2 = (int)(float.Parse(randomSecs2Box.Text,
CultureInfo.InvariantCulture.NumberFormat) * 1000);
randnum1 = (randnum1 == 0) ? 1 : randnum1;
randnum2 = (randnum2 == 0) ? 1 : randnum2;
}
else
{
sleep = ToInt(millisecsBox.Text)
+ ToInt(secondsBox.Text) * 1000
+ ToInt(minsBox.Text) * 60000
+ ToInt(hoursBox.Text) * 3600000;
sleep = (sleep == 0) ? 1 : sleep;
}
}
catch (FormatException ex)
{
Cerror(ex.ToString());
return;
}
/* Grab Mousebutton And Clicktype */
mouseBtn = mouseBtnCBOX.Text;
clickType = clickTypeCBOX.Text;
/* Grab Repeat Stuff */
repeatTimesChecked = (bool)repeatTimesRBtn.IsChecked;
if (repeatTimesChecked)
{
try
{
repeatTimes = Int32.Parse(repeatTimesBox.Text);
}
catch (FormatException)
{
Cerror("Invalid Repeat Times Number");
return;
}
}
/* Grab Coords Stuff */
customCoordsChecked = (bool)coordsCBtn.IsChecked;
bgModel = (bool)BGModel.IsChecked;
if (customCoordsChecked)
{
try
{
customCoordsX = Int32.Parse(xBox.Text);
customCoordsY = Int32.Parse(yBox.Text);
}
catch (FormatException)
{
Cerror("Invalid Repeat Times Number");
return;
}
}
}));
int repeatCount = 0;
Random rnd = new Random();
while (true)
{
bool doClick = false;
Dispatcher.Invoke((Action)(() =>
{
doClick = stopBtn.IsEnabled;
}));
if (doClick)
{
if (repeatTimesChecked)
{
if (repeatCount >= repeatTimes)
{
Dispatcher.Invoke((Action)(() =>
{
ToggleClick();
}));
break;
}
repeatCount += 1;
}
if (clickType == "单击")
{
WinApi.DoClick(mouseBtn, customCoordsChecked, customCoordsX, customCoordsY,bgModel);
}
else
{
WinApi.DoClick(mouseBtn, customCoordsChecked, customCoordsX, customCoordsY, bgModel);
Thread.Sleep(300);
WinApi.DoClick(mouseBtn, customCoordsChecked, customCoordsX, customCoordsY, bgModel);
}
Dispatcher.Invoke((Action)(() =>
{
// Random sleep
sleep = (!useRandomSleep) ? sleep : rnd.Next((randnum1 < randnum2) ? randnum1 : randnum2
, (randnum1 > randnum2) ? randnum1 : randnum2);
}));
Thread.Sleep(sleep);
}
else
{
break;
}
}
}
//bool bgModel1 = true;
private void ChangeState(object sender, RoutedEventArgs e)
{
//bgModel1 = false;
BGModel.IsChecked = false;
}
private void mainWindow_Loaded(object sender, RoutedEventArgs e)
{
if (AlphaRegistry.GetTheme() == "Dark")
ThemesController.SetTheme(ThemesController.ThemeTypes.Dark);
this.Topmost = AlphaRegistry.GetTopmost();
Thread keyhandler = new Thread(KeyHandler);
keyhandler.Start();
// Get Keybind Values From SOFTWARE\AlphaClicker
AlphaRegistry.GetKeybindValues();
LoadKeybind();
}
private void mainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
// So all threads close and not become background process
Environment.Exit(0);
}
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DragMove();
}
/* Top Bar */
private void closeButton_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Close();
}
private void minimizeButton_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
this.WindowState = WindowState.Minimized;
Visibility = Visibility.Visible;
this.ShowInTaskbar = false;
if (!notifyiconHasInitialzed)
{
InitialTray();
}
}
private void getCoordsBtn_Click(object sender, RoutedEventArgs e)
{
this.WindowState = WindowState.Minimized;
GetCursorPos win = new GetCursorPos();
win.Owner = this;
win.Show();
}
private void startBtn_Click(object sender, RoutedEventArgs e)
{
ToggleClick();
}
private void stopBtn_Click(object sender, RoutedEventArgs e)
{
ToggleClick();
}
private void changeHotkeyBtn_Click(object sender, RoutedEventArgs e)
{
keyEnabled = false;
ChangeHotkey win = new ChangeHotkey();
win.WindowStartupLocation = WindowStartupLocation.CenterOwner;
win.Owner = this;
win.ShowDialog();
}
private void windowSettingsBtn_Click(object sender, RoutedEventArgs e)
{
WindowSettings win = new WindowSettings();
win.WindowStartupLocation = WindowStartupLocation.CenterOwner;
win.Owner = this;
win.ShowDialog();
}
}
}