-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathWindowsUtility.cs
More file actions
427 lines (376 loc) · 16 KB
/
WindowsUtility.cs
File metadata and controls
427 lines (376 loc) · 16 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
namespace Menees.Windows.Presentation
{
#region Using Directives
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Threading;
using Menees.Diagnostics;
using Menees.Shell;
#endregion
/// <summary>
/// Methods and properties for Windows applications.
/// </summary>
public static class WindowsUtility
{
#region Public Properties
/// <summary>
/// Gets whether the clipboard currently contains text data.
/// </summary>
public static bool ClipboardContainsText
{
get
{
bool result = false;
try
{
IDataObject? data = Clipboard.GetDataObject();
if (data != null)
{
result = data.GetDataPresent(DataFormats.Text);
}
}
catch (ExternalException)
{
result = false;
}
return result;
}
}
#endregion
#region Public Methods
/// <summary>
/// Tries to bring the specified window to the front of the Z-order and activate it.
/// </summary>
/// <param name="window">The window to activate.</param>
public static void BringToFront(Window window)
{
Conditions.RequireReference(window, nameof(window));
// This came from http://stackoverflow.com/questions/257587/bring-a-window-to-the-front-in-wpf/4831839#4831839
if (!window.IsVisible)
{
window.Show();
}
if (window.WindowState == WindowState.Minimized)
{
window.WindowState = WindowState.Normal;
}
window.Activate();
if (!window.Topmost)
{
window.Topmost = true;
window.Topmost = false;
}
window.Focus();
}
/// <summary>
/// Gets a reference to the Window that hosts the content tree that contains the dependency object.
/// </summary>
/// <param name="dependencyObject">A dependency object or null.</param>
/// <returns>A Window reference to the host window, or null if <paramref name="dependencyObject"/> is null.</returns>
public static Window? GetWindow(DependencyObject dependencyObject)
{
Window? result = null;
if (dependencyObject != null)
{
result = Window.GetWindow(dependencyObject);
}
return result;
}
/// <summary>
/// Initializes the application's name and sets up a handler to report uncaught
/// exceptions.
/// </summary>
/// <param name="applicationName">The name of the application to pass to <see cref="ApplicationInfo.Initialize"/>.</param>
/// <param name="showException">The action to call when an exception needs to be shown. This can be null,
/// which will cause <see cref="ShowError(Window,string)"/> to be called.</param>
/// <param name="applicationAssembly">The assembly that's initializing the application, typically the main executable.</param>
public static void InitializeApplication(string applicationName, Action<Exception>? showException, Assembly? applicationAssembly = null)
{
ApplicationInfo.Initialize(applicationName, applicationAssembly ?? Assembly.GetCallingAssembly(), () => HandleUtility.IsApplicationActivated);
Application.Current.DispatcherUnhandledException += (sender, e) =>
{
e.Handled = true;
Exception ex = e.Exception;
Log.Error(typeof(WindowsUtility), "An unhandled exception occurred in a Windows thread.", ex);
ApplicationInfo.ShowUnhandledException(ex, message => e.Dispatcher.BeginInvoke(new Action(() => ShowError(null, message))), showException);
};
}
/// <summary>
/// Gets whether the specified window is in design mode.
/// </summary>
public static bool IsInDesignMode(DependencyObject dependencyObject)
{
Conditions.RequireReference(dependencyObject, nameof(dependencyObject));
// http://stackoverflow.com/questions/834283/is-there-a-way-to-check-if-wpf-is-currently-executing-in-design-mode-or-not
bool result = DesignerProperties.GetIsInDesignMode(dependencyObject);
return result;
}
/// <summary>
/// Selects a file system path and allows the user to type in a path if necessary.
/// </summary>
/// <param name="owner">The owner of the displayed modal dialog.</param>
/// <param name="title">A short title for the path being selected.</param>
/// <param name="initialFolder">The initial path to select.</param>
/// <returns>The path the user selected if they pressed OK. Null otherwise (e.g., the user cancelled).</returns>
public static string? SelectFolder(DependencyObject owner, string? title, string? initialFolder)
=> SelectFolder(GetWindow(owner), title, initialFolder);
/// <summary>
/// Selects a file system path and allows the user to type in a path if necessary.
/// </summary>
/// <param name="owner">The owner of the displayed modal dialog.</param>
/// <param name="title">A short title for the path being selected.</param>
/// <param name="initialFolder">The initial path to select.</param>
/// <returns>The path the user selected if they pressed OK. Null otherwise (e.g., the user cancelled).</returns>
public static string? SelectFolder(Window? owner, string? title, string? initialFolder)
=> HandleUtility.SelectFolder(TryGetHandle(owner), title, initialFolder);
/// <summary>
/// Moves the window behind other top-level windows.
/// </summary>
/// <param name="window">The window to move to the bottom of the Z-order.</param>
public static void SendToBack(Window window)
{
Conditions.RequireReference(window, nameof(window));
NativeMethods.SendToBack(window);
}
/// <summary>
/// Executes the default action on the specified file using the Windows shell.
/// </summary>
/// <param name="dependencyObject">A dependency object that can be used to find
/// the parent window for any error dialogs.</param>
/// <param name="fileName">The text or filename to execute.</param>
/// <returns>Whether the file was opened/executed successfully.</returns>
public static bool ShellExecute(DependencyObject dependencyObject, string fileName)
=> ShellExecute(GetWindow(dependencyObject), fileName);
/// <summary>
/// Executes an action on the specified file using the Windows shell.
/// </summary>
/// <param name="dependencyObject">A dependency object that can be used to find
/// the parent window for any error dialogs.</param>
/// <param name="fileName">The text or filename to execute.</param>
/// <param name="verb">The shell action that should be taken. Pass an empty string for the default action.</param>
/// <returns>The process started by executing the file.</returns>
public static Process? ShellExecute(DependencyObject dependencyObject, string fileName, string verb)
=> ShellExecute(GetWindow(dependencyObject), fileName, verb);
/// <summary>
/// Executes the default action on the specified file using the Windows shell.
/// </summary>
/// <param name="owner">The parent window for any error dialogs.</param>
/// <param name="fileName">The text or filename to execute.</param>
/// <returns>Whether the file was opened/executed successfully.</returns>
public static bool ShellExecute(Window? owner, string fileName)
{
bool result = false;
try
{
using (Process? process = ShellExecute(owner, fileName, string.Empty))
{
result = true;
}
}
catch (Win32Exception)
{
// The core ShellExecute logic already displays an error dialog if a Win32Exception occurs.
}
return result;
}
/// <summary>
/// Executes an action on the specified file using the Windows shell.
/// </summary>
/// <param name="owner">The parent window for any error dialogs.</param>
/// <param name="fileName">The text or filename to execute.</param>
/// <param name="verb">The shell action that should be taken. Pass an empty string for the default action.</param>
/// <returns>The process started by executing the file.</returns>
public static Process? ShellExecute(Window? owner, string fileName, string verb)
=> ShellUtility.ShellExecute(TryGetHandle(owner), fileName, verb);
/// <summary>
/// Displays a standard "About" dialog for the current application assembly.
/// </summary>
/// <remarks>
/// At application startup, you should call the <see cref="InitializeApplication"/>
/// method to set the current application's name, which will be displayed in the
/// About box.
/// </remarks>
/// <param name="owner">The owner of the displayed modal dialog.</param>
/// <param name="mainAssembly">The main assembly for the application,
/// which the version and copyright information will be read from.</param>
/// <param name="repository">The name of a GitHub repository. If null, then
/// <see cref="ApplicationInfo.ApplicationName"/> is used.</param>
public static void ShowAboutBox(Window? owner, Assembly mainAssembly, string? repository = null)
{
// If an assembly wasn't provided, then we want the version of the calling assembly not the current assembly.
AboutBox dialog = new(mainAssembly ?? Assembly.GetCallingAssembly(), repository);
dialog.Execute(owner);
}
/// <summary>
/// Displays an error message in a MessageBox.
/// </summary>
/// <param name="dependencyObject">A dependency object that can be used to find the owner window.
/// This can be null to use the desktop as the owner.</param>
/// <param name="message">The message to display.</param>
/// <param name="caption">The caption to use as the dialog's title. If null, the application name is used.</param>
public static void ShowError(DependencyObject dependencyObject, string message, string? caption = null)
{
ShowError(GetWindow(dependencyObject), message, caption);
}
/// <summary>
/// Displays an error message in a MessageBox.
/// </summary>
/// <param name="owner">The dialog owner window. This can be null to use the desktop as the owner.</param>
/// <param name="message">The message to display.</param>
/// <param name="caption">The caption to use as the dialog's title. If null, the application name is used.</param>
public static void ShowError(Window? owner, string message, string? caption = null)
{
caption ??= ApplicationInfo.ApplicationName;
// WPF's stupid MessageBox implementation throws an ArgumentNullException if we pass it a null owner.
if (owner != null)
{
MessageBox.Show(owner, message, caption, MessageBoxButton.OK, MessageBoxImage.Error);
}
else
{
MessageBox.Show(message, caption, MessageBoxButton.OK, MessageBoxImage.Error);
}
}
/// <summary>
/// Displays an information message in a MessageBox.
/// </summary>
/// <param name="dependencyObject">A dependency object that can be used to find the owner window.
/// This can be null to use the desktop as the owner.</param>
/// <param name="message">The message to display.</param>
/// <param name="caption">The caption to use as the dialog's title. If null, the application name is used.</param>
public static void ShowInfo(DependencyObject dependencyObject, string message, string? caption = null)
{
ShowInfo(GetWindow(dependencyObject), message, caption);
}
/// <summary>
/// Displays an information message in a MessageBox.
/// </summary>
/// <param name="owner">The dialog owner window. This can be null to use the desktop as the owner.</param>
/// <param name="message">The message to display.</param>
/// <param name="caption">The caption to use as the dialog's title. If null, the application name is used.</param>
public static void ShowInfo(Window? owner, string message, string? caption = null)
{
caption ??= ApplicationInfo.ApplicationName;
// WPF's stupid MessageBox implementation throws an ArgumentNullException if we pass it a null owner.
if (owner != null)
{
MessageBox.Show(owner, message, caption, MessageBoxButton.OK, MessageBoxImage.Information);
}
else
{
MessageBox.Show(message, caption, MessageBoxButton.OK, MessageBoxImage.Information);
}
}
/// <summary>
/// Displays a yes/no question in a MessageBox.
/// </summary>
/// <param name="owner">The dialog owner window. This can be null to use the desktop as the owner.</param>
/// <param name="yesNoQuestion">The yes/no question to display.</param>
/// <param name="caption">The caption to use as the dialog's title. If null, the application name is used.</param>
/// <param name="defaultYes">Whether the default button should be Yes (instead of No).</param>
public static bool ShowQuestion(Window? owner, string yesNoQuestion, string? caption = null, bool defaultYes = true)
{
caption ??= ApplicationInfo.ApplicationName;
MessageBoxResult defaultResult = defaultYes ? MessageBoxResult.Yes : MessageBoxResult.No;
// WPF's stupid MessageBox implementation throws an ArgumentNullException if we pass it a null owner.
MessageBoxResult mbResult;
if (owner != null)
{
mbResult = MessageBox.Show(owner, yesNoQuestion, caption, MessageBoxButton.YesNo, MessageBoxImage.Question, defaultResult);
}
else
{
mbResult = MessageBox.Show(yesNoQuestion, caption, MessageBoxButton.YesNo, MessageBoxImage.Question, defaultResult);
}
bool result = mbResult == MessageBoxResult.Yes;
return result;
}
/// <summary>
/// Shows an input box for a single value with validation.
/// </summary>
/// <param name="owner">The owner of the displayed modal dialog.</param>
/// <param name="prompt">The message to prompt with (up to 4 lines long).</param>
/// <param name="title">The caption of the displayed dialog. This can be null
/// to use the application name as the title.</param>
/// <param name="defaultValue">The initial value of the input field.</param>
/// <param name="maxLength">The maximum length of the input field in characters.
/// This can be null to use the default maximum length limit.</param>
/// <param name="validate">An optional function to validate the input. This can be null.
/// The function should return a null if the input passes validation, and it should return an error
/// message to display to the end user if the input fails validation.</param>
/// <returns>The user-entered value if they pressed OK, or null if Cancel was pressed.</returns>
public static string? ShowInputBox(
Window? owner,
string prompt,
string? title,
string? defaultValue,
int? maxLength = null,
Func<string, string>? validate = null)
{
InputDialog dialog = new();
if (string.IsNullOrEmpty(title))
{
dialog.Title = ApplicationInfo.ApplicationName;
}
else
{
dialog.Title = title;
}
string? result = dialog.Execute(owner, prompt, defaultValue, maxLength, validate);
return result;
}
/// <summary>
/// Gets the distinct set of <see cref="ValidationError"/> instances from the specified object's visual tree.
/// </summary>
/// <param name="dependencyObject">An object with a visual tree that can contain child items with
/// validation errors (e.g., a DataGrid).</param>
/// <returns>The unique ValidationError instances</returns>
/// <remarks>
/// This adds errors to an ISet because the same error can show up multiple times in the same visual tree.
/// For example, when a DataGridRow has an error in a TextBox, both will report the same ValidationError.
/// </remarks>
public static ISet<ValidationError> GetValidationErrors(DependencyObject dependencyObject)
{
Conditions.RequireReference(dependencyObject, nameof(dependencyObject));
HashSet<ValidationError> result = [];
GetValidationErrors(dependencyObject, result);
return result;
}
#endregion
#region Private Methods
private static void GetValidationErrors(DependencyObject parent, ISet<ValidationError> errors)
{
foreach (ValidationError error in Validation.GetErrors(parent))
{
errors.Add(error);
}
int visualChildCount = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < visualChildCount; i++)
{
DependencyObject child = VisualTreeHelper.GetChild(parent, i);
GetValidationErrors(child, errors);
}
}
private static IntPtr? TryGetHandle(Window? window)
{
IntPtr? result = null;
if (window != null)
{
result = NativeMethods.GetHandle(window);
}
return result;
}
#endregion
}
}