-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainForm.cs
More file actions
300 lines (282 loc) · 13.5 KB
/
MainForm.cs
File metadata and controls
300 lines (282 loc) · 13.5 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
using ClickLoop.Services;
using MaterialSkin;
using MaterialSkin.Controls;
using System.Diagnostics;
using System.Globalization;
using System.Resources;
using System.Runtime.InteropServices;
namespace ClickLoop
{
public partial class MainForm : MaterialForm
{
//###################################
[DllImport("User32.dll")]
static extern int SetForegroundWindow(IntPtr point);
[DllImport("User32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
//###################################
public static bool running = false;
public static string version = "1.0.1";
public static string TextToSend;
private readonly ProcessService _processService;
private readonly LoopService _loopService;
CancellationTokenSource s_cts;
private IList<string> _runningProcesses;
private NotifyIcon trayIcon;
private ContextMenuStrip trayMenu;
private bool hasShownTrayNotification = false;
private bool allowClose = false;
private ResourceManager resManager = new ResourceManager("ClickLoop.Resources.Strings", typeof(MainForm).Assembly);
private CultureInfo currentCulture = CultureInfo.CurrentUICulture;
private ToolStripMenuItem settingsToolStripMenuItem;
private ToolStripMenuItem languageToolStripMenuItem;
private ToolStripMenuItem englishToolStripMenuItem;
private ToolStripMenuItem frenchToolStripMenuItem;
//####################################
public MainForm()
{
InitializeLanguageAndCulture();
InitializeMaterialSkin();
_loopService = new LoopService();
_processService = new ProcessService();
InitializeComponent();
InitializeFormControls();
InitializeMenuStripStyling();
InitializeTrayIconAndMenu();
InitializeSettingsMenu();
}
private void InitializeLanguageAndCulture()
{
var lang = Properties.Settings.Default.Language;
if (!string.IsNullOrEmpty(lang) && lang != currentCulture.TwoLetterISOLanguageName)
{
currentCulture = new CultureInfo(lang);
}
}
private void InitializeMaterialSkin()
{
var materialSkinManager = MaterialSkinManager.Instance;
materialSkinManager.AddFormToManage(this);
materialSkinManager.Theme = MaterialSkinManager.Themes.LIGHT;
materialSkinManager.ColorScheme = new ColorScheme(Primary.Blue600, Primary.Blue700, Primary.Blue200, Accent.LightBlue200, TextShade.WHITE);
}
private void InitializeFormControls()
{
labelDescription.AutoSize = true;
labelDescription.MaximumSize = new Size(400, 0);
refresh();
numericUpDown1.Value = Properties.Settings.Default.loopRotation;
numericUpDown2.Value = Properties.Settings.Default.keySpeed;
}
private void InitializeMenuStripStyling()
{
menuStrip1.BackColor = Color.FromArgb(33, 150, 243);
menuStrip1.ForeColor = Color.White;
menuStrip1.Font = new Font("Segoe UI", 10, FontStyle.Regular);
foreach (ToolStripMenuItem menuItem in menuStrip1.Items)
{
menuItem.ForeColor = Color.White;
menuItem.BackColor = Color.FromArgb(33, 150, 243);
menuItem.Font = new Font("Segoe UI", 10, FontStyle.Regular);
foreach (ToolStripItem subItem in menuItem.DropDownItems)
{
subItem.ForeColor = Color.Black;
subItem.BackColor = Color.White;
subItem.Font = new Font("Segoe UI", 10, FontStyle.Regular);
}
}
}
private void InitializeTrayIconAndMenu()
{
trayMenu = new ContextMenuStrip();
trayMenu.Items.Clear();
trayMenu.Items.Add(resManager.GetString("Start", currentCulture), null, (s, e) => StartLoopButton(null, null));
trayMenu.Items.Add(resManager.GetString("Stop", currentCulture), null, (s, e) => CancelLoopButton(null, null));
trayMenu.Items.Add(resManager.GetString("Show", currentCulture), null, (s, e) => { this.Show(); this.WindowState = FormWindowState.Normal; this.BringToFront(); });
trayMenu.Items.Add(resManager.GetString("Exit", currentCulture), null, (s, e) => { allowClose = true; Application.Exit(); });
trayIcon = new NotifyIcon
{
Text = resManager.GetString("AppName", currentCulture),
Icon = new Icon(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "loop.ico")),
ContextMenuStrip = trayMenu,
Visible = false
};
trayIcon.DoubleClick += (s, e) => { this.Show(); this.WindowState = FormWindowState.Normal; this.BringToFront(); };
}
private void InitializeSettingsMenu()
{
settingsToolStripMenuItem = new ToolStripMenuItem(resManager.GetString("Settings", currentCulture));
languageToolStripMenuItem = new ToolStripMenuItem(resManager.GetString("Language", currentCulture));
englishToolStripMenuItem = new ToolStripMenuItem("English");
frenchToolStripMenuItem = new ToolStripMenuItem("Français");
englishToolStripMenuItem.Click += (s, e) => ChangeLanguage("en");
frenchToolStripMenuItem.Click += (s, e) => ChangeLanguage("fr");
languageToolStripMenuItem.DropDownItems.Add(englishToolStripMenuItem);
languageToolStripMenuItem.DropDownItems.Add(frenchToolStripMenuItem);
settingsToolStripMenuItem.DropDownItems.Add(languageToolStripMenuItem);
menuStrip1.Items.Add(settingsToolStripMenuItem);
}
private async void StartLoopButton(object sender, EventArgs e)
{
string selectedProcess = ListProcesses.SelectedValue?.ToString();
if (string.IsNullOrEmpty(selectedProcess) || string.IsNullOrEmpty(TextToSend))
{
MessageBox.Show(resManager.GetString("NoMatchingProcess", currentCulture), resManager.GetString("Error", currentCulture));
return;
}
int interval = (int)(numericUpDown1.Value * 1000);
int speed = (int)(numericUpDown2.Value * 1000);
UpdateUIForLoop(true);
await _loopService.StartLoopAsync(interval, speed, TextToSend, _processService.GetProcessByName(selectedProcess),
onCancel: () => UpdateUIForLoop(false),
onError: message => MessageBox.Show(message, resManager.GetString("Error", currentCulture)));
}
private void CancelLoopButton(object sender, EventArgs e)
{
_loopService.CancelLoop();
UpdateUIForLoop(false);
}
private void textInput_TextChanged(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(materialTextInput.Text))
{
TextToSend = materialTextInput.Text;
TextToSend = TextToSend.Replace(" ", " {BS} ");
TextToSend = TextToSend.Replace("\r\n", " {ENTER} ");
}
}
private void BuyMeCoffee(object sender, EventArgs e)
{
MessageBoxButtons messageBoxButtons = MessageBoxButtons.YesNo;
DialogResult result = MessageBox.Show(resManager.GetString("RedirectMessage", currentCulture), resManager.GetString("RedirectTitle", currentCulture), messageBoxButtons);
if (result == DialogResult.Yes) Process.Start("https://www.paypal.com/paypalme/aymenajroud");
}
private void supportToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show(resManager.GetString("TutorialTips", currentCulture), resManager.GetString("TutorialTitle", currentCulture));
}
private void versionToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBoxButtons messageBoxButtons = MessageBoxButtons.YesNo;
DialogResult result = MessageBox.Show(string.Format(resManager.GetString("VersionMessage", currentCulture), version), resManager.GetString("VersionTitle", currentCulture), messageBoxButtons);
if (result == DialogResult.Yes)
Process.Start("https://ajroudsoftwares.tn");
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void refreshButton(object sender, EventArgs e)
{
refresh();
}
private void contactMeToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBoxButtons messageBoxButtons = MessageBoxButtons.YesNo;
DialogResult result = MessageBox.Show(resManager.GetString("ContactMessage", currentCulture), resManager.GetString("ContactTitle", currentCulture), messageBoxButtons);
if (result == DialogResult.Yes) Process.Start("mailto:contact@ajroudsoftwares.tn?subject=ClickLoopContactMe");
}
private void refresh()
{
_runningProcesses = _processService.GetRunningProcesses();
ListProcesses.DataSource = _runningProcesses;
}
private void ListProcessesComboBox_ValueChanged(object sender, EventArgs e)
{
string ProcInfo = ((ComboBox)sender).Text;
try
{
var process = _processService.GetProcessByName(ProcInfo);
labelDescription.Text = resManager.GetString("Description", currentCulture) + ":\n" + process.MainModule.FileVersionInfo.FileDescription;
pictureBox1.Image = Icon.ExtractAssociatedIcon(process.MainModule.FileName).ToBitmap();
toolTip1.SetToolTip(labelDescription, string.Empty);
}
catch (AccessViolationException)
{
labelDescription.Text = resManager.GetString("AccessDenied", currentCulture);
pictureBox1.Image = null;
}
catch (Exception ex)
{
labelDescription.Text = resManager.GetString("Description", currentCulture) + ": " + ex.Message;
toolTip1.SetToolTip(labelDescription, resManager.GetString("AdminTip", currentCulture));
pictureBox1.Image = null;
}
}
private void numericUpDown2_Validated(object sender, EventArgs e)
{
Properties.Settings.Default.keySpeed = ((NumericUpDown)sender).Value;
Properties.Settings.Default.Save();
}
private void numericUpDown1_Validated(object sender, EventArgs e)
{
Properties.Settings.Default.loopRotation = ((NumericUpDown)sender).Value;
Properties.Settings.Default.Save();
}
private void UpdateUIForLoop(bool isRunning)
{
running = isRunning;
materialButtonCancel.Enabled = isRunning;
materialButtonCancel.BackColor = isRunning ? Color.Red : Color.Gray;
materialButtonStart.Enabled = !isRunning;
}
private void ChangeLanguage(string lang)
{
if (lang == currentCulture.TwoLetterISOLanguageName)
return; // No change
currentCulture = new CultureInfo(lang);
Properties.Settings.Default.Language = lang;
Properties.Settings.Default.Save();
allowClose = true;
Application.Restart(); // Only restart when user changes language
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
if (this.WindowState == FormWindowState.Minimized)
{
this.Hide();
trayIcon.Visible = true;
if (!hasShownTrayNotification)
{
trayIcon.ShowBalloonTip(3000, resManager.GetString("AppName", currentCulture), resManager.GetString("TrayNotification", currentCulture), ToolTipIcon.Info);
hasShownTrayNotification = true;
}
}
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
if (!allowClose)
{
e.Cancel = true;
this.Hide();
trayIcon.Visible = true;
if (!hasShownTrayNotification)
{
trayIcon.ShowBalloonTip(3000, resManager.GetString("AppName", currentCulture), resManager.GetString("TrayNotification", currentCulture), ToolTipIcon.Info);
hasShownTrayNotification = true;
}
}
}
}
}
// Add resource files for localization
// Create Resources\Strings.resx (English) and Resources\Strings.fr.resx (French)
// Add keys for all UI/user-facing strings, e.g.:
// NoMatchingProcess: No matching process found
// Error: error
// TaskCanceled: task is Canceled
// ProcessTerminated: Process {0} was terminated, loop aborted!
// DescriptionAccessDenied: Description : access denied
// Description: Description:
// Settings: Settings
// Language: Language
// ... (add all other user-facing strings from MainForm.cs and LoopService.cs)
//
// In code, use resManager.GetString("Key", currentCulture) to fetch localized strings.
// To switch language, set currentCulture = new CultureInfo("fr");
//
// Example usage:
// MessageBox.Show(resManager.GetString("NoMatchingProcess", currentCulture), resManager.GetString("Error", currentCulture));
//
// (Resource files should be created in Resources folder in the project root)