-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskCreateDialog.cs
More file actions
226 lines (203 loc) · 8.01 KB
/
Copy pathTaskCreateDialog.cs
File metadata and controls
226 lines (203 loc) · 8.01 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
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using TaskPlanSetter.Services;
namespace TaskPlanSetter
{
/// <summary>新建任务:名称 + 启动程序信息;触发器由服务层写入默认策略。</summary>
internal sealed class TaskCreateDialog : Form
{
private readonly TextBox _txtName;
private readonly TextBox _txtProgram;
private readonly TextBox _txtArgs;
private readonly TextBox _txtStartIn;
private readonly CheckBox _chkEnabled;
private TaskAdvancedOptions _advanced;
public string TaskNameInput => _txtName.Text.Trim();
public string ProgramPath => _txtProgram.Text.Trim();
public string Arguments => _txtArgs.Text;
public string WorkingDirectory => _txtStartIn.Text.Trim();
public bool TaskEnabled => _chkEnabled.Checked;
public TaskAdvancedOptions AdvancedOptions => _advanced;
public TaskCreateDialog()
{
Text = "新建任务计划";
FormBorderStyle = FormBorderStyle.FixedDialog;
StartPosition = FormStartPosition.CenterParent;
ShowInTaskbar = false;
MinimizeBox = false;
MaximizeBox = false;
ClientSize = new Size(640, 360);
BackColor = TechUi.PanelBg;
Font = new Font("Segoe UI", 9f);
_advanced = TaskAdvancedOptions.CreateDefault();
int y = 16;
const int labelW = 88;
const int left = 16;
const int gap = 34;
Controls.Add(MkCaption("新建任务(默认高级选项可通过“高级...”调整)", left, y, ClientSize.Width - 32));
y += 36;
Controls.Add(MkLabel("任务名称", left, y));
_txtName = MkText(left + labelW, y, ClientSize.Width - left - labelW - 16);
Controls.Add(_txtName);
y += gap;
Controls.Add(MkLabel("程序路径", left, y));
_txtProgram = MkText(left + labelW, y, ClientSize.Width - left - labelW - 124);
Controls.Add(_txtProgram);
var btnBrowseProgram = new Button
{
Text = "浏览...",
Location = new Point(ClientSize.Width - 108, y - 1),
Size = new Size(92, 26),
Cursor = Cursors.Hand
};
TechUi.StyleOutlinedButton(btnBrowseProgram);
btnBrowseProgram.Click += (_, __) => BrowseProgram(_txtProgram, _txtStartIn);
Controls.Add(btnBrowseProgram);
y += gap;
Controls.Add(MkLabel("参数", left, y));
_txtArgs = MkText(left + labelW, y, ClientSize.Width - left - labelW - 16);
Controls.Add(_txtArgs);
y += gap;
Controls.Add(MkLabel("起始于", left, y));
_txtStartIn = MkText(left + labelW, y, ClientSize.Width - left - labelW - 16);
Controls.Add(_txtStartIn);
y += gap + 2;
_chkEnabled = new CheckBox
{
Text = "创建后启用",
Location = new Point(left + labelW, y),
AutoSize = true,
Checked = true
};
TechUi.StyleCheckBox(_chkEnabled, TechUi.PanelBg);
Controls.Add(_chkEnabled);
var btnAdvanced = new Button
{
Text = "高级...",
Size = new Size(96, 30),
Location = new Point(16, ClientSize.Height - 44),
Anchor = AnchorStyles.Bottom | AnchorStyles.Left,
Cursor = Cursors.Hand
};
TechUi.StyleOutlinedButton(btnAdvanced);
btnAdvanced.Click += (_, __) =>
{
using (var dlg = new TaskAdvancedDialog(_advanced, isExistingTask: false))
{
if (dlg.ShowDialog(this) == DialogResult.OK)
{
_advanced = dlg.Result;
_chkEnabled.Checked = _advanced.Enabled;
}
}
};
var btnOk = new Button
{
Text = "创建",
DialogResult = DialogResult.None,
Size = new Size(96, 30),
Location = new Point(ClientSize.Width - 220, ClientSize.Height - 44),
Anchor = AnchorStyles.Bottom | AnchorStyles.Right,
Cursor = Cursors.Hand
};
TechUi.StylePrimaryButton(btnOk);
btnOk.Click += (_, __) =>
{
if (!TryValidate())
return;
_advanced.Enabled = _chkEnabled.Checked;
DialogResult = DialogResult.OK;
Close();
};
var btnCancel = new Button
{
Text = "取消",
DialogResult = DialogResult.Cancel,
Size = new Size(96, 30),
Location = new Point(ClientSize.Width - 108, ClientSize.Height - 44),
Anchor = AnchorStyles.Bottom | AnchorStyles.Right,
Cursor = Cursors.Hand
};
TechUi.StyleOutlinedButton(btnCancel);
Controls.Add(btnAdvanced);
Controls.Add(btnOk);
Controls.Add(btnCancel);
AcceptButton = btnOk;
CancelButton = btnCancel;
}
private bool TryValidate()
{
if (string.IsNullOrWhiteSpace(TaskNameInput))
{
TechDialog.ShowInfo(this, "校验", "请输入任务名称。");
_txtName.Focus();
return false;
}
if (TaskNameInput.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0 ||
TaskNameInput.Contains("\\") || TaskNameInput.Contains("/"))
{
TechDialog.ShowInfo(this, "校验", "任务名称不能包含路径分隔符或非法文件名字符。");
_txtName.Focus();
return false;
}
if (string.IsNullOrWhiteSpace(ProgramPath))
{
TechDialog.ShowInfo(this, "校验", "请输入要启动的程序路径。");
_txtProgram.Focus();
return false;
}
return true;
}
private static void BrowseProgram(TextBox programBox, TextBox startInBox)
{
using (var ofd = new OpenFileDialog())
{
ofd.Title = "选择要运行的程序";
ofd.Filter = "可执行文件|*.exe;*.bat;*.cmd;*.ps1|所有文件|*.*";
if (ofd.ShowDialog() != DialogResult.OK)
return;
programBox.Text = ofd.FileName;
if (string.IsNullOrWhiteSpace(startInBox.Text))
startInBox.Text = Path.GetDirectoryName(ofd.FileName) ?? string.Empty;
}
}
private static Label MkCaption(string text, int x, int y, int w)
{
return new Label
{
Text = text,
ForeColor = TechUi.Accent,
Font = new Font("Segoe UI", 9f, FontStyle.Bold),
Location = new Point(x, y),
Size = new Size(w, 36),
AutoSize = false
};
}
private static Label MkLabel(string text, int x, int y)
{
return new Label
{
Text = text,
ForeColor = TechUi.TextMuted,
Location = new Point(x, y + 3),
AutoSize = true
};
}
private static TextBox MkText(int x, int y, int width)
{
var t = new TextBox
{
BorderStyle = BorderStyle.FixedSingle,
BackColor = TechUi.ControlBg,
ForeColor = TechUi.TextMuted,
Font = new Font("Consolas", 9.75f),
Location = new Point(x, y),
Size = new Size(width, 23)
};
TechUi.StyleTextBox(t);
return t;
}
}
}