-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMainWindow.xaml.cs
542 lines (479 loc) · 20.3 KB
/
MainWindow.xaml.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
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
using MaterialDesignColors;
using MaterialDesignThemes.Wpf;
using System;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Animation;
using static BongoPawClicker.User32API;
namespace BongoPawClicker
{
/// <summary>
/// MainWindow.xaml
/// </summary>
///
public partial class MainWindow : Window
{
public bool meow;
public bool darkMode;
public bool topmost;
//Default Hot Key is Alt,Control + P
private Key hotKey;
private ModifierKeys hotKeyModifiers;
System.Media.SoundPlayer meowAudio;
public MainWindow()
{
InitializeComponent();
//Restore HotKey
hotKey = (Key)Enum.Parse(typeof(Key), Properties.Settings.Default.HotKey);
hotKeyModifiers = (ModifierKeys)Enum.Parse(typeof(ModifierKeys), Properties.Settings.Default.Modifiers);
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
//Read User's Configuration
meow = Properties.Settings.Default.Meow;
darkMode = Properties.Settings.Default.DarkMode;
if (darkMode)
{
SwitchToDarkMode();
}
topmost = Properties.Settings.Default.Topmost;
this.Topmost = topmost;
//Read User's Last Input
ClickTypeSelection.SelectedIndex = Properties.Settings.Default.ClickType;
MinsTextBox.Text = Properties.Settings.Default.ClickIntervalMins;
SecsTextBox.Text = Properties.Settings.Default.ClickIntervalSecs;
MsTextBox.Text = Properties.Settings.Default.ClickIntervalms;
RandomDelayEnabledToggleButton.IsChecked = Properties.Settings.Default.RandomDelayEnable;
RandomDelaySecsTextBox.Text = Properties.Settings.Default.RandomDelaySecs;
RandomDelayMsTextBox.Text = Properties.Settings.Default.RandomDelayms;
PositionXTextBox.Text = Properties.Settings.Default.XPos;
PositionYTextBox.Text = Properties.Settings.Default.YPos;
RandomAreaEnabledToggleButton.IsChecked = Properties.Settings.Default.RandomAreaEnabled;
RandomAreaWidth.Text = Properties.Settings.Default.RandomAreaWidth;
RandomAreaHeight.Text = Properties.Settings.Default.RandomAreaHeight;
InfinityClickEnabledToggleButton.IsChecked = Properties.Settings.Default.InfinityClickEnabled;
ClickTimesTextBox.Text = Properties.Settings.Default.ClickTimes;
meowAudio = new System.Media.SoundPlayer(Properties.Resources.meow);
}
#region HotKey
//Register HotKey on program start
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
Regist(this, hotKeyModifiers, hotKey, () =>
{
if (StopButton.IsEnabled) { StopButton.RaiseEvent(new RoutedEventArgs(System.Windows.Controls.Primitives.ButtonBase.ClickEvent)); }
else { StartButton.RaiseEvent(new RoutedEventArgs(System.Windows.Controls.Primitives.ButtonBase.ClickEvent)); }
});
}
//Update HotKey
public void UpdateHotKey(Key key, ModifierKeys modifiers)
{
hotKey = key;
hotKeyModifiers = modifiers;
UpdateHintBox(0);
ModifyGlobalHotKey(new WindowInteropHelper(this), key, modifiers);
}
#endregion
#region UI
//Update Hint TextBox
public void UpdateHintBox(int signal)
{
if (CultureInfo.CurrentUICulture.Name == "zh-CN")
{
switch (signal)
{
case 0:
HintTextBox.Text = $"当前快捷键为 {hotKeyModifiers} + {hotKey}";
break;
case 1:
HintTextBox.Text = "点击完成了喵~";
break;
case 2:
HintTextBox.Text = $"点击开始!按下 {hotKeyModifiers} + {hotKey} 来停止喵~";
break;
case 3:
HintTextBox.Text = $"无限点击开启, 记得用 {hotKeyModifiers} + {hotKey} 来退出喵~";
break;
case 4:
HintTextBox.Text = "无限点击已关闭喵~";
break;
}
}
else
{
switch (signal)
{
case 0:
HintTextBox.Text = $"Current hotKey is {hotKeyModifiers} + {hotKey}";
break;
case 1:
HintTextBox.Text = "Click Completed! Meow~";
break;
case 2:
HintTextBox.Text = $"Click started! Press {hotKeyModifiers} + {hotKey} to stop!";
break;
case 3:
HintTextBox.Text = $"Infinite clicks enabled, remember to use {hotKeyModifiers} + {hotKey} to stop it!";
break;
case 4:
HintTextBox.Text = "Infinite clicks disabled";
break;
}
}
}
private void WindowsDragZone(object sender, MouseButtonEventArgs e)
{
DragMove();
}
//Load Setting Panel
private void SettingButton_Click(object sender, RoutedEventArgs e)
{
SettingPanel settingPanel = new SettingPanel(hotKey, hotKeyModifiers);
settingPanel.WindowStartupLocation = WindowStartupLocation.CenterOwner;
settingPanel.Owner = this;
settingPanel.ShowDialog();
}
//Theme Settings
public void SwitchToDarkMode()
{
var paletteHelper = new PaletteHelper();
PrimaryColor primary = PrimaryColor.Amber;
Color primaryColor = SwatchHelper.Lookup[(MaterialDesignColor)primary];
Resources["CardBackgroundColor"] = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#3d3d3d"));
Resources["IndicatorColor"] = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#ffb330"));
ITheme theme = paletteHelper.GetTheme();
theme.SetBaseTheme(Theme.Dark);
theme.SetPrimaryColor(primaryColor);
paletteHelper.SetTheme(theme);
darkMode = true;
}
public void SwitchToLightMode()
{
var paletteHelper = new PaletteHelper();
PrimaryColor primary = PrimaryColor.DeepPurple;
Color primaryColor = SwatchHelper.Lookup[(MaterialDesignColor)primary];
Resources["CardBackgroundColor"] = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#f8f8ff"));
Resources["IndicatorColor"] = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#b39ddb"));
ITheme theme = paletteHelper.GetTheme();
theme.SetBaseTheme(Theme.Light);
theme.SetPrimaryColor(primaryColor);
paletteHelper.SetTheme(theme);
darkMode = false;
}
//Load Random Area Selector
private void PositionSelectButton_Click(object sender, RoutedEventArgs e)
{
//Minimising the main window will cause the child window to lose focus
//So here the main window is set to transparent to achieve the same visual effect
this.Opacity = 0;
//this.WindowState = WindowState.Minimized;
PositionSelector positionSelector = new PositionSelector(RandomAreaEnabledToggleButton.IsChecked.Value);
positionSelector.Owner = this;
positionSelector.ShowDialog();
}
public void UpdateClickPositionTextBox(int x, int y, int w, int h)
{
try
{
PositionXTextBox.Text = x.ToString();
PositionYTextBox.Text = y.ToString();
RandomAreaWidth.Text = w.ToString();
RandomAreaHeight.Text = h.ToString();
}
catch (Exception ex)
{
ErrorMessageOutput(ex.ToString());
return;
}
}
//Animation: Minimize Window
private void MinusButton_Click(object sender, RoutedEventArgs e)
{
var story = (Storyboard)this.Resources["MinimizeWindow"];
if (story != null)
{
story.Completed += delegate { this.WindowState = WindowState.Minimized; };
story.Begin(this);
}
//this.WindowState = WindowState.Minimized;
}
//Animation: Restore Window From TaskBar
private void Window_StateChanged(object sender, EventArgs e)
{
if (WindowState == WindowState.Normal && this.Opacity==0)
{
var story = (Storyboard)this.Resources["MaximizeWindow"];
story.Begin(this);
}
}
private void CloseButton_Click(object sender, RoutedEventArgs e)
{
//Save User's Options
Properties.Settings.Default.Meow = meow;
Properties.Settings.Default.DarkMode = darkMode;
Properties.Settings.Default.Topmost = topmost;
Properties.Settings.Default.HotKey = hotKey.ToString();
Properties.Settings.Default.Modifiers = hotKeyModifiers.ToString();
//Save User's Input
Properties.Settings.Default.ClickType = ClickTypeSelection.SelectedIndex;
Properties.Settings.Default.ClickIntervalMins = MinsTextBox.Text;
Properties.Settings.Default.ClickIntervalSecs = SecsTextBox.Text;
Properties.Settings.Default.ClickIntervalms = MsTextBox.Text;
Properties.Settings.Default.RandomDelayEnable = RandomDelayEnabledToggleButton.IsChecked.Value;
Properties.Settings.Default.RandomDelaySecs = RandomDelaySecsTextBox.Text;
Properties.Settings.Default.RandomDelayms = RandomDelayMsTextBox.Text;
Properties.Settings.Default.XPos = PositionXTextBox.Text;
Properties.Settings.Default.YPos = PositionYTextBox.Text;
Properties.Settings.Default.RandomAreaEnabled = RandomAreaEnabledToggleButton.IsChecked.Value;
Properties.Settings.Default.RandomAreaWidth = RandomAreaWidth.Text;
Properties.Settings.Default.RandomAreaHeight = RandomAreaHeight.Text;
Properties.Settings.Default.InfinityClickEnabled = InfinityClickEnabledToggleButton.IsChecked.Value;
Properties.Settings.Default.ClickTimes = ClickTimesTextBox.Text;
Properties.Settings.Default.Save();
//Close Animation
var story = (Storyboard)this.Resources["HideWindow"];
if (story != null)
{
story.Completed += delegate { Close(); };
story.Begin(this);
}
//Close();
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
UnRegist(new WindowInteropHelper(this));
Environment.Exit(0);
}
private void InfinityClickEnabledToggleButton_Checked(object sender, RoutedEventArgs e)
{
UpdateHintBox(3);
}
private void InfinityClickEnabledToggleButton_Unchecked(object sender, RoutedEventArgs e)
{
UpdateHintBox(4);
}
private void StartButton_Click(object sender, RoutedEventArgs e)
{
StartButton.IsEnabled = false;
StopButton.IsEnabled = true;
UpdateHintBox(2);
Thread clickLuncher = new Thread(ClickLuncher);
clickLuncher.Start();
}
private void StopButton_Click(object sender, RoutedEventArgs e)
{
StopButton.IsEnabled = false;
StartButton.IsEnabled = true;
}
#endregion
#region Exception Handle
private void ErrorMessageOutput(string message)
{
MessageBox.Show(message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
}
//Limit the text box to numbers only
private void NumberValidationTextBox(object sender, TextCompositionEventArgs e)
{
Regex regex = new Regex("[^0-9]+");
e.Handled = regex.IsMatch(e.Text);
}
//Verify if the input value is a number
private int ValidIntConvertor(string text)
{
int res;
try
{
res = Int32.Parse((text == "") ? "0" : text);
return res;
}
catch (Exception ex)
{
ErrorMessageOutput(ex.ToString());
return 0;
}
}
#endregion
//Read in the relevant parameters and implement mouse clicks
private void ClickLuncher()
{
Random random = new Random();
//Click Type Parameters
int clickType = -1;
//Click Interval Parameters
bool randomDelayEnabled = false;
int clickInterval = 0;
int randomDelay = 0;
//Click Position Parameters
bool randomAreaEnabled = false;
int xPos = 0, yPos = 0;
int randomAreaWidth = 0, randomAreaHeight = 0;
//Click Times Parameters
bool infinityClickEnabled = false;
int clickTimes = 0;
Dispatcher.Invoke((Action)(() =>
{
//Get Click Type
clickType = ClickTypeSelection.SelectedIndex;
//Get Click Interval
try
{
randomDelayEnabled = RandomDelayEnabledToggleButton.IsChecked.Value;
clickInterval = (ValidIntConvertor(MinsTextBox.Text) * 60
+ ValidIntConvertor(SecsTextBox.Text)) * 1000
+ ValidIntConvertor(MsTextBox.Text);
clickInterval = (clickInterval == 0) ? 1 : clickInterval;
if (randomDelayEnabled)
{
randomDelay = ValidIntConvertor(RandomDelaySecsTextBox.Text) * 1000
+ ValidIntConvertor(RandomDelayMsTextBox.Text);
}
}
catch (Exception ex)
{
ErrorMessageOutput(ex.ToString());
return;
}
//Get Click Position
try
{
randomAreaEnabled = RandomAreaEnabledToggleButton.IsChecked.Value;
xPos = ValidIntConvertor(PositionXTextBox.Text);
yPos = ValidIntConvertor(PositionYTextBox.Text);
if (randomAreaEnabled)
{
randomAreaWidth = ValidIntConvertor(RandomAreaWidth.Text);
randomAreaHeight = ValidIntConvertor(RandomAreaHeight.Text);
}
}
catch (Exception ex)
{
ErrorMessageOutput(ex.ToString());
return;
}
//Get Click Times
if (!infinityClickEnabled)
{
try
{
infinityClickEnabled = InfinityClickEnabledToggleButton.IsChecked.Value;
clickTimes = ValidIntConvertor(ClickTimesTextBox.Text);
}
catch (Exception ex)
{
ErrorMessageOutput(ex.ToString());
return;
}
}
}));
//Do Clicks
int xCoord = xPos, yCoord = yPos; //Click Position Coords
bool manualStop = false;
while (infinityClickEnabled || clickTimes > 0)
{
Dispatcher.Invoke((Action)(() =>
{
manualStop = !StopButton.IsEnabled;
NonePaw.Visibility = Visibility.Collapsed;
}));
if (manualStop) { break; }
//Calculate random click position if random click areas are enabled
if (randomAreaEnabled)
{
xCoord = (randomAreaWidth == 0) ? xPos : random.Next(xPos, xPos + randomAreaWidth + 1);
yCoord = (randomAreaHeight == 0) ? yPos : random.Next(yPos, yPos + randomAreaHeight + 1);
}
//Click Type
if (clickType == 0)//Left Single Click
{
Dispatcher.InvokeAsync((Action)(async () =>
{
LeftPaw.Visibility = Visibility.Visible;
NonePaw.Visibility = Visibility.Collapsed;
await Task.Delay(100);
LeftPaw.Visibility = Visibility.Collapsed;
NonePaw.Visibility = Visibility.Visible;
}));
User32API.ClickOnScreen(0, xCoord, yCoord);
}
else if (clickType == 1)//Left Double Click
{
//Cat Paw Down
Dispatcher.InvokeAsync((Action)(async () =>
{
BothPaw.Visibility = Visibility.Visible;
NonePaw.Visibility = Visibility.Collapsed;
await Task.Delay(100);
BothPaw.Visibility = Visibility.Collapsed;
NonePaw.Visibility = Visibility.Visible;
}));
User32API.ClickOnScreen(0, xCoord, yCoord);//First Left Click
//Double Click Interval
if (randomDelayEnabled)
{
Thread.Sleep(random.Next(50, 300));//Simulate manual double-click delay
}
else
{
Thread.Sleep(200);
}
//Cat Paw Up
Dispatcher.InvokeAsync((Action)(async () =>
{
BothPaw.Visibility = Visibility.Visible;
NonePaw.Visibility = Visibility.Collapsed;
await Task.Delay(100);
BothPaw.Visibility = Visibility.Collapsed;
NonePaw.Visibility = Visibility.Visible;
}));
User32API.ClickOnScreen(0, xCoord, yCoord);//Second Left Click
}
else//Right Single Click
{
Dispatcher.InvokeAsync((Action)(async () =>
{
RightPaw.Visibility = Visibility.Visible;
NonePaw.Visibility = Visibility.Collapsed;
await Task.Delay(100);
RightPaw.Visibility = Visibility.Collapsed;
NonePaw.Visibility = Visibility.Visible;
}));
User32API.ClickOnScreen(2, xCoord, yCoord);
}
//Click Interval
if (randomDelayEnabled)
{
Thread.Sleep(clickInterval + random.Next(0, randomDelay));
}
else
{
Thread.Sleep(clickInterval);
}
if (!infinityClickEnabled) { clickTimes--; }
}
Dispatcher.InvokeAsync((Action)(() =>
{
if (StopButton.IsEnabled)
{
StopButton.IsEnabled = false;
StartButton.IsEnabled = true;
}
NonePaw.Visibility = Visibility.Visible;
LeftPaw.Visibility = Visibility.Collapsed;
RightPaw.Visibility = Visibility.Collapsed;
BothPaw.Visibility = Visibility.Collapsed;
if (meow)
{
meowAudio.Play();
}
UpdateHintBox(1);
}));
}
}
}