Repository navigation
Expand file tree
/
Copy pathVistaActiveTaskDialog.cs
More file actions
428 lines (396 loc) · 20.6 KB
/
Copy pathVistaActiveTaskDialog.cs
File metadata and controls
428 lines (396 loc) · 20.6 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
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
//------------------------------------------------------------------
// <summary>
// A P/Invoke wrapper for TaskDialog. Usability was given preference to perf and size.
// </summary>
//
// <remarks/>
//------------------------------------------------------------------
namespace PSTaskDialog
{
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics.CodeAnalysis;
/// <summary>
/// The active Task Dialog window. Provides several methods for acting on the active TaskDialog.
/// You should not use this object after the TaskDialog Destroy notification callback. Doing so
/// will result in undefined behavior and likely crash.
/// </summary>
public class VistaActiveTaskDialog : IWin32Window
{
/// <summary>
/// The Task Dialog's window handle.
/// </summary>
[SuppressMessage("Microsoft.Reliability", "CA2006:UseSafeHandleToEncapsulateNativeResources")] // We don't own the window.
private IntPtr handle;
/// <summary>
/// Creates a ActiveTaskDialog.
/// </summary>
/// <param name="handle">The Task Dialog's window handle.</param>
internal VistaActiveTaskDialog(IntPtr handle)
{
if (handle == IntPtr.Zero)
{
throw new ArgumentNullException("handle");
}
this.handle = handle;
}
/// <summary>
/// The Task Dialog's window handle.
/// </summary>
public IntPtr Handle
{
get { return this.handle; }
}
//// Not supported. Task Dialog Spec does not indicate what this is for.
////public void NavigatePage()
////{
//// // TDM_NAVIGATE_PAGE = WM_USER+101,
//// UnsafeNativeMethods.SendMessage(
//// this.windowHandle,
//// (uint)UnsafeNativeMethods.TASKDIALOG_MESSAGES.TDM_NAVIGATE_PAGE,
//// IntPtr.Zero,
//// //a UnsafeNativeMethods.TASKDIALOGCONFIG value);
////}
/// <summary>
/// Simulate the action of a button click in the TaskDialog. This can be a DialogResult value
/// or the ButtonID set on a TasDialogButton set on TaskDialog.Buttons.
/// </summary>
/// <param name="buttonId">Indicates the button ID to be selected.</param>
/// <returns>If the function succeeds the return value is true.</returns>
public bool ClickButton(int buttonId)
{
// TDM_CLICK_BUTTON = WM_USER+102, // wParam = Button ID
return VistaUnsafeNativeMethods.SendMessage(
this.handle,
(uint)VistaUnsafeNativeMethods.TASKDIALOG_MESSAGES.TDM_CLICK_BUTTON,
(IntPtr)buttonId,
IntPtr.Zero) != IntPtr.Zero;
}
/// <summary>
/// Used to indicate whether the hosted progress bar should be displayed in marquee mode or not.
/// </summary>
/// <param name="marquee">Specifies whether the progress bar sbould be shown in Marquee mode.
/// A value of true turns on Marquee mode.</param>
/// <returns>If the function succeeds the return value is true.</returns>
public bool SetMarqueeProgressBar(bool marquee)
{
// TDM_SET_MARQUEE_PROGRESS_BAR = WM_USER+103, // wParam = 0 (nonMarque) wParam != 0 (Marquee)
return VistaUnsafeNativeMethods.SendMessage(
this.handle,
(uint)VistaUnsafeNativeMethods.TASKDIALOG_MESSAGES.TDM_SET_MARQUEE_PROGRESS_BAR,
(marquee ? (IntPtr)1 : IntPtr.Zero),
IntPtr.Zero) != IntPtr.Zero;
// Future: get more detailed error from and throw.
}
/// <summary>
/// Sets the state of the progress bar.
/// </summary>
/// <param name="newState">The state to set the progress bar.</param>
/// <returns>If the function succeeds the return value is true.</returns>
public bool SetProgressBarState(VistaProgressBarState newState)
{
// TDM_SET_PROGRESS_BAR_STATE = WM_USER+104, // wParam = new progress state
return VistaUnsafeNativeMethods.SendMessage(
this.handle,
(uint)VistaUnsafeNativeMethods.TASKDIALOG_MESSAGES.TDM_SET_PROGRESS_BAR_STATE,
(IntPtr)newState,
IntPtr.Zero) != IntPtr.Zero;
// Future: get more detailed error from and throw.
}
/// <summary>
/// Set the minimum and maximum values for the hosted progress bar.
/// </summary>
/// <param name="minRange">Minimum range value. By default, the minimum value is zero.</param>
/// <param name="maxRange">Maximum range value. By default, the maximum value is 100.</param>
/// <returns>If the function succeeds the return value is true.</returns>
public bool SetProgressBarRange(Int16 minRange, Int16 maxRange)
{
// TDM_SET_PROGRESS_BAR_RANGE = WM_USER+105, // lParam = MAKELPARAM(nMinRange, nMaxRange)
// #define MAKELPARAM(l, h) ((LPARAM)(DWORD)MAKELONG(l, h))
// #define MAKELONG(a, b) ((LONG)(((WORD)(((DWORD_PTR)(a)) & 0xffff)) | ((DWORD)((WORD)(((DWORD_PTR)(b)) & 0xffff))) << 16))
IntPtr lparam = (IntPtr)((((Int32)minRange) & 0xffff) | ((((Int32)maxRange) & 0xffff) << 16));
return VistaUnsafeNativeMethods.SendMessage(
this.handle,
(uint)VistaUnsafeNativeMethods.TASKDIALOG_MESSAGES.TDM_SET_PROGRESS_BAR_RANGE,
IntPtr.Zero,
lparam) != IntPtr.Zero;
// Return value is actually prior range.
}
/// <summary>
/// Set the current position for a progress bar.
/// </summary>
/// <param name="newPosition">The new position.</param>
/// <returns>Returns the previous value if successful, or zero otherwise.</returns>
public int SetProgressBarPosition(int newPosition)
{
// TDM_SET_PROGRESS_BAR_POS = WM_USER+106, // wParam = new position
return (int)VistaUnsafeNativeMethods.SendMessage(
this.handle,
(uint)VistaUnsafeNativeMethods.TASKDIALOG_MESSAGES.TDM_SET_PROGRESS_BAR_POS,
(IntPtr)newPosition,
IntPtr.Zero);
}
/// <summary>
/// Sets the animation state of the Marquee Progress Bar.
/// </summary>
/// <param name="startMarquee">true starts the marquee animation and false stops it.</param>
/// <param name="speed">The time in milliseconds between refreshes.</param>
public void SetProgressBarMarquee(bool startMarquee, uint speed)
{
// TDM_SET_PROGRESS_BAR_MARQUEE = WM_USER+107, // wParam = 0 (stop marquee), wParam != 0 (start marquee), lparam = speed (milliseconds between repaints)
VistaUnsafeNativeMethods.SendMessage(
this.handle,
(uint)VistaUnsafeNativeMethods.TASKDIALOG_MESSAGES.TDM_SET_PROGRESS_BAR_MARQUEE,
(startMarquee ? new IntPtr(1) : IntPtr.Zero),
(IntPtr)speed);
}
/// <summary>
/// Updates the content text.
/// </summary>
/// <param name="content">The new value.</param>
/// <returns>If the function succeeds the return value is true.</returns>
public bool SetContent(string content)
{
// TDE_CONTENT,
// TDM_SET_ELEMENT_TEXT = WM_USER+108 // wParam = element (TASKDIALOG_ELEMENTS), lParam = new element text (LPCWSTR)
return VistaUnsafeNativeMethods.SendMessageWithString(
this.handle,
(uint)VistaUnsafeNativeMethods.TASKDIALOG_MESSAGES.TDM_SET_ELEMENT_TEXT,
(IntPtr)VistaUnsafeNativeMethods.TASKDIALOG_ELEMENTS.TDE_CONTENT,
content) != IntPtr.Zero;
}
/// <summary>
/// Updates the Expanded Information text.
/// </summary>
/// <param name="expandedInformation">The new value.</param>
/// <returns>If the function succeeds the return value is true.</returns>
public bool SetExpandedInformation(string expandedInformation)
{
// TDE_EXPANDED_INFORMATION,
// TDM_SET_ELEMENT_TEXT = WM_USER+108 // wParam = element (TASKDIALOG_ELEMENTS), lParam = new element text (LPCWSTR)
return VistaUnsafeNativeMethods.SendMessageWithString(
this.handle,
(uint)VistaUnsafeNativeMethods.TASKDIALOG_MESSAGES.TDM_SET_ELEMENT_TEXT,
(IntPtr)VistaUnsafeNativeMethods.TASKDIALOG_ELEMENTS.TDE_EXPANDED_INFORMATION,
expandedInformation) != IntPtr.Zero;
}
/// <summary>
/// Updates the Footer text.
/// </summary>
/// <param name="footer">The new value.</param>
/// <returns>If the function succeeds the return value is true.</returns>
public bool SetFooter(string footer)
{
// TDE_FOOTER,
// TDM_SET_ELEMENT_TEXT = WM_USER+108 // wParam = element (TASKDIALOG_ELEMENTS), lParam = new element text (LPCWSTR)
return VistaUnsafeNativeMethods.SendMessageWithString(
this.handle,
(uint)VistaUnsafeNativeMethods.TASKDIALOG_MESSAGES.TDM_SET_ELEMENT_TEXT,
(IntPtr)VistaUnsafeNativeMethods.TASKDIALOG_ELEMENTS.TDE_FOOTER,
footer) != IntPtr.Zero;
}
/// <summary>
/// Updates the Main Instruction.
/// </summary>
/// <param name="mainInstruction">The new value.</param>
/// <returns>If the function succeeds the return value is true.</returns>
public bool SetMainInstruction(string mainInstruction)
{
// TDE_MAIN_INSTRUCTION
// TDM_SET_ELEMENT_TEXT = WM_USER+108 // wParam = element (TASKDIALOG_ELEMENTS), lParam = new element text (LPCWSTR)
return VistaUnsafeNativeMethods.SendMessageWithString(
this.handle,
(uint)VistaUnsafeNativeMethods.TASKDIALOG_MESSAGES.TDM_SET_ELEMENT_TEXT,
(IntPtr)VistaUnsafeNativeMethods.TASKDIALOG_ELEMENTS.TDE_MAIN_INSTRUCTION,
mainInstruction) != IntPtr.Zero;
}
/// <summary>
/// Simulate the action of a radio button click in the TaskDialog.
/// The passed buttonID is the ButtonID set on a TaskDialogButton set on TaskDialog.RadioButtons.
/// </summary>
/// <param name="buttonId">Indicates the button ID to be selected.</param>
public void ClickRadioButton(int buttonId)
{
// TDM_CLICK_RADIO_BUTTON = WM_USER+110, // wParam = Radio Button ID
VistaUnsafeNativeMethods.SendMessage(
this.handle,
(uint)VistaUnsafeNativeMethods.TASKDIALOG_MESSAGES.TDM_CLICK_RADIO_BUTTON,
(IntPtr)buttonId,
IntPtr.Zero);
}
/// <summary>
/// Enable or disable a button in the TaskDialog.
/// The passed buttonID is the ButtonID set on a TaskDialogButton set on TaskDialog.Buttons
/// or a common button ID.
/// </summary>
/// <param name="buttonId">Indicates the button ID to be enabled or diabled.</param>
/// <param name="enable">Enambe the button if true. Disable the button if false.</param>
public void EnableButton(int buttonId, bool enable)
{
// TDM_ENABLE_BUTTON = WM_USER+111, // lParam = 0 (disable), lParam != 0 (enable), wParam = Button ID
VistaUnsafeNativeMethods.SendMessage(
this.handle,
(uint)VistaUnsafeNativeMethods.TASKDIALOG_MESSAGES.TDM_ENABLE_BUTTON,
(IntPtr)buttonId,
(IntPtr)(enable ? 0 : 1 ));
}
/// <summary>
/// Enable or disable a radio button in the TaskDialog.
/// The passed buttonID is the ButtonID set on a TaskDialogButton set on TaskDialog.RadioButtons.
/// </summary>
/// <param name="buttonId">Indicates the button ID to be enabled or diabled.</param>
/// <param name="enable">Enambe the button if true. Disable the button if false.</param>
public void EnableRadioButton(int buttonId, bool enable)
{
// TDM_ENABLE_RADIO_BUTTON = WM_USER+112, // lParam = 0 (disable), lParam != 0 (enable), wParam = Radio Button ID
VistaUnsafeNativeMethods.SendMessage(
this.handle,
(uint)VistaUnsafeNativeMethods.TASKDIALOG_MESSAGES.TDM_ENABLE_RADIO_BUTTON,
(IntPtr)buttonId,
(IntPtr)(enable ? 0 : 1));
}
/// <summary>
/// Check or uncheck the verification checkbox in the TaskDialog.
/// </summary>
/// <param name="checkedState">The checked state to set the verification checkbox.</param>
/// <param name="setKeyboardFocusToCheckBox">True to set the keyboard focus to the checkbox, and fasle otherwise.</param>
public void ClickVerification(bool checkedState, bool setKeyboardFocusToCheckBox)
{
// TDM_CLICK_VERIFICATION = WM_USER+113, // wParam = 0 (unchecked), 1 (checked), lParam = 1 (set key focus)
VistaUnsafeNativeMethods.SendMessage(
this.handle,
(uint)VistaUnsafeNativeMethods.TASKDIALOG_MESSAGES.TDM_CLICK_VERIFICATION,
(checkedState ? new IntPtr(1) : IntPtr.Zero),
(setKeyboardFocusToCheckBox ? new IntPtr(1) : IntPtr.Zero));
}
/// <summary>
/// Updates the content text.
/// </summary>
/// <param name="content">The new value.</param>
public void UpdateContent(string content)
{
// TDE_CONTENT,
// TDM_UPDATE_ELEMENT_TEXT = WM_USER+114, // wParam = element (TASKDIALOG_ELEMENTS), lParam = new element text (LPCWSTR)
VistaUnsafeNativeMethods.SendMessageWithString(
this.handle,
(uint)VistaUnsafeNativeMethods.TASKDIALOG_MESSAGES.TDM_UPDATE_ELEMENT_TEXT,
(IntPtr)VistaUnsafeNativeMethods.TASKDIALOG_ELEMENTS.TDE_CONTENT,
content);
}
/// <summary>
/// Updates the Expanded Information text. No effect if it was previously set to null.
/// </summary>
/// <param name="expandedInformation">The new value.</param>
public void UpdateExpandedInformation(string expandedInformation)
{
// TDE_EXPANDED_INFORMATION,
// TDM_UPDATE_ELEMENT_TEXT = WM_USER+114, // wParam = element (TASKDIALOG_ELEMENTS), lParam = new element text (LPCWSTR)
VistaUnsafeNativeMethods.SendMessageWithString(
this.handle,
(uint)VistaUnsafeNativeMethods.TASKDIALOG_MESSAGES.TDM_UPDATE_ELEMENT_TEXT,
(IntPtr)VistaUnsafeNativeMethods.TASKDIALOG_ELEMENTS.TDE_EXPANDED_INFORMATION,
expandedInformation);
}
/// <summary>
/// Updates the Footer text. No Effect if it was perviously set to null.
/// </summary>
/// <param name="footer">The new value.</param>
public void UpdateFooter(string footer)
{
// TDE_FOOTER,
// TDM_UPDATE_ELEMENT_TEXT = WM_USER+114, // wParam = element (TASKDIALOG_ELEMENTS), lParam = new element text (LPCWSTR)
VistaUnsafeNativeMethods.SendMessageWithString(
this.handle,
(uint)VistaUnsafeNativeMethods.TASKDIALOG_MESSAGES.TDM_UPDATE_ELEMENT_TEXT,
(IntPtr)VistaUnsafeNativeMethods.TASKDIALOG_ELEMENTS.TDE_FOOTER,
footer);
}
/// <summary>
/// Updates the Main Instruction.
/// </summary>
/// <param name="mainInstruction">The new value.</param>
public void UpdateMainInstruction(string mainInstruction)
{
// TDE_MAIN_INSTRUCTION
// TDM_UPDATE_ELEMENT_TEXT = WM_USER+114, // wParam = element (TASKDIALOG_ELEMENTS), lParam = new element text (LPCWSTR)
VistaUnsafeNativeMethods.SendMessageWithString(
this.handle,
(uint)VistaUnsafeNativeMethods.TASKDIALOG_MESSAGES.TDM_UPDATE_ELEMENT_TEXT,
(IntPtr)VistaUnsafeNativeMethods.TASKDIALOG_ELEMENTS.TDE_MAIN_INSTRUCTION,
mainInstruction);
}
/// <summary>
/// Designate whether a given Task Dialog button or command link should have a User Account Control (UAC) shield icon.
/// </summary>
/// <param name="buttonId">ID of the push button or command link to be updated.</param>
/// <param name="elevationRequired">False to designate that the action invoked by the button does not require elevation;
/// true to designate that the action does require elevation.</param>
public void SetButtonElevationRequiredState(int buttonId, bool elevationRequired)
{
// TDM_SET_BUTTON_ELEVATION_REQUIRED_STATE = WM_USER+115, // wParam = Button ID, lParam = 0 (elevation not required), lParam != 0 (elevation required)
VistaUnsafeNativeMethods.SendMessage(
this.handle,
(uint)VistaUnsafeNativeMethods.TASKDIALOG_MESSAGES.TDM_SET_BUTTON_ELEVATION_REQUIRED_STATE,
(IntPtr)buttonId,
(IntPtr)(elevationRequired ? new IntPtr(1) : IntPtr.Zero));
}
/// <summary>
/// Updates the main instruction icon. Note the type (standard via enum or
/// custom via Icon type) must be used when upating the icon.
/// </summary>
/// <param name="icon">Task Dialog standard icon.</param>
public void UpdateMainIcon(VistaTaskDialogIcon icon)
{
// TDM_UPDATE_ICON = WM_USER+116 // wParam = icon element (TASKDIALOG_ICON_ELEMENTS), lParam = new icon (hIcon if TDF_USE_HICON_* was set, PCWSTR otherwise)
VistaUnsafeNativeMethods.SendMessage(
this.handle,
(uint)VistaUnsafeNativeMethods.TASKDIALOG_MESSAGES.TDM_UPDATE_ICON,
(IntPtr)VistaUnsafeNativeMethods.TASKDIALOG_ICON_ELEMENTS.TDIE_ICON_MAIN,
(IntPtr)icon);
}
/// <summary>
/// Updates the main instruction icon. Note the type (standard via enum or
/// custom via Icon type) must be used when upating the icon.
/// </summary>
/// <param name="icon">The icon to set.</param>
public void UpdateMainIcon(Icon icon)
{
// TDM_UPDATE_ICON = WM_USER+116 // wParam = icon element (TASKDIALOG_ICON_ELEMENTS), lParam = new icon (hIcon if TDF_USE_HICON_* was set, PCWSTR otherwise)
VistaUnsafeNativeMethods.SendMessage(
this.handle,
(uint)VistaUnsafeNativeMethods.TASKDIALOG_MESSAGES.TDM_UPDATE_ICON,
(IntPtr)VistaUnsafeNativeMethods.TASKDIALOG_ICON_ELEMENTS.TDIE_ICON_MAIN,
(icon == null ? IntPtr.Zero : icon.Handle));
}
/// <summary>
/// Updates the footer icon. Note the type (standard via enum or
/// custom via Icon type) must be used when upating the icon.
/// </summary>
/// <param name="icon">Task Dialog standard icon.</param>
public void UpdateFooterIcon(VistaTaskDialogIcon icon)
{
// TDM_UPDATE_ICON = WM_USER+116 // wParam = icon element (TASKDIALOG_ICON_ELEMENTS), lParam = new icon (hIcon if TDF_USE_HICON_* was set, PCWSTR otherwise)
VistaUnsafeNativeMethods.SendMessage(
this.handle,
(uint)VistaUnsafeNativeMethods.TASKDIALOG_MESSAGES.TDM_UPDATE_ICON,
(IntPtr)VistaUnsafeNativeMethods.TASKDIALOG_ICON_ELEMENTS.TDIE_ICON_FOOTER,
(IntPtr)icon);
}
/// <summary>
/// Updates the footer icon. Note the type (standard via enum or
/// custom via Icon type) must be used when upating the icon.
/// </summary>
/// <param name="icon">The icon to set.</param>
public void UpdateFooterIcon(Icon icon)
{
// TDM_UPDATE_ICON = WM_USER+116 // wParam = icon element (TASKDIALOG_ICON_ELEMENTS), lParam = new icon (hIcon if TDF_USE_HICON_* was set, PCWSTR otherwise)
VistaUnsafeNativeMethods.SendMessage(
this.handle,
(uint)VistaUnsafeNativeMethods.TASKDIALOG_MESSAGES.TDM_UPDATE_ICON,
(IntPtr)VistaUnsafeNativeMethods.TASKDIALOG_ICON_ELEMENTS.TDIE_ICON_FOOTER,
(icon == null ? IntPtr.Zero : icon.Handle));
}
}
}