-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainForm.cs
More file actions
64 lines (52 loc) · 1.54 KB
/
MainForm.cs
File metadata and controls
64 lines (52 loc) · 1.54 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
using System.Runtime.InteropServices;
namespace ShutdownTimer;
public partial class Main : Form {
private readonly TextBox textMinutes;
private readonly Button buttonStart;
private readonly ShutdownTimer shutdownTimer;
public Main() {
InitializeComponent();
textMinutes = (TextBox)Controls.Find("TextMinutes", false)[0];
buttonStart = (Button)Controls.Find("ButtonStart", false)[0];
shutdownTimer = new();
shutdownTimer.StateChanged += ShutdownTimer_StateChanged;
ButtonStart_Click(this, EventArgs.Empty);
}
private void ShutdownTimer_StateChanged(bool active) {
WriteState(active);
}
private void WriteState(bool active) {
if (buttonStart.InvokeRequired) {
buttonStart.Invoke(WriteState, active);
return;
}
textMinutes.Enabled = !active;
if (active) {
buttonStart.BackColor = Color.DarkRed;
buttonStart.Text = "Stop";
return;
}
buttonStart.BackColor = Color.DarkGreen;
buttonStart.Text = "Start";
}
private void ButtonStart_Click(object sender, EventArgs e) {
if (!int.TryParse(textMinutes.Text, out int minutes)) {
textMinutes.Text = "30";
return;
}
if (shutdownTimer.Active) {
shutdownTimer.Stop();
return;
}
shutdownTimer.Start(minutes);
}
#region DarkForm
[LibraryImport("DwmApi")]
private static partial int DwmSetWindowAttribute(IntPtr hwnd, int attr, int[] attrValue, int attrSize);
protected override void OnHandleCreated(EventArgs e) {
if (DwmSetWindowAttribute(Handle, 19, new[] { 1 }, 4) != 0) {
_ = DwmSetWindowAttribute(Handle, 20, new[] { 1 }, 4);
}
}
#endregion
}