-
Notifications
You must be signed in to change notification settings - Fork 35
/
NtUiLib.TaskDialog.pas
345 lines (291 loc) · 10 KB
/
NtUiLib.TaskDialog.pas
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
unit NtUiLib.TaskDialog;
{
This module provides support for showing Task Dialogs or Message Boxes.
NOTE: some features require a manifest with enabled runtime themes.
}
interface
uses
Ntapi.WinUser, DelphiApi.Reflection, NtUtils;
const
IDNONE = TMessageResponse.IDNONE;
IDOK = TMessageResponse.IDOK;
IDCANCEL = TMessageResponse.IDCANCEL;
IDABORT = TMessageResponse.IDABORT;
IDRETRY = TMessageResponse.IDRETRY;
IDIGNORE = TMessageResponse.IDIGNORE;
IDYES = TMessageResponse.IDYES;
IDNO = TMessageResponse.IDNO;
IDTIMEOUT = TMessageResponse.IDTIMEOUT;
type
[NamingStyle(nsCamelCase, 'di')]
TDialogIcon = (
diNone,
diError,
diWarning,
diInfo,
diShield,
diConfirmation,
diApplication
);
[NamingStyle(nsCamelCase, 'db')]
TDialogButtons = (
dbOk,
dbOkCancel,
dbYesNo,
dbYesNoCancel,
dbRetryCancel,
dbAbortRetryIgnore,
dbYesIgnore,
dbYesAbortIgnore
);
// Show a Task Dialog or fallback to a Message Box and check for errors
function UsrxShowTaskDialogWithStatus(
out Response: TMessageResponse;
[opt] OwnerWindow: THwnd;
const Title: String;
const MainInstruction: String;
const Content: String;
Icon: TDialogIcon = diInfo;
Buttons: TDialogButtons = dbOk;
DefaultButton: TMessageResponse = IDNONE
): TNtxStatus;
// Show a Task Dialog or fallback to a Message Box
function UsrxShowTaskDialog(
[opt] OwnerWindow: THwnd;
const Title: String;
const MainInstruction: String;
const Content: String;
Icon: TDialogIcon = diInfo;
Buttons: TDialogButtons = dbOk;
DefaultButton: TMessageResponse = IDNONE
): TMessageResponse;
// Show a cross-session message to the interactive user and check for errors
function WsxShowMessageInteractiveWithStatus(
out Response: TMessageResponse;
const Title: String;
const Content: String;
Icon: TDialogIcon = diInfo;
Buttons: TDialogButtons = dbOk;
TimeoutSeconds: Cardinal = 0
): TNtxStatus;
// Show a cross-session message to the interactive user
function WsxShowMessageInteractive(
const Title: String;
const Content: String;
Icon: TDialogIcon = diInfo;
Buttons: TDialogButtons = dbOk;
TimeoutSeconds: Cardinal = 0
): TMessageResponse;
// Show the best available dialog to the interactive user and check for errors
function UsrxShowMessageAlwaysInteractiveWithStatus(
out Response: TMessageResponse;
const Title: String;
const MainInstruction: String;
const Content: String;
Icon: TDialogIcon = diInfo;
Buttons: TDialogButtons = dbOk;
DefaultButton: TMessageResponse = IDNONE;
TimeoutSeconds: Cardinal = 0
): TNtxStatus;
// Show the best available dialog to the interactive user
function UsrxShowMessageAlwaysInteractive(
const Title: String;
const MainInstruction: String;
const Content: String;
Icon: TDialogIcon = diInfo;
Buttons: TDialogButtons = dbOk;
DefaultButton: TMessageResponse = IDNONE;
TimeoutSeconds: Cardinal = 0
): TMessageResponse;
implementation
uses
Ntapi.WinNt, Ntapi.winsta, Ntapi.CommCtrls, NtUtils.Ldr,
NtUtils.Errors, NtUtils.WinStation, NtUtils.WinUser;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
function UsrxpMakeMessageStyle(
Icon: TDialogIcon;
Buttons: TDialogButtons
): TMessageStyle;
begin
Result := MB_TASKMODAL;
case Icon of
diError: Result := Result or MB_ICONERROR;
diWarning: Result := Result or MB_ICONWARNING;
diInfo: Result := Result or MB_ICONINFORMATION;
diConfirmation : Result := Result or MB_ICONINFORMATION;
end;
// Use the logically closest collection of buttons from the available options
case Buttons of
dbOk: Result := Result or MB_OK;
dbOkCancel: Result := Result or MB_OKCANCEL;
dbYesNo: Result := Result or MB_YESNO;
dbYesNoCancel: Result := Result or MB_YESNOCANCEL;
dbRetryCancel: Result := Result or MB_RETRYCANCEL;
dbAbortRetryIgnore: Result := Result or MB_ABORTRETRYIGNORE;
dbYesIgnore: Result := Result or MB_OKCANCEL;
dbYesAbortIgnore: Result := Result or MB_YESNOCANCEL;
end;
end;
procedure UsrxpAdjustResponse(
var Response: TMessageResponse;
Buttons: TDialogButtons
);
begin
case Buttons of
dbYesIgnore:
case Response of
IDOK: Response := IDYES;
IDCANCEL: Response := IDIGNORE;
end;
dbYesAbortIgnore:
case Response of
IDNO: Response := IDABORT;
IDCANCEL: Response := IDIGNORE;
end;
end;
end;
function UsrxShowTaskDialogWithStatus;
var
DlgConfig: TTaskDialogConfig;
CustomButtons: array [0..2] of TTaskDialogButton;
begin
// Task Dialog might not be available due to missing manifest or low level of
// privileges. Use it when available; otherwise, fall back to a Message Box.
if LdrxCheckDelayedImport(delayed_TaskDialogIndirect).IsSuccess then
begin
DlgConfig := Default(TTaskDialogConfig);
DlgConfig.cbSize := SizeOf(DlgConfig);
DlgConfig.Flags := TDF_ALLOW_DIALOG_CANCELLATION;
DlgConfig.Owner := OwnerWindow;
DlgConfig.WindowTitle := PWideChar(Title);
DlgConfig.MainInstruction := PWideChar(MainInstruction);
DlgConfig.Content := PWideChar(Content);
DlgConfig.nDefaultButton := DefaultButton;
DlgConfig.cButtons := 0;
case Buttons of
dbOk:
DlgConfig.CommonButtons := TDCBF_OK_BUTTON;
dbOkCancel:
DlgConfig.CommonButtons := TDCBF_OK_BUTTON or TDCBF_CANCEL_BUTTON;
dbYesNo:
DlgConfig.CommonButtons := TDCBF_YES_BUTTON or TDCBF_NO_BUTTON;
dbYesNoCancel:
DlgConfig.CommonButtons := TDCBF_YES_BUTTON or TDCBF_NO_BUTTON or
TDCBF_CANCEL_BUTTON;
dbRetryCancel:
DlgConfig.CommonButtons := TDCBF_RETRY_BUTTON or TDCBF_CANCEL_BUTTON;
dbAbortRetryIgnore:
begin
CustomButtons[DlgConfig.cButtons].ButtonID := IDABORT;
CustomButtons[DlgConfig.cButtons].ButtonText := '&Abort';
Inc(DlgConfig.cButtons);
CustomButtons[DlgConfig.cButtons].ButtonID := IDRETRY;
CustomButtons[DlgConfig.cButtons].ButtonText := '&Retry';
Inc(DlgConfig.cButtons);
CustomButtons[DlgConfig.cButtons].ButtonID := IDIGNORE;
CustomButtons[DlgConfig.cButtons].ButtonText := '&Ignore';
Inc(DlgConfig.cButtons);
end;
dbYesIgnore:
begin
CustomButtons[DlgConfig.cButtons].ButtonID := IDYES;
CustomButtons[DlgConfig.cButtons].ButtonText := '&Yes';
Inc(DlgConfig.cButtons);
CustomButtons[DlgConfig.cButtons].ButtonID := IDIGNORE;
CustomButtons[DlgConfig.cButtons].ButtonText := '&Ignore';
Inc(DlgConfig.cButtons);
end;
dbYesAbortIgnore:
begin
CustomButtons[DlgConfig.cButtons].ButtonID := IDYES;
CustomButtons[DlgConfig.cButtons].ButtonText := '&Yes';
Inc(DlgConfig.cButtons);
CustomButtons[DlgConfig.cButtons].ButtonID := IDABORT;
CustomButtons[DlgConfig.cButtons].ButtonText := '&Abort';
Inc(DlgConfig.cButtons);
CustomButtons[DlgConfig.cButtons].ButtonID := IDIGNORE;
CustomButtons[DlgConfig.cButtons].ButtonText := '&Ignore';
Inc(DlgConfig.cButtons);
end;
end;
if DlgConfig.cButtons > 0 then
DlgConfig.Buttons := Pointer(@CustomButtons[0]);
case Icon of
diError: DlgConfig.MainIcon.pszIcon := TD_ERROR_ICON;
diWarning: DlgConfig.MainIcon.pszIcon := TD_WARNING_ICON;
diInfo: DlgConfig.MainIcon.pszIcon := TD_INFORMATION_ICON;
diShield: DlgConfig.MainIcon.pszIcon := TD_SHIELD_ICON;
diConfirmation: DlgConfig.MainIcon.pszIcon := IDI_QUESTION;
diApplication: DlgConfig.MainIcon.pszIcon := IDI_APPLICATION;
end;
// Show the task dialog
if TaskDialogIndirect(DlgConfig, @Response, nil, nil).IsSuccess then
Exit;
end;
// Cannot display the task dialog; load the message box
Result := LdrxCheckDelayedImport(delayed_MessageBoxW);
if not Result.IsSuccess then
Exit;
// Show the message box
Result.Location := 'MessageBoxW';
Response := MessageBoxW(OwnerWindow, PWideChar(MainInstruction + #$D#$A#$D#$A
+ Content), PWideChar(Title), UsrxpMakeMessageStyle(Icon, Buttons));
Result.Win32Result := Response <> IDNONE;
if not Result.IsSuccess then
Exit;
// Adjust the response for replaced buttons
UsrxpAdjustResponse(Response, Buttons);
end;
function UsrxShowTaskDialog;
begin
if not UsrxShowTaskDialogWithStatus(Result, OwnerWindow, Title,
MainInstruction, Content, Icon, Buttons, DefaultButton).IsSuccess then
Result := IDNONE;
end;
function WsxShowMessageInteractiveWithStatus;
var
InteractiveSession: TSessionId;
begin
Result := WsxFindActiveSessionId(InteractiveSession);
if not Result.IsSuccess then
Exit;
Result := WsxSendMessage(InteractiveSession, Title, Content,
UsrxpMakeMessageStyle(Icon, Buttons), TimeoutSeconds, True, @Response);
if not Result.IsSuccess then
Exit;
UsrxpAdjustResponse(Response, Buttons);
end;
function WsxShowMessageInteractive;
begin
if not WsxShowMessageInteractiveWithStatus(Result, Title, Content, Icon,
Buttons, TimeoutSeconds).IsSuccess then
Result := IDNONE;
end;
function UsrxShowMessageAlwaysInteractiveWithStatus;
var
IsInteractive: LongBool;
WindowModeReverter: IAutoReleasable;
begin
if UsrxObject.Query(UsrxCurrentDesktop, UOI_IO, IsInteractive).IsSuccess and
IsInteractive then
begin
// Make sure the window will be drawn as visible
WindowModeReverter := UsrxOverridePebWindowMode(TShowMode32.SW_SHOW_NORMAL);
// Show the message on the current desktop
Result := UsrxShowTaskDialogWithStatus(Response, 0, Title, MainInstruction,
Content, Icon, Buttons, DefaultButton)
end
else
// Ask CSRSS to show the message in the interactive session
Result := WsxShowMessageInteractiveWithStatus(Response, Title,
MainInstruction + #$D#$A#$D#$A + Content, Icon, Buttons, TimeoutSeconds);
end;
function UsrxShowMessageAlwaysInteractive;
begin
if not UsrxShowMessageAlwaysInteractiveWithStatus(Result, Title, MainInstruction,
Content, Icon, Buttons, DefaultButton, TimeoutSeconds).IsSuccess then
Result := IDNONE;
end;
end.