Repository navigation
Expand file tree
/
Copy pathMessageBox.cs
More file actions
494 lines (444 loc) · 16.8 KB
/
Copy pathMessageBox.cs
File metadata and controls
494 lines (444 loc) · 16.8 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
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.ComponentModel;
using System.Runtime.InteropServices;
namespace System.Windows.Forms;
/// <summary>
/// Displays a message box that can contain text, buttons, and symbols that inform and instruct the user.
/// </summary>
public class MessageBox
{
[ThreadStatic]
private static HelpInfo[]? t_helpInfoTable;
// This is meant to be a static class, but predates that feature.
private MessageBox()
{
}
internal static HelpInfo? HelpInfo
{
get
{
// Unfortunately, there's no easy way to obtain handle of a message box.
// We'll have to rely on the fact that modal message loops have to pop off in an orderly way.
if (t_helpInfoTable is not null && t_helpInfoTable.Length > 0)
{
// The top of the stack is actually at the end of the array.
return t_helpInfoTable[^1];
}
return null;
}
}
private static MESSAGEBOX_STYLE GetMessageBoxStyle(
IWin32Window? owner,
MessageBoxButtons buttons,
MessageBoxIcon icon,
MessageBoxDefaultButton defaultButton,
MessageBoxOptions options,
bool showHelp)
{
SourceGenerated.EnumValidator.Validate(buttons, nameof(buttons));
SourceGenerated.EnumValidator.Validate(icon, nameof(icon));
SourceGenerated.EnumValidator.Validate(defaultButton, nameof(defaultButton));
// options intentionally not verified because we don't expose all the options Win32 supports.
if (!SystemInformation.UserInteractive && (options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) == 0)
{
throw new InvalidOperationException(SR.CantShowModalOnNonInteractive);
}
if (owner is not null && (options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) != 0)
{
throw new ArgumentException(SR.CantShowMBServiceWithOwner, nameof(options));
}
if (showHelp && (options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) != 0)
{
throw new ArgumentException(SR.CantShowMBServiceWithHelp, nameof(options));
}
MESSAGEBOX_STYLE style = (showHelp) ? MESSAGEBOX_STYLE.MB_HELP : 0;
style |= (MESSAGEBOX_STYLE)buttons | (MESSAGEBOX_STYLE)icon | (MESSAGEBOX_STYLE)defaultButton | (MESSAGEBOX_STYLE)options;
return style;
}
private static void PopHelpInfo()
{
// we roll our own stack here because we want a pretty lightweight implementation.
// usually there's only going to be one message box shown at a time. But if
// someone shows two message boxes (say by launching them via a WM_TIMER message)
// we've got to gracefully handle the current help info.
if (t_helpInfoTable is null)
{
Debug.Fail("Why are we being called when there's nothing to pop?");
}
else
{
if (t_helpInfoTable.Length == 1)
{
t_helpInfoTable = null;
}
else
{
int newCount = t_helpInfoTable.Length - 1;
HelpInfo[] newTable = new HelpInfo[newCount];
Array.Copy(t_helpInfoTable, newTable, newCount);
t_helpInfoTable = newTable;
}
}
}
private static void PushHelpInfo(HelpInfo hpi)
{
// we roll our own stack here because we want a pretty lightweight implementation.
// usually there's only going to be one message box shown at a time. But if
// someone shows two message boxes (say by launching them via a WM_TIMER message)
// we've got to gracefully handle the current help info.
int lastCount = 0;
HelpInfo[] newTable;
if (t_helpInfoTable is null)
{
newTable = new HelpInfo[lastCount + 1];
}
else
{
// if we already have a table - allocate a new slot
lastCount = t_helpInfoTable.Length;
newTable = new HelpInfo[lastCount + 1];
Array.Copy(t_helpInfoTable, newTable, lastCount);
}
newTable[lastCount] = hpi;
t_helpInfoTable = newTable;
}
/// <summary>
/// Displays a message box with specified text, caption, and style with Help Button.
/// </summary>
public static DialogResult Show(
string? text,
string? caption,
MessageBoxButtons buttons,
MessageBoxIcon icon,
MessageBoxDefaultButton defaultButton,
MessageBoxOptions options,
bool displayHelpButton)
{
return ShowCore(null, text, caption, buttons, icon, defaultButton, options, displayHelpButton);
}
/// <summary>
/// Displays a message box with specified text, caption, style and Help file Path .
/// </summary>
public static DialogResult Show(
string? text,
string? caption,
MessageBoxButtons buttons,
MessageBoxIcon icon,
MessageBoxDefaultButton defaultButton,
MessageBoxOptions options,
string helpFilePath)
{
HelpInfo hpi = new(helpFilePath);
return ShowCore(null, text, caption, buttons, icon, defaultButton, options, hpi);
}
/// <summary>
/// Displays a message box with specified text, caption, style and Help file Path for a IWin32Window.
/// </summary>
public static DialogResult Show(
IWin32Window? owner,
string? text,
string? caption,
MessageBoxButtons buttons,
MessageBoxIcon icon,
MessageBoxDefaultButton defaultButton,
MessageBoxOptions options,
string helpFilePath)
{
HelpInfo hpi = new(helpFilePath);
return ShowCore(owner, text, caption, buttons, icon, defaultButton, options, hpi);
}
/// <summary>
/// Displays a message box with specified text, caption, style, Help file Path and keyword.
/// </summary>
public static DialogResult Show(
string? text,
string? caption,
MessageBoxButtons buttons,
MessageBoxIcon icon,
MessageBoxDefaultButton defaultButton,
MessageBoxOptions options,
string helpFilePath,
string keyword)
{
HelpInfo hpi = new(helpFilePath, keyword);
return ShowCore(null, text, caption, buttons, icon, defaultButton, options, hpi);
}
/// <summary>
/// Displays a message box with specified text, caption, style, Help file Path and keyword for a IWin32Window.
/// </summary>
public static DialogResult Show(
IWin32Window? owner,
string? text,
string? caption,
MessageBoxButtons buttons,
MessageBoxIcon icon,
MessageBoxDefaultButton defaultButton,
MessageBoxOptions options,
string helpFilePath,
string keyword)
{
HelpInfo hpi = new(helpFilePath, keyword);
return ShowCore(owner, text, caption, buttons, icon, defaultButton, options, hpi);
}
/// <summary>
/// Displays a message box with specified text, caption, style, Help file Path and HelpNavigator.
/// </summary>
public static DialogResult Show(
string? text,
string? caption,
MessageBoxButtons buttons,
MessageBoxIcon icon,
MessageBoxDefaultButton defaultButton,
MessageBoxOptions options,
string helpFilePath,
HelpNavigator navigator)
{
HelpInfo hpi = new(helpFilePath, navigator);
return ShowCore(null, text, caption, buttons, icon, defaultButton, options, hpi);
}
/// <summary>
/// Displays a message box with specified text, caption, style, Help file Path and HelpNavigator for IWin32Window.
/// </summary>
public static DialogResult Show(
IWin32Window? owner,
string? text,
string? caption,
MessageBoxButtons buttons,
MessageBoxIcon icon,
MessageBoxDefaultButton defaultButton,
MessageBoxOptions options,
string helpFilePath,
HelpNavigator navigator)
{
HelpInfo hpi = new(helpFilePath, navigator);
return ShowCore(owner, text, caption, buttons, icon, defaultButton, options, hpi);
}
/// <summary>
/// Displays a message box with specified text, caption, style, Help file Path ,HelpNavigator and object.
/// </summary>
public static DialogResult Show(
string? text,
string? caption,
MessageBoxButtons buttons,
MessageBoxIcon icon,
MessageBoxDefaultButton defaultButton,
MessageBoxOptions options,
string helpFilePath,
HelpNavigator navigator,
object? param)
{
HelpInfo hpi = new(helpFilePath, navigator, param);
return ShowCore(null, text, caption, buttons, icon, defaultButton, options, hpi);
}
/// <summary>
/// Displays a message box with specified text, caption, style, Help file Path ,HelpNavigator and object for a IWin32Window.
/// </summary>
public static DialogResult Show(
IWin32Window? owner,
string? text,
string? caption,
MessageBoxButtons buttons,
MessageBoxIcon icon,
MessageBoxDefaultButton defaultButton,
MessageBoxOptions options,
string helpFilePath,
HelpNavigator navigator,
object? param)
{
HelpInfo hpi = new(helpFilePath, navigator, param);
return ShowCore(owner, text, caption, buttons, icon, defaultButton, options, hpi);
}
/// <summary>
/// Displays a message box with specified text, caption, and style.
/// </summary>
public static DialogResult Show(
string? text,
string? caption,
MessageBoxButtons buttons,
MessageBoxIcon icon,
MessageBoxDefaultButton defaultButton,
MessageBoxOptions options)
{
return ShowCore(null, text, caption, buttons, icon, defaultButton, options, false);
}
/// <summary>
/// Displays a message box with specified text, caption, and style.
/// </summary>
public static DialogResult Show(
string? text,
string? caption,
MessageBoxButtons buttons,
MessageBoxIcon icon,
MessageBoxDefaultButton defaultButton)
{
return ShowCore(null, text, caption, buttons, icon, defaultButton, 0, false);
}
/// <summary>
/// Displays a message box with specified text, caption, and style.
/// </summary>
public static DialogResult Show(
string? text,
string? caption,
MessageBoxButtons buttons,
MessageBoxIcon icon)
{
return ShowCore(null, text, caption, buttons, icon, MessageBoxDefaultButton.Button1, 0, false);
}
/// <summary>
/// Displays a message box with specified text, caption, and style.
/// </summary>
public static DialogResult Show(string? text, string? caption, MessageBoxButtons buttons)
{
return ShowCore(null, text, caption, buttons, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, 0, false);
}
/// <summary>
/// Displays a message box with specified text and caption.
/// </summary>
public static DialogResult Show(string? text, string? caption)
{
return ShowCore(null, text, caption, MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, 0, false);
}
/// <summary>
/// Displays a message box with specified text.
/// </summary>
public static DialogResult Show(string? text)
{
return ShowCore(null, text, string.Empty, MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, 0, false);
}
/// <summary>
/// Displays a message box with specified text, caption, and style.
/// </summary>
public static DialogResult Show(
IWin32Window? owner,
string? text,
string? caption,
MessageBoxButtons buttons,
MessageBoxIcon icon,
MessageBoxDefaultButton defaultButton,
MessageBoxOptions options)
{
return ShowCore(owner, text, caption, buttons, icon, defaultButton, options, false);
}
/// <summary>
/// Displays a message box with specified text, caption, and style.
/// </summary>
public static DialogResult Show(
IWin32Window? owner,
string? text,
string? caption,
MessageBoxButtons buttons,
MessageBoxIcon icon,
MessageBoxDefaultButton defaultButton)
{
return ShowCore(owner, text, caption, buttons, icon, defaultButton, 0, false);
}
/// <summary>
/// Displays a message box with specified text, caption, and style.
/// </summary>
public static DialogResult Show(
IWin32Window? owner,
string? text,
string? caption,
MessageBoxButtons buttons,
MessageBoxIcon icon)
{
return ShowCore(owner, text, caption, buttons, icon, MessageBoxDefaultButton.Button1, 0, false);
}
/// <summary>
/// Displays a message box with specified text, caption, and style.
/// </summary>
public static DialogResult Show(
IWin32Window? owner,
string? text,
string? caption,
MessageBoxButtons buttons)
{
return ShowCore(owner, text, caption, buttons, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, 0, false);
}
/// <summary>
/// Displays a message box with specified text and caption.
/// </summary>
public static DialogResult Show(IWin32Window? owner, string? text, string? caption)
{
return ShowCore(owner, text, caption, MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, 0, false);
}
/// <summary>
/// Displays a message box with specified text.
/// </summary>
public static DialogResult Show(IWin32Window? owner, string? text)
{
return ShowCore(owner, text, string.Empty, MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, 0, false);
}
private static DialogResult ShowCore(
IWin32Window? owner,
string? text,
string? caption,
MessageBoxButtons buttons,
MessageBoxIcon icon,
MessageBoxDefaultButton defaultButton,
MessageBoxOptions options,
HelpInfo hpi)
{
DialogResult result = DialogResult.None;
try
{
PushHelpInfo(hpi);
result = ShowCore(owner, text, caption, buttons, icon, defaultButton, options, true);
}
finally
{
PopHelpInfo();
}
return result;
}
private static DialogResult ShowCore(
IWin32Window? owner,
string? text,
string? caption,
MessageBoxButtons buttons,
MessageBoxIcon icon,
MessageBoxDefaultButton defaultButton,
MessageBoxOptions options,
bool showHelp)
{
if (AppContextSwitches.NoClientNotifications)
{
return DialogResult.None;
}
MESSAGEBOX_STYLE style = GetMessageBoxStyle(owner, buttons, icon, defaultButton, options, showHelp);
HandleRef<HWND> handle = default;
if (showHelp || ((options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) == 0))
{
handle = owner is null ? Control.GetHandleRef(PInvoke.GetActiveWindow()) : Control.GetSafeHandle(owner);
}
if (Application.UseVisualStyles)
{
// CLR4.0 or later, shell32.dll needs to be loaded explicitly.
if (PInvoke.GetModuleHandle(Libraries.Shell32) == 0)
{
if (PInvoke.LoadLibraryFromSystemPathIfAvailable(Libraries.Shell32) == HINSTANCE.Null)
{
int lastWin32Error = Marshal.GetLastWin32Error();
throw new Win32Exception(lastWin32Error, string.Format(SR.LoadDLLError, Libraries.Shell32));
}
}
}
// Activate theming scope to get theming for controls at design time and when hosted in browser.
// NOTE: If a theming context is already active, this call is very fast, so shouldn't be a perf issue.
using ThemingScope scope = new(Application.UseVisualStyles);
Application.BeginModalMessageLoop();
try
{
return (DialogResult)PInvoke.MessageBox(handle.Handle, text, caption, style);
}
finally
{
Application.EndModalMessageLoop();
// Right after the dialog box is closed, Windows sends WM_SETFOCUS back to the previously active control
// but since we have disabled this thread main window the message is lost. So we have to send it again after
// we enable the main window.
PInvokeCore.SendMessage(handle, PInvokeCore.WM_SETFOCUS);
}
}
}