-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSettingsForm.cs
More file actions
307 lines (283 loc) · 13.1 KB
/
SettingsForm.cs
File metadata and controls
307 lines (283 loc) · 13.1 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
301
302
303
304
305
306
307
using System;
using System.Windows.Forms;
namespace ScreenControl
{
public partial class SettingsForm : Form
{
// 公共属性,供父窗体访问设置值
public bool EnableHotkeys { get; private set; }
public int CloseScreenDelay { get; private set; } // 延迟关闭屏幕的时间(秒)
public int TurnOffScreenKey { get; private set; }
public MainForm.KeyModifier TurnOffScreenModifier { get; private set; }
public SettingsForm(bool enableHotkeys, int closeScreenDelay,
int turnOffScreenKey, MainForm.KeyModifier turnOffScreenModifier)
{
InitializeComponent();
// 初始化设置值
EnableHotkeys = enableHotkeys;
CloseScreenDelay = closeScreenDelay;
TurnOffScreenKey = turnOffScreenKey;
TurnOffScreenModifier = turnOffScreenModifier;
// 加载设置到界面
LoadSettingsToUI();
// 为快捷键输入框添加事件处理
txtTurnOffScreenKey.KeyDown += TxtHotkey_KeyDown;
}
private void LoadSettingsToUI()
{
// 设置复选框状态
chkEnableHotkeys.Checked = EnableHotkeys;
// 设置延迟时间
numCloseScreenDelay.Value = CloseScreenDelay;
// 设置关闭屏幕快捷键
cboTurnOffScreenModifier.SelectedIndex = (int)TurnOffScreenModifier;
txtTurnOffScreenKey.Text = ((Keys)TurnOffScreenKey).ToString();
// 根据热键启用状态更新控件可用性
UpdateHotkeyControlsEnabled();
}
private void UpdateHotkeyControlsEnabled()
{
bool isEnabled = chkEnableHotkeys.Checked;
cboTurnOffScreenModifier.Enabled = isEnabled;
txtTurnOffScreenKey.Enabled = isEnabled;
}
private void TxtHotkey_KeyDown(object sender, KeyEventArgs e)
{
// 阻止默认按键行为
e.SuppressKeyPress = true;
// 捕获按下的键和修饰键
TextBox textBox = sender as TextBox;
if (textBox != null)
{
// 记录按键信息
Keys key = e.KeyCode;
MainForm.KeyModifier modifier = MainForm.KeyModifier.None;
if (e.Alt)
modifier |= MainForm.KeyModifier.Alt;
if (e.Control)
modifier |= MainForm.KeyModifier.Control;
if (e.Shift)
modifier |= MainForm.KeyModifier.Shift;
if (e.KeyData.HasFlag(Keys.LWin) || e.KeyData.HasFlag(Keys.RWin))
modifier |= MainForm.KeyModifier.Win;
// 更新对应的修饰键组合框
if (textBox == txtTurnOffScreenKey)
{
cboTurnOffScreenModifier.SelectedIndex = (int)modifier;
TurnOffScreenModifier = modifier;
TurnOffScreenKey = (int)key;
}
// 更新文本框显示
textBox.Text = key.ToString();
}
}
private void btnOK_Click(object sender, EventArgs e)
{
// 验证并保存设置
if (ValidateAndSaveSettings())
{
DialogResult = DialogResult.OK;
Close();
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
private bool ValidateAndSaveSettings()
{
try
{
// 保存设置
EnableHotkeys = chkEnableHotkeys.Checked;
CloseScreenDelay = (int)numCloseScreenDelay.Value;
// 保存快捷键设置
TurnOffScreenModifier = (MainForm.KeyModifier)cboTurnOffScreenModifier.SelectedIndex;
return true;
}
catch (Exception ex)
{
MessageBox.Show("保存设置时出错:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
} }
}
// 自动生成的部分类,包含UI初始化代码
public partial class SettingsForm
{
private System.ComponentModel.IContainer components = null;
private GroupBox groupBox1;
private CheckBox chkEnableHotkeys;
private NumericUpDown numCloseScreenDelay;
private Label labelCloseScreenDelay;
private Label labelTurnOffScreenKey;
private ComboBox cboTurnOffScreenModifier;
private TextBox txtTurnOffScreenKey;
private Button btnOK;
private Button btnCancel;
private Label labelHotkeyHint;
private void chkEnableHotkeys_CheckedChanged(object sender, EventArgs e)
{
UpdateHotkeyControlsEnabled();
}
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.txtTurnOffScreenKey = new System.Windows.Forms.TextBox();
this.cboTurnOffScreenModifier = new System.Windows.Forms.ComboBox();
this.labelTurnOffScreenKey = new System.Windows.Forms.Label();
this.labelCloseScreenDelay = new System.Windows.Forms.Label();
this.numCloseScreenDelay = new System.Windows.Forms.NumericUpDown();
this.chkEnableHotkeys = new System.Windows.Forms.CheckBox();
this.btnOK = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button();
this.labelHotkeyHint = new System.Windows.Forms.Label();
this.groupBox1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.numCloseScreenDelay)).BeginInit();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.txtTurnOffScreenKey);
this.groupBox1.Controls.Add(this.cboTurnOffScreenModifier);
this.groupBox1.Controls.Add(this.labelTurnOffScreenKey);
this.groupBox1.Controls.Add(this.labelCloseScreenDelay);
this.groupBox1.Controls.Add(this.numCloseScreenDelay);
this.groupBox1.Controls.Add(this.chkEnableHotkeys);
this.groupBox1.Location = new System.Drawing.Point(12, 12);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(450, 150);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "屏幕控制设置";
//
// labelCloseScreenDelay
//
this.labelCloseScreenDelay.AutoSize = true;
this.labelCloseScreenDelay.Location = new System.Drawing.Point(27, 60);
this.labelCloseScreenDelay.Name = "labelCloseScreenDelay";
this.labelCloseScreenDelay.Size = new System.Drawing.Size(123, 15);
this.labelCloseScreenDelay.TabIndex = 2;
this.labelCloseScreenDelay.Text = "延迟关闭屏幕:(秒)";
//
// numCloseScreenDelay
//
this.numCloseScreenDelay.Location = new System.Drawing.Point(156, 58);
this.numCloseScreenDelay.Maximum = new decimal(new int[] {
60, 0, 0, 0});
this.numCloseScreenDelay.Minimum = new decimal(new int[] {
0, 0, 0, 0});
this.numCloseScreenDelay.Name = "numCloseScreenDelay";
this.numCloseScreenDelay.Size = new System.Drawing.Size(130, 21);
this.numCloseScreenDelay.TabIndex = 1;
this.numCloseScreenDelay.Value = new decimal(new int[] {
5, 0, 0, 0});
//
// chkEnableHotkeys
//
this.chkEnableHotkeys.AutoSize = true;
this.chkEnableHotkeys.Location = new System.Drawing.Point(30, 30);
this.chkEnableHotkeys.Name = "chkEnableHotkeys";
this.chkEnableHotkeys.Size = new System.Drawing.Size(240, 19);
this.chkEnableHotkeys.TabIndex = 0;
this.chkEnableHotkeys.Text = "启用全局快捷键监听(最小化时也可使用)";
this.chkEnableHotkeys.UseVisualStyleBackColor = true;
this.chkEnableHotkeys.CheckedChanged += new System.EventHandler(this.chkEnableHotkeys_CheckedChanged);
//
// labelTurnOffScreenKey
//
this.labelTurnOffScreenKey.AutoSize = true;
this.labelTurnOffScreenKey.Location = new System.Drawing.Point(27, 95);
this.labelTurnOffScreenKey.Name = "labelTurnOffScreenKey";
this.labelTurnOffScreenKey.Size = new System.Drawing.Size(105, 15);
this.labelTurnOffScreenKey.TabIndex = 3;
this.labelTurnOffScreenKey.Text = "关闭屏幕快捷键:";
//
// cboTurnOffScreenModifier
//
this.cboTurnOffScreenModifier.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cboTurnOffScreenModifier.FormattingEnabled = true;
this.cboTurnOffScreenModifier.Items.AddRange(new object[] {
"无",
"Alt",
"Ctrl",
"Ctrl+Alt",
"Shift",
"Alt+Shift",
"Ctrl+Shift",
"Ctrl+Alt+Shift",
"Win"});
this.cboTurnOffScreenModifier.Location = new System.Drawing.Point(156, 92);
this.cboTurnOffScreenModifier.Name = "cboTurnOffScreenModifier";
this.cboTurnOffScreenModifier.Size = new System.Drawing.Size(120, 23);
this.cboTurnOffScreenModifier.TabIndex = 5;
//
// txtTurnOffScreenKey
//
this.txtTurnOffScreenKey.Location = new System.Drawing.Point(282, 92);
this.txtTurnOffScreenKey.Name = "txtTurnOffScreenKey";
this.txtTurnOffScreenKey.Size = new System.Drawing.Size(100, 21);
this.txtTurnOffScreenKey.TabIndex = 7;
this.txtTurnOffScreenKey.Text = "D1";
//
// btnOK
//
this.btnOK.Location = new System.Drawing.Point(180, 210);
this.btnOK.Name = "btnOK";
this.btnOK.Size = new System.Drawing.Size(75, 30);
this.btnOK.TabIndex = 1;
this.btnOK.Text = "确定";
this.btnOK.UseVisualStyleBackColor = true;
this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
//
// btnCancel
//
this.btnCancel.Location = new System.Drawing.Point(270, 210);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(75, 30);
this.btnCancel.TabIndex = 2;
this.btnCancel.Text = "取消";
this.btnCancel.UseVisualStyleBackColor = true;
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
//
// labelHotkeyHint
//
this.labelHotkeyHint.AutoSize = true;
this.labelHotkeyHint.ForeColor = System.Drawing.Color.Gray;
this.labelHotkeyHint.Location = new System.Drawing.Point(30, 180);
this.labelHotkeyHint.Name = "labelHotkeyHint";
this.labelHotkeyHint.Size = new System.Drawing.Size(352, 15);
this.labelHotkeyHint.TabIndex = 3;
this.labelHotkeyHint.Text = "提示:点击快捷键输入框后直接按您想要设置的组合键即可";
//
// SettingsForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(480, 260);
this.Controls.Add(this.labelHotkeyHint);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.btnOK);
this.Controls.Add(this.groupBox1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "SettingsForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "屏幕控制设置";
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.numCloseScreenDelay)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
}
}