-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
280 lines (235 loc) · 9.26 KB
/
Form1.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
using Microsoft.Win32;
using System;
using System.Threading;
using System.Windows.Forms;
using System.Text;
using Auto_VOL.Helpers;
using System.Security.Principal;
namespace Auto_VOL
{
public partial class Form1 : Form
{
private CancellationTokenSource cancellationTokenSource;
private CancellationToken cancellationToken;
private readonly VolumeHelper _volumeHelper;
private readonly ProcessHelper _processHelper;
private bool isOn;
private const string APP_NAME = "Auto_VOL";
private const string DEFAULT_PROCESS_NAME = "notepad";
private const int DEFAULT_VOL_ON_OPEN = 6;
private const int DEFAULT_VOL_ON_CLOSE = 2;
public Form1()
{
InitializeComponent();
_volumeHelper = new VolumeHelper();
_processHelper = new ProcessHelper();
UpdateToken();
}
private void Form1_Load(object sender, EventArgs e)
{
string processName = Properties.Settings.Default.processName;
tb_processName.Text = processName;
int volOnOpen = Properties.Settings.Default.volOnOpen;
tb_onOpenVol.Text = volOnOpen.ToString();
int volOnClose = Properties.Settings.Default.volOnClose;
tb_onCloseVol.Text = volOnClose.ToString();
isOn = Properties.Settings.Default.isOn;
VolumeAndProcess volProc = new VolumeAndProcess
{
VolOnClose = volOnClose,
VolOnOpen = volOnOpen,
ProcessName = processName
};
ChangeState(isOn, volProc);
ContextMenu contextMenu = new ContextMenu();
contextMenu.MenuItems.Add(new MenuItem("Exit", ExitApp_Click));
notifyIcon1.ContextMenu = contextMenu;
}
private void btn_changeOnOff_Click(object sender, EventArgs e)
{
if (isOn != true)
{
string message = string.Empty;
string processName = tb_processName.Text;
if (string.IsNullOrEmpty(processName))
{
message += "The process name cannot be null\n";
}
string volOnCloseStr = tb_onCloseVol.Text;
int? volOnClose = volOnCloseStr.ToSafeZeroToHundredInt();
if (!volOnClose.HasValue)
{
message += $"Volume on close: {volOnCloseStr} is not a valid number\n";
}
string volOnOpenStr = tb_onOpenVol.Text;
int? volOnOpen = volOnOpenStr.ToSafeZeroToHundredInt();
if (!volOnOpen.HasValue)
{
message += $"Volume on open: {volOnOpenStr} is not a valid number\n";
}
if(message != string.Empty)
{
MessageBox.Show(message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
VolumeAndProcess volAnProc = new VolumeAndProcess
{
VolOnClose = volOnClose.Value,
VolOnOpen = volOnOpen.Value,
ProcessName = processName
};
tb_onOpenVol.Text = volAnProc.VolOnOpen.ToString();
tb_onCloseVol.Text = volAnProc.VolOnClose.ToString();
SaveSettings(volAnProc);
UpdateVolProcess(volAnProc);
btn_changeOnOff.Text = "Stop";
isOn = true;
}
else
{
cancellationTokenSource.Cancel();
btn_changeOnOff.Text = "Start";
isOn = false;
}
Properties.Settings.Default.isOn = isOn;
Properties.Settings.Default.Save();
}
private void SaveSettings(VolumeAndProcess volAndProc)
{
Properties.Settings.Default.volOnOpen = volAndProc.VolOnOpen;
Properties.Settings.Default.volOnClose = volAndProc.VolOnClose;
Properties.Settings.Default.processName = volAndProc.ProcessName;
Properties.Settings.Default.Save();
}
private void UpdateToken()
{
cancellationTokenSource = new CancellationTokenSource();
cancellationToken = cancellationTokenSource.Token;
}
private void btn_exitApp_Click(object sender, EventArgs e)
{
ExitApp();
}
private void ExitApp_Click(object sender, EventArgs e)
{
ExitApp();
}
private void ExitApp()
{
notifyIcon1.Dispose();
Application.Exit();
}
private void ChangeState(bool isOn, VolumeAndProcess volAndProc = null)
{
this.isOn = isOn;
if (isOn)
{
if (volAndProc == null)
{
throw new ArgumentNullException(nameof(volAndProc));
}
btn_changeOnOff.Text = "Stop";
UpdateVolProcess(volAndProc);
}
else
{
cancellationTokenSource.Cancel();
UpdateToken();
btn_changeOnOff.Text = "Start";
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
WindowState = FormWindowState.Minimized;
Hide();
notifyIcon1.Visible = true;
notifyIcon1.BalloonTipText = "Application minimized";
notifyIcon1.BalloonTipTitle = APP_NAME;
notifyIcon1.ShowBalloonTip(2);
}
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
Show();
WindowState = FormWindowState.Normal;
}
private void UpdateVolProcess(VolumeAndProcess volumeAndProcess)
{
cancellationTokenSource.Cancel();
UpdateToken();
_processHelper.WhenProcessStartOrCloses(_volumeHelper.SetVolume, volumeAndProcess.VolOnOpen, _volumeHelper.SetVolume, volumeAndProcess.VolOnClose, volumeAndProcess.ProcessName, cancellationToken);
}
private void btn_defaultSettings_Click(object sender, EventArgs e)
{
ChangeState(false);
Properties.Settings.Default.isOn = false;
Properties.Settings.Default.processName = DEFAULT_PROCESS_NAME;
Properties.Settings.Default.volOnClose = DEFAULT_VOL_ON_CLOSE;
Properties.Settings.Default.volOnOpen = DEFAULT_VOL_ON_OPEN;
tb_onCloseVol.Text = DEFAULT_VOL_ON_CLOSE.ToString();
tb_onOpenVol.Text = DEFAULT_VOL_ON_OPEN.ToString();
tb_processName.Text = DEFAULT_PROCESS_NAME;
Properties.Settings.Default.Save();
}
private void ChangeStartWithSO(bool enable)
{
try
{
string runKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
RegistryKey startupKey = Registry.LocalMachine.OpenSubKey(runKey, true);
bool hasStartupKey = startupKey.GetValue(APP_NAME) != null;
if (enable)
{
if (!hasStartupKey)
{
startupKey.SetValue(APP_NAME, @"""" + Application.ExecutablePath.ToString() + @"""");
startupKey.Close();
}
MessageBox.Show("The program will starts with the SO", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
if (hasStartupKey)
{
startupKey.DeleteValue(APP_NAME, false);
startupKey.Close();
}
MessageBox.Show("The program will not starts with the SO", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show("Try to run as administrator!\n\n" + ex.Message);
}
}
private bool? WillStartWithOS()
{
try
{
RegistryKey startupKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
bool hasStartupKey = startupKey.GetValue(APP_NAME) != null;
return hasStartupKey;
}
catch (Exception)
{
return null;
}
}
private void btn_startWithOS_Click(object sender, EventArgs e)
{
WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity);
bool isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator);
if (isAdmin == false)
{
MessageBox.Show("You need to run as administrator to use this function!");
return;
}
bool startWithOS = WillStartWithOS().Value;
ChangeStartWithSO(!startWithOS);
}
}
}