-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSettingsForm.cs
181 lines (163 loc) · 6.15 KB
/
SettingsForm.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Reflection;
namespace PowerControl
{
public partial class mySettingsForm : Form
{
const string myApplicationKey = "PowerControl";
uint myPreviousExecutionState = 0;
bool myExecutionStateChanged = false;
DateTime myCountDownTarget;
uint[] myCountDowns = new uint[] { 60, 3600, 7200, 10800, 18000, 28800, 43200, 86400, 172800, 259200, 432000 };
bool myReallyClose = false;
Properties.Settings mySettings = new PowerControl.Properties.Settings();
public mySettingsForm()
{
InitializeComponent();
myRemainingTime.Text = "";
myComboBox.SelectedIndex = mySettings.LastTimerValueIndex;
myStartMinimizedCheckBox.Checked = mySettings.StartMinimized;
if (mySettings.LastX != -1)
{
this.StartPosition = FormStartPosition.Manual;
this.Location = new Point(mySettings.LastX, mySettings.LastY);
}
myStartAtLoginCheckBox.Checked = (AutostartUility.IsAutoStartEnabled(myApplicationKey) != null);
}
void myTimer_Tick(object sender, EventArgs e)
{
TimeSpan remaining = TimeSpan.FromSeconds(
Convert.ToInt32((myCountDownTarget - DateTime.Now).TotalSeconds));
if (remaining.TotalSeconds <= 0)
{
myPreventCheckBox.Checked = false;
}
else
{
myRemainingTime.Text = remaining.ToString();
myNotifyIcon.Text = "Prevent System to sleep for " + remaining.ToString();
preventSleepMenuItem.Text = "Prevent Sleep for " + remaining.ToString();
}
}
private void myPreventCheckBox_CheckedChanged(object sender, EventArgs e)
{
if (!myPreventCheckBox.Checked)
{
myTimer.Stop();
this.myNotifyIcon.Icon = Properties.Resources.BlueSuspend16;
myRemainingTime.Text = "";
NativeMethods.SetThreadExecutionState(myPreviousExecutionState);
myExecutionStateChanged = false;
myComboBox.Show();
myNotifyIcon.Text = "Do not prevent System to sleep";
}
else
{
myPreviousExecutionState = NativeMethods.SetThreadExecutionState(NativeMethods.ES_CONTINUOUS | NativeMethods.ES_SYSTEM_REQUIRED);
myExecutionStateChanged = true;
myCountDownTarget = DateTime.Now + TimeSpan.FromSeconds(myCountDowns[myComboBox.SelectedIndex]);
myTimer.Start();
myComboBox.Hide();
this.myNotifyIcon.Icon = Properties.Resources.OrangeSuspend16;
}
preventSleepMenuItem.Checked = myPreventCheckBox.Checked;
}
private void notifyIcon_DoubleClick(object sender, EventArgs e)
{
this.Show();
this.BringToFront();
}
private void exitMenuItem_Click(object sender, EventArgs e)
{
myReallyClose = true;
this.Close();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (myReallyClose)
{
if (myExecutionStateChanged)
{
myTimer.Stop();
myTimer.Dispose();
NativeMethods.SetThreadExecutionState(myPreviousExecutionState);
}
Point p = this.Location;
mySettings.LastX = p.X;
mySettings.LastY = p.Y;
mySettings.Save();
myNotifyIcon.Dispose();
}
else
{
e.Cancel = true;
this.Hide();
}
}
private void settingsMenuItem_Click(object sender, EventArgs e)
{
this.Show();
this.BringToFront();
}
private void myComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
mySettings.LastTimerValueIndex = myComboBox.SelectedIndex;
mySettings.Save();
preventSleepMenuItem.Text = "Prevent Sleep for " + myComboBox.SelectedItem as String;
}
private void preventSleepMenuItem_Click(object sender, EventArgs e)
{
preventSleepMenuItem.Checked = !preventSleepMenuItem.Checked;
myPreventCheckBox.Checked = preventSleepMenuItem.Checked;
}
private void preventSleepMenuItem_CheckedChanged(object sender, EventArgs e)
{
if (!preventSleepMenuItem.Checked)
{
preventSleepMenuItem.Text = "Prevent Sleep for " + myComboBox.SelectedItem as String;
}
}
private void startMinimized_CheckedChanged(object sender, EventArgs e)
{
mySettings.StartMinimized = myStartMinimizedCheckBox.Checked;
mySettings.Save();
}
private void SettingsForm_Shown(object sender, EventArgs e)
{
if (mySettings.StartMinimized)
{
this.Hide();
}
}
private void startAtLogin_CheckedChanged(object sender, EventArgs e)
{
if (myStartAtLoginCheckBox.Checked)
{
AutostartUility.SetAutoStart(myApplicationKey, Assembly.GetExecutingAssembly().Location);
}
else
{
AutostartUility.UnSetAutoStart(myApplicationKey);
}
}
private void SettingsForm_Move(object sender, EventArgs e)
{
}
}
internal static class NativeMethods
{
// Import SetThreadExecutionState Win32 API and necessary flags
[DllImport("kernel32.dll")]
public static extern uint SetThreadExecutionState(uint esFlags);
public const uint ES_CONTINUOUS = 0x80000000;
public const uint ES_SYSTEM_REQUIRED = 0x00000001;
}
}