-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcTaskDialog.cs
455 lines (420 loc) · 20.8 KB
/
cTaskDialog.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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace PSTaskDialog
{
//--------------------------------------------------------------------------------
#region PUBLIC enums
//--------------------------------------------------------------------------------
public enum eSysIcons { Information, Question, Warning, Error };
public enum eTaskDialogButtons { YesNo, YesNoCancel, OKCancel, OK, Close, Cancel, None }
#endregion
//--------------------------------------------------------------------------------
public static class cTaskDialog
{
// PUBLIC static values...
static public bool VerificationChecked = false;
static public int RadioButtonResult = -1;
static public int CommandButtonResult = -1;
static public int EmulatedFormWidth = 450;
static public bool ForceEmulationMode = false;
static public bool UseToolWindowOnXP = true;
static public bool PlaySystemSounds = true;
static public EventHandler OnTaskDialogShown = null;
static public EventHandler OnTaskDialogClosed = null;
//--------------------------------------------------------------------------------
#region ShowTaskDialogBox
//--------------------------------------------------------------------------------
static public DialogResult ShowTaskDialogBox(IWin32Window Owner,
string Title,
string MainInstruction,
string Content,
string ExpandedInfo,
string Footer,
string VerificationText,
string RadioButtons,
string CommandButtons,
eTaskDialogButtons Buttons,
eSysIcons MainIcon,
eSysIcons FooterIcon,
int DefaultIndex)
{
DialogResult result;
if (OnTaskDialogShown != null)
OnTaskDialogShown(null, EventArgs.Empty);
if (VistaTaskDialog.IsAvailableOnThisOS && !ForceEmulationMode)
{
// [OPTION 1] Show Vista TaskDialog
VistaTaskDialog vtd = new VistaTaskDialog();
vtd.WindowTitle = Title;
vtd.MainInstruction = MainInstruction;
vtd.Content = Content;
vtd.ExpandedInformation = ExpandedInfo;
vtd.Footer = Footer;
// Radio Buttons
if (RadioButtons != "")
{
List<VistaTaskDialogButton> lst = new List<VistaTaskDialogButton>();
string[] arr = RadioButtons.Split(new char[] { '|' });
for (int i = 0; i < arr.Length; i++)
{
try
{
VistaTaskDialogButton button = new VistaTaskDialogButton();
button.ButtonId = 1000 + i;
button.ButtonText = arr[i];
lst.Add(button);
}
catch (FormatException)
{
}
}
vtd.RadioButtons = lst.ToArray();
vtd.NoDefaultRadioButton = (DefaultIndex == -1);
if (DefaultIndex >= 0)
vtd.DefaultRadioButton = DefaultIndex;
}
// Custom Buttons
if (CommandButtons != "")
{
List<VistaTaskDialogButton> lst = new List<VistaTaskDialogButton>();
string[] arr = CommandButtons.Split(new char[] { '|' });
for (int i = 0; i < arr.Length; i++)
{
try
{
VistaTaskDialogButton button = new VistaTaskDialogButton();
button.ButtonId = 2000 + i;
button.ButtonText = arr[i];
lst.Add(button);
}
catch (FormatException)
{
}
}
vtd.Buttons = lst.ToArray();
if (DefaultIndex >= 0)
vtd.DefaultButton = DefaultIndex;
}
switch (Buttons)
{
case eTaskDialogButtons.YesNo:
vtd.CommonButtons = VistaTaskDialogCommonButtons.Yes | VistaTaskDialogCommonButtons.No;
break;
case eTaskDialogButtons.YesNoCancel:
vtd.CommonButtons = VistaTaskDialogCommonButtons.Yes | VistaTaskDialogCommonButtons.No | VistaTaskDialogCommonButtons.Cancel;
break;
case eTaskDialogButtons.OKCancel:
vtd.CommonButtons = VistaTaskDialogCommonButtons.Ok | VistaTaskDialogCommonButtons.Cancel;
break;
case eTaskDialogButtons.OK:
vtd.CommonButtons = VistaTaskDialogCommonButtons.Ok;
break;
case eTaskDialogButtons.Close:
vtd.CommonButtons = VistaTaskDialogCommonButtons.Close;
break;
case eTaskDialogButtons.Cancel:
vtd.CommonButtons = VistaTaskDialogCommonButtons.Cancel;
break;
default:
vtd.CommonButtons = 0;
break;
}
switch (MainIcon)
{
case eSysIcons.Information: vtd.MainIcon = VistaTaskDialogIcon.Information; break;
case eSysIcons.Question: vtd.MainIcon = VistaTaskDialogIcon.Information; break;
case eSysIcons.Warning: vtd.MainIcon = VistaTaskDialogIcon.Warning; break;
case eSysIcons.Error: vtd.MainIcon = VistaTaskDialogIcon.Error; break;
}
switch (FooterIcon)
{
case eSysIcons.Information: vtd.FooterIcon = VistaTaskDialogIcon.Information; break;
case eSysIcons.Question: vtd.FooterIcon = VistaTaskDialogIcon.Information; break;
case eSysIcons.Warning: vtd.FooterIcon = VistaTaskDialogIcon.Warning; break;
case eSysIcons.Error: vtd.FooterIcon = VistaTaskDialogIcon.Error; break;
}
vtd.EnableHyperlinks = false;
vtd.ShowProgressBar = false;
vtd.AllowDialogCancellation = (Buttons == eTaskDialogButtons.Cancel ||
Buttons == eTaskDialogButtons.Close ||
Buttons == eTaskDialogButtons.OKCancel ||
Buttons == eTaskDialogButtons.YesNoCancel);
vtd.CallbackTimer = false;
vtd.ExpandedByDefault = false;
vtd.ExpandFooterArea = false;
vtd.PositionRelativeToWindow = true;
vtd.RightToLeftLayout = false;
vtd.NoDefaultRadioButton = false;
vtd.CanBeMinimized = false;
vtd.ShowMarqueeProgressBar = false;
vtd.UseCommandLinks = (CommandButtons != "");
vtd.UseCommandLinksNoIcon = false;
vtd.VerificationText = VerificationText;
vtd.VerificationFlagChecked = false;
vtd.ExpandedControlText = "Hide details";
vtd.CollapsedControlText = "Show details";
vtd.Callback = null;
// Show the Dialog
result = (DialogResult)vtd.Show((vtd.CanBeMinimized ? null : Owner), out VerificationChecked, out RadioButtonResult);
// if a command button was clicked, then change return result
// to "DialogResult.OK" and set the CommandButtonResult
if ((int)result >= 2000)
{
CommandButtonResult = ((int)result - 2000);
result = DialogResult.OK;
}
if (RadioButtonResult >= 1000)
RadioButtonResult -= 1000; // deduct the ButtonID start value for radio buttons
}
else
{
// [OPTION 2] Show Emulated Form
using (frmTaskDialog td = new frmTaskDialog())
{
td.Title = Title;
td.MainInstruction = MainInstruction;
td.Content = Content;
td.ExpandedInfo = ExpandedInfo;
td.Footer = Footer;
td.RadioButtons = RadioButtons;
td.CommandButtons = CommandButtons;
td.Buttons = Buttons;
td.MainIcon = MainIcon;
td.FooterIcon = FooterIcon;
td.VerificationText = VerificationText;
td.Width = EmulatedFormWidth;
td.DefaultButtonIndex = DefaultIndex;
td.BuildForm();
result = td.ShowDialog(Owner);
RadioButtonResult = td.RadioButtonIndex;
CommandButtonResult = td.CommandButtonClickedIndex;
VerificationChecked = td.VerificationCheckBoxChecked;
}
}
if (OnTaskDialogClosed != null)
OnTaskDialogClosed(null, EventArgs.Empty);
return result;
}
//--------------------------------------------------------------------------------
// Overloaded versions...
//--------------------------------------------------------------------------------
static public DialogResult ShowTaskDialogBox(IWin32Window Owner,
string Title,
string MainInstruction,
string Content,
string ExpandedInfo,
string Footer,
string VerificationText,
string RadioButtons,
string CommandButtons,
eTaskDialogButtons Buttons,
eSysIcons MainIcon,
eSysIcons FooterIcon)
{
return ShowTaskDialogBox(Owner, Title, MainInstruction, Content, ExpandedInfo, Footer, VerificationText, RadioButtons, CommandButtons, Buttons, MainIcon, FooterIcon, 0);
}
static public DialogResult ShowTaskDialogBox(string Title,
string MainInstruction,
string Content,
string ExpandedInfo,
string Footer,
string VerificationText,
string RadioButtons,
string CommandButtons,
eTaskDialogButtons Buttons,
eSysIcons MainIcon,
eSysIcons FooterIcon)
{
return ShowTaskDialogBox(null, Title, MainInstruction, Content, ExpandedInfo, Footer, VerificationText, RadioButtons, CommandButtons, Buttons, MainIcon, FooterIcon, 0);
}
#endregion
//--------------------------------------------------------------------------------
#region MessageBox
//--------------------------------------------------------------------------------
static public DialogResult MessageBox(IWin32Window Owner,
string Title,
string MainInstruction,
string Content,
string ExpandedInfo,
string Footer,
string VerificationText,
eTaskDialogButtons Buttons,
eSysIcons MainIcon,
eSysIcons FooterIcon)
{
return ShowTaskDialogBox(Owner, Title, MainInstruction, Content, ExpandedInfo, Footer, VerificationText, "", "", Buttons, MainIcon, FooterIcon);
}
//--------------------------------------------------------------------------------
// Overloaded versions...
//--------------------------------------------------------------------------------
static public DialogResult MessageBox(string Title,
string MainInstruction,
string Content,
string ExpandedInfo,
string Footer,
string VerificationText,
eTaskDialogButtons Buttons,
eSysIcons MainIcon,
eSysIcons FooterIcon)
{
return ShowTaskDialogBox(null, Title, MainInstruction, Content, ExpandedInfo, Footer, VerificationText, "", "", Buttons, MainIcon, FooterIcon);
}
static public DialogResult MessageBox(IWin32Window Owner,
string Title,
string MainInstruction,
string Content,
eTaskDialogButtons Buttons,
eSysIcons MainIcon)
{
return MessageBox(Owner, Title, MainInstruction, Content, "", "", "", Buttons, MainIcon, eSysIcons.Information);
}
static public DialogResult MessageBox(string Title,
string MainInstruction,
string Content,
eTaskDialogButtons Buttons,
eSysIcons MainIcon)
{
return MessageBox(null, Title, MainInstruction, Content, "", "", "", Buttons, MainIcon, eSysIcons.Information);
}
//--------------------------------------------------------------------------------
#endregion
//--------------------------------------------------------------------------------
#region ShowRadioBox
//--------------------------------------------------------------------------------
static public int ShowRadioBox(IWin32Window Owner,
string Title,
string MainInstruction,
string Content,
string ExpandedInfo,
string Footer,
string VerificationText,
string RadioButtons,
eSysIcons MainIcon,
eSysIcons FooterIcon,
int DefaultIndex)
{
DialogResult res = ShowTaskDialogBox(Owner, Title, MainInstruction, Content, ExpandedInfo, Footer, VerificationText,
RadioButtons, "", eTaskDialogButtons.OKCancel, MainIcon, FooterIcon, DefaultIndex);
if (res == DialogResult.OK)
return RadioButtonResult;
else
return -1;
}
//--------------------------------------------------------------------------------
// Overloaded versions...
//--------------------------------------------------------------------------------
static public int ShowRadioBox(string Title,
string MainInstruction,
string Content,
string ExpandedInfo,
string Footer,
string VerificationText,
string RadioButtons,
eSysIcons MainIcon,
eSysIcons FooterIcon,
int DefaultIndex)
{
DialogResult res = ShowTaskDialogBox(null, Title, MainInstruction, Content, ExpandedInfo, Footer, VerificationText,
RadioButtons, "", eTaskDialogButtons.OKCancel, MainIcon, FooterIcon, DefaultIndex);
if (res == DialogResult.OK)
return RadioButtonResult;
else
return -1;
}
static public int ShowRadioBox(IWin32Window Owner,
string Title,
string MainInstruction,
string Content,
string ExpandedInfo,
string Footer,
string VerificationText,
string RadioButtons,
eSysIcons MainIcon,
eSysIcons FooterIcon)
{
return ShowRadioBox(Owner, Title, MainInstruction, Content, ExpandedInfo, Footer, VerificationText, RadioButtons, eSysIcons.Question, eSysIcons.Information, 0);
}
static public int ShowRadioBox(IWin32Window Owner,
string Title,
string MainInstruction,
string Content,
string RadioButtons,
int DefaultIndex)
{
return ShowRadioBox(Owner, Title, MainInstruction, Content, "", "", "", RadioButtons, eSysIcons.Question, eSysIcons.Information, DefaultIndex);
}
static public int ShowRadioBox(IWin32Window Owner,
string Title,
string MainInstruction,
string Content,
string RadioButtons)
{
return ShowRadioBox(Owner, Title, MainInstruction, Content, "", "", "", RadioButtons, eSysIcons.Question, eSysIcons.Information, 0);
}
static public int ShowRadioBox(string Title,
string MainInstruction,
string Content,
string RadioButtons)
{
return ShowRadioBox(null, Title, MainInstruction, Content, "", "", "", RadioButtons, eSysIcons.Question, eSysIcons.Information, 0);
}
#endregion
//--------------------------------------------------------------------------------
#region ShowCommandBox
//--------------------------------------------------------------------------------
static public int ShowCommandBox(IWin32Window Owner,
string Title,
string MainInstruction,
string Content,
string ExpandedInfo,
string Footer,
string VerificationText,
string CommandButtons,
bool ShowCancelButton,
eSysIcons MainIcon,
eSysIcons FooterIcon)
{
DialogResult res = ShowTaskDialogBox(Owner, Title, MainInstruction, Content, ExpandedInfo, Footer, VerificationText,
"", CommandButtons, (ShowCancelButton ? eTaskDialogButtons.Cancel : eTaskDialogButtons.None),
MainIcon, FooterIcon);
if (res == DialogResult.OK)
return CommandButtonResult;
else
return -1;
}
//--------------------------------------------------------------------------------
// Overloaded versions...
//--------------------------------------------------------------------------------
static public int ShowCommandBox(string Title,
string MainInstruction,
string Content,
string ExpandedInfo,
string Footer,
string VerificationText,
string CommandButtons,
bool ShowCancelButton,
eSysIcons MainIcon,
eSysIcons FooterIcon)
{
DialogResult res = ShowTaskDialogBox(null, Title, MainInstruction, Content, ExpandedInfo, Footer, VerificationText,
"", CommandButtons, (ShowCancelButton ? eTaskDialogButtons.Cancel : eTaskDialogButtons.None),
MainIcon, FooterIcon);
if (res == DialogResult.OK)
return CommandButtonResult;
else
return -1;
}
static public int ShowCommandBox(IWin32Window Owner, string Title, string MainInstruction, string Content, string CommandButtons, bool ShowCancelButton)
{
return ShowCommandBox(Owner, Title, MainInstruction, Content, "", "", "", CommandButtons, ShowCancelButton, eSysIcons.Question, eSysIcons.Information);
}
static public int ShowCommandBox(string Title, string MainInstruction, string Content, string CommandButtons, bool ShowCancelButton)
{
return ShowCommandBox(null, Title, MainInstruction, Content, "", "", "", CommandButtons, ShowCancelButton, eSysIcons.Question, eSysIcons.Information);
}
#endregion
//--------------------------------------------------------------------------------
}
}