-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelpers.cs
More file actions
411 lines (358 loc) · 15.8 KB
/
Copy pathHelpers.cs
File metadata and controls
411 lines (358 loc) · 15.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
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Text;
using System.Reflection;
using System.ComponentModel;
using System.Xml;
namespace Windows_Dialog_Box_Generator
{
public static class UIHelper
{
public static RadioButton? GetCheckedRadioButton(Control container)
{
foreach (var c in container.Controls)
{
if (c is RadioButton radio)
{
if (radio.Checked)
{
return radio;
}
}
}
return null;
}
/// <summary>
/// Automatically sets a radio button to be checked with exclusive behavior.
/// </summary>
/// <remarks>
/// Unlike setting <see cref="RadioButton.Checked"/> directly, this method automatically unchecks the other RadioButtons in the parent container. This overload auto-detects the container from the parent of the RadioButton to be checked.
/// </remarks>
/// <param name="target">The target radio button to be checked.</param>
public static void SetCheckedRadioButton(RadioButton target)
{
if (target.Parent == null) return;
foreach (var rb in target.Parent.Controls.OfType<RadioButton>())
{
rb.Checked = rb == target;
}
}
/// <summary>
/// Automatically sets a radio button to be checked with exclusive behavior.
/// </summary>
/// <remarks>
/// Unlike setting <see cref="RadioButton.Checked"/> directly, this method automatically unchecks the other RadioButtons in the container.
/// </remarks>
/// <param name="container">The container to look for <see cref="RadioButton"/>s in.</param>
/// <param name="target">The target radio button to be checked.</param>
public static void SetCheckedRadioButton(Control container, RadioButton target)
{
foreach (var rb in container.Controls.OfType<RadioButton>())
{
rb.Checked = rb == target;
}
}
/// <summary>
/// Automatically sets a radio button to be checked with exclusive behavior. This overload allows multiple containers.
/// </summary>
/// <remarks>
/// Unlike setting <see cref="RadioButton.Checked"/> directly, this method automatically unchecks the other RadioButtons in the containers.
/// </remarks>
/// <param name="target">The target radio button to be checked.</param>
/// <param name="containers">The containers to look for <see cref="RadioButton"/>s in.</param>
public static void SetCheckedRadioButton(RadioButton target, params Control[] containers)
{
foreach (var rb in containers.SelectMany(c => c.Controls.OfType<RadioButton>()))
{
rb.Checked = rb == target;
}
}
}
public static class DialogHelper
{
[DllImport("shell32.dll", EntryPoint = "PickIconDlg", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool PickIconDlg(nint hWnd, [MarshalAs(UnmanagedType.LPTStr)] StringBuilder pszIconPath, uint cchIconPath, ref int piIconIndex);
// The Windows API's PickIconDlg allows passing null for the hWnd
/// <summary>
/// Declares the <c>MAX_PATH</c> environment variable from the Windows API.
/// </summary>
public const int MAX_PATH = 260;
/// <summary>
/// Displays a dialog box that allows the user to choose an icon from the selection available embedded in a resource such as an executable or DLL file.
/// </summary>
/// <param name="hWnd">The handle of the parent window.</param>
/// <param name="defIconPath">A string that contains the fully qualified path of the default resource that contains the icons.
/// You should verify that the path is valid before using it.</param>
/// <param name="defIconIndex">An integer that specifies the index of the initial selection.</param>
/// <param name="iconPath">If the user chooses a different resource in the dialog, contains the path of that file when the function returns; otherwise, it contains
/// the path stored in <paramref name="defIconPath"/>.</param>
/// <param name="iconIndex">When this function returns successfully, receives the index of the icon that was selected.</param>
/// <returns><see langword="true"/> if successful; otherwise, <see langword="false"/>.</returns>
[SupportedOSPlatform("windows5.1")] // Windows XP
public static bool ShowPickIconDialog(nint hWnd, string defIconPath, int defIconIndex, out string iconPath, out int iconIndex)
{
var sb = new StringBuilder(defIconPath, MAX_PATH);
iconIndex = defIconIndex;
var result = PickIconDlg(hWnd, sb, (uint)sb.Capacity, ref iconIndex);
iconPath = sb.ToString();
return result;
}
}
public enum TDM : uint
{
NAVIGATE_PAGE = WM.USER + 101,
/// <summary>
/// "wParam = Button ID"
/// </summary>
CLICK_BUTTON = WM.USER + 102,
/// <summary>
/// "wParam = 0 (nonMarque) wParam != 0 (Marquee)"
/// </summary>
SET_MARQUEE_PROGRESS_BAR = WM.USER + 103,
/// <summary>
/// "wParam = new progress state"
/// </summary>
SET_PROGRESS_BAR_STATE = WM.USER + 104,
/// <summary>
/// "lParam = MAKELPARAM(nMinRange, nMaxRange)"
/// </summary>
SET_PROGRESS_BAR_RANGE = WM.USER + 105,
/// <summary>
/// "wParam = new position"
/// </summary>
SET_PROGRESS_BAR_POS = WM.USER + 106,
/// <summary>
/// "wParam = 0 (stop marquee), wParam != 0 (start marquee),
/// lparam = speed (milliseconds between repaints)"
/// </summary>
SET_PROGRESS_BAR_MARQUEE = WM.USER + 107,
/// <summary>
/// "wParam = element (TASKDIALOG_ELEMENTS), lParam = new element text (LPCWSTR)"
/// </summary>
SET_ELEMENT_TEXT = WM.USER + 108,
/// <summary>
/// "wParam = Radio Button ID"
/// </summary>
CLICK_RADIO_BUTTON = WM.USER + 110,
/// <summary>
/// "lParam = 0 (disable), lParam != 0 (enable), wParam = Button ID"
/// </summary>
ENABLE_BUTTON = WM.USER + 111,
/// <summary>
/// "lParam = 0 (disable), lParam != 0 (enable), wParam = Radio Button ID"
/// </summary>
ENABLE_RADIO_BUTTON = WM.USER + 112,
/// <summary>
/// "wParam = 0 (unchecked), 1 (checked), lParam = 1 (set key focus)"
/// </summary>
CLICK_VERIFICATION = WM.USER + 113,
/// <summary>
/// "wParam = element (TASKDIALOG_ELEMENTS), lParam = new element text (LPCWSTR)"
/// </summary>
UPDATE_ELEMENT_TEXT = WM.USER + 114,
/// <summary>
/// "wParam = Button ID, lParam = 0 (elevation not required),
/// lParam != 0 (elevation required)"
/// </summary>
SET_BUTTON_ELEVATION_REQUIRED_STATE = WM.USER + 115,
/// <summary>
/// "wParam = icon element (TASKDIALOG_ICON_ELEMENTS), lParam = new icon
/// (hIcon if TDF_USE_HICON_* was set, PCWSTR otherwise)"
/// </summary>
UPDATE_ICON = WM.USER + 116,
}
public enum WM : uint
{
USER = 0x0400,
WM_SETICON = 0x0080,
}
public static class ObjectExtensions
{
// Warning: may throw an exception if the object doesn't have a parameterless constructor
public static object? Clone(this object obj)
{
var t = obj.GetType();
var n = t.GetConstructor(Type.EmptyTypes)?.Invoke(null);
foreach (var prop in t.GetProperties(BindingFlags.Instance | BindingFlags.Public))
{
if (prop.CanRead && prop.CanWrite)
prop.SetValue(n, prop.GetValue(obj));
}
return n;
}
}
/// <summary>
/// Maps RID (Resource IDs) to IID (Icon IDs) and allows you to easily convert between them.
/// </summary>
public partial class IconResourceMapper
{
private readonly List<ushort> _resourceIds = new();
/// <summary>
/// Creates a new instance of the <see cref="IconResourceMapper"/> class.
/// </summary>
/// <param name="dllPath">The path to the DLL (Dynamic Link Library) to load and map.</param>
/// <exception cref="Win32Exception">An error occured loading the DLL library.</exception>
/// <exception cref="NotSupportedException">Could not map resource IDs because non-integer resource names are not supported.</exception>
public IconResourceMapper(string dllPath)
{
IntPtr hModule = LoadLibraryExW(dllPath, IntPtr.Zero, LoadLibraryFlags.LOAD_LIBRARY_AS_DATAFILE);
if (hModule == IntPtr.Zero)
throw new Win32Exception();
try
{
EnumResourceNamesW(hModule, RT_GROUP_ICON, (h, t, name, l) =>
{
// Resource IDs can be either integer or string
if (IsIntResource(name, out ushort id))
_resourceIds.Add(id);
else
throw new NotSupportedException("Non-integer resource names are not supported.");
return true;
}, IntPtr.Zero);
}
finally
{
FreeLibrary(hModule);
}
}
/// <summary>
/// Returns the icon index corresponding to the resource ID.
/// </summary>
public int GetIconIndex(ushort resourceId)
{
int index = _resourceIds.IndexOf(resourceId);
if (index == -1)
throw new ArgumentException($"Resource ID {resourceId} not found in this DLL.");
return index;
}
/// <summary>
/// Returns the resource ID corresponding to the icon index.
/// </summary>
public ushort GetResourceId(int iconIndex)
{
if (iconIndex < 0 || iconIndex >= _resourceIds.Count)
throw new ArgumentOutOfRangeException(nameof(iconIndex));
return _resourceIds[iconIndex];
}
public int Count => _resourceIds.Count;
#region Win32 API
private const uint RT_GROUP_ICON = 14;
private delegate bool EnumResNameProc(IntPtr hModule, IntPtr lpszType, IntPtr lpszName, IntPtr lParam);
[LibraryImport("kernel32.dll", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)]
private static partial IntPtr LoadLibraryExW(string lpFileName, IntPtr hFile, LoadLibraryFlags dwFlags);
[LibraryImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static partial bool FreeLibrary(IntPtr hModule);
[LibraryImport("kernel32.dll", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)]
[return: MarshalAs(UnmanagedType.Bool)]
private static partial bool EnumResourceNamesW(IntPtr hModule, uint lpszType, EnumResNameProc lpEnumFunc, IntPtr lParam);
private static bool IsIntResource(IntPtr name, out ushort id)
{
long val = name.ToInt64();
if ((val >> 16) == 0)
{
id = (ushort)val;
return true;
}
id = 0;
return false;
}
[Flags]
private enum LoadLibraryFlags : uint
{
LOAD_LIBRARY_AS_DATAFILE = 0x00000002
}
#endregion
}
/// <summary>
/// Converts an icon index returned by PickIconDlg to an actual resource ID.
/// </summary>
public partial class IconIndexMapper
{
private readonly List<int> _resourceIds = new();
public IconIndexMapper(string dllPath)
{
// Enumerate icon resource IDs
EnumResourceNames(new IntPtr(-1), dllPath);
}
public int GetResourceIdFromPickIconIndex(int pickIndex)
{
if (pickIndex < 0 || pickIndex >= _resourceIds.Count)
throw new ArgumentOutOfRangeException(nameof(pickIndex));
return _resourceIds[pickIndex];
}
#region Win32
private delegate bool EnumResNameProc(IntPtr hModule, int lpszType, IntPtr lpszName, IntPtr lParam);
[LibraryImport("kernel32.dll", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)]
private static partial IntPtr LoadLibraryExW(string lpFileName, IntPtr hFile, int dwFlags);
[LibraryImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static partial bool FreeLibrary(IntPtr hModule);
[LibraryImport("kernel32.dll", StringMarshalling = StringMarshalling.Utf16, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static partial bool EnumResourceNamesW(IntPtr hModule, int lpszType, EnumResNameProc lpEnumFunc, IntPtr lParam);
private const int RT_GROUP_ICON = 14;
private const int LOAD_LIBRARY_AS_DATAFILE = 0x00000002;
private void EnumResourceNames(IntPtr dummy, string dllPath)
{
IntPtr hModule = LoadLibraryExW(dllPath, IntPtr.Zero, LOAD_LIBRARY_AS_DATAFILE);
if (hModule == IntPtr.Zero)
throw new Win32Exception(Marshal.GetLastWin32Error());
try
{
EnumResourceNamesW(hModule, RT_GROUP_ICON, (h, t, name, l) =>
{
// Resource IDs can be int or string (if MAKEINTRESOURCE not used)
if (((long)name >> 16) == 0) // integer ID
{
_resourceIds.Add(name.ToInt32());
}
return true;
}, IntPtr.Zero);
}
finally
{
FreeLibrary(hModule);
}
}
#endregion
}
public static partial class IconHelper
{
private const uint RT_GROUP_ICON = 14;
private const uint LOAD_LIBRARY_AS_DATAFILE = 0x00000002;
private delegate bool EnumResNameProc(IntPtr hModule, IntPtr lpszType, IntPtr lpszName, IntPtr lParam);
[LibraryImport("kernel32.dll", SetLastError = true, StringMarshalling = StringMarshalling.Utf16)]
private static partial IntPtr LoadLibraryExW(string lpFileName, IntPtr hFile, uint dwFlags);
[LibraryImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static partial bool FreeLibrary(IntPtr hModule);
[LibraryImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static partial bool EnumResourceNamesW(IntPtr hModule, uint lpszType, EnumResNameProc lpEnumFunc, IntPtr lParam);
/// <summary>
/// Returns the number of icons contained in the specified file.
/// </summary>
public static int CountIcons(string filePath)
{
IntPtr hModule = LoadLibraryExW(filePath, IntPtr.Zero, LOAD_LIBRARY_AS_DATAFILE);
if (hModule == IntPtr.Zero)
throw new Win32Exception(Marshal.GetLastWin32Error());
int count = 0;
try
{
EnumResourceNamesW(hModule, RT_GROUP_ICON, (h, t, name, l) =>
{
count++;
return true;
}, IntPtr.Zero);
}
finally
{
FreeLibrary(hModule);
}
return count;
}
}
}