-
Notifications
You must be signed in to change notification settings - Fork 0
/
DialogWindows.cs
205 lines (178 loc) · 6.73 KB
/
DialogWindows.cs
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
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Text;
namespace DotCef;
[SupportedOSPlatform("windows")]
public static class DialogWindows
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct OPENFILENAME
{
public int lStructSize;
public IntPtr hwndOwner;
public IntPtr hInstance;
public string lpstrFilter;
public string lpstrCustomFilter;
public int nMaxCustFilter;
public int nFilterIndex;
public IntPtr lpstrFile;
public int nMaxFile;
public IntPtr lpstrFileTitle;
public int nMaxFileTitle;
public IntPtr lpstrInitialDir;
public string lpstrTitle;
public int Flags;
public short nFileOffset;
public short nFileExtension;
public string lpstrDefExt;
public IntPtr lCustData;
public IntPtr lpfnHook;
public string lpTemplateName;
}
[DllImport("comdlg32.dll", CharSet = CharSet.Unicode)]
private static extern bool GetOpenFileName([In, Out] ref OPENFILENAME ofn);
[DllImport("comdlg32.dll", CharSet = CharSet.Unicode)]
private static extern uint CommDlgExtendedError();
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct BROWSEINFO
{
public IntPtr hwndOwner;
public IntPtr pidlRoot;
public IntPtr pszDisplayName;
public string lpszTitle;
public uint ulFlags;
public IntPtr lpfn;
public IntPtr lParam;
public int iImage;
}
[DllImport("shell32.dll", CharSet = CharSet.Unicode)]
private static extern IntPtr SHBrowseForFolder(ref BROWSEINFO lpbi);
[DllImport("shell32.dll", CharSet = CharSet.Unicode)]
private static extern bool SHGetPathFromIDList(IntPtr pidl, StringBuilder pszPath);
[DllImport("ole32.dll")]
private static extern void CoTaskMemFree(IntPtr ptr);
private const int MAX_PATH = 260;
private const int OFN_PATHMUSTEXIST = 0x00000800;
private const int OFN_FILEMUSTEXIST = 0x00001000;
private const int OFN_ALLOWMULTISELECT = 0x00000200;
private const int OFN_EXPLORER = 0x00080000;
private const int OFN_OVERWRITEPROMPT = 0x00000002;
private const int OFN_NOCHANGEDIR = 0x00000008;
[DllImport("comdlg32.dll", CharSet = CharSet.Unicode)]
private static extern bool GetSaveFileName([In, Out] ref OPENFILENAME ofn);
public unsafe static string[] PickFiles(bool multiple, params (string, string)[] filters)
{
var ofn = new OPENFILENAME();
ofn.lStructSize = Marshal.SizeOf<OPENFILENAME>();
ofn.lpstrFile = Marshal.AllocHGlobal(2 * 1024);
Unsafe.InitBlockUnaligned((byte*)ofn.lpstrFile, 0, 2 * 1024);
try
{
ofn.nMaxFile = 1024;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
if (multiple)
ofn.Flags |= OFN_ALLOWMULTISELECT | OFN_EXPLORER;
StringBuilder filterBuilder = new StringBuilder();
foreach (var (description, extension) in filters)
filterBuilder.Append($"{description}\0{extension}\0");
filterBuilder.Append("\0");
ofn.lpstrFilter = filterBuilder.ToString();
var files = new List<string>();
if (GetOpenFileName(ref ofn))
{
if (multiple)
{
IntPtr currentPtr = ofn.lpstrFile;
string firstItem = Marshal.PtrToStringUni(currentPtr)!;
currentPtr += (firstItem.Length + 1) * 2;
string nextItem = Marshal.PtrToStringUni(currentPtr)!;
if (string.IsNullOrEmpty(nextItem))
{
files.Add(firstItem);
}
else
{
while (true)
{
nextItem = Marshal.PtrToStringUni(currentPtr)!;
if (string.IsNullOrEmpty(nextItem))
break;
files.Add(Path.Combine(firstItem, nextItem));
currentPtr += (nextItem.Length + 1) * 2;
}
}
}
else
{
files.Add(Marshal.PtrToStringUni(ofn.lpstrFile)!);
}
}
else
{
uint error = CommDlgExtendedError();
}
return files.ToArray();
}
finally
{
Marshal.FreeHGlobal(ofn.lpstrFile);
}
}
public static string PickDirectory()
{
var bi = new BROWSEINFO();
bi.lpszTitle = "Select Directory";
IntPtr pidl = SHBrowseForFolder(ref bi);
if (pidl != IntPtr.Zero)
{
StringBuilder path = new StringBuilder(MAX_PATH);
if (SHGetPathFromIDList(pidl, path))
{
CoTaskMemFree(pidl);
return path.ToString();
}
CoTaskMemFree(pidl);
}
return "";
}
public static string SaveFile(string defaultName, List<(string description, string extension)> filters)
{
var ofn = new OPENFILENAME();
ofn.lStructSize = Marshal.SizeOf<OPENFILENAME>();
ofn.hwndOwner = IntPtr.Zero;
ofn.lpstrFile = Marshal.AllocHGlobal(2 * 1024);
try
{
ofn.nMaxFile = 1024;
ofn.lpstrFilter = "";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = IntPtr.Zero;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = IntPtr.Zero;
ofn.Flags = OFN_OVERWRITEPROMPT | OFN_NOCHANGEDIR;
if (!string.IsNullOrEmpty(defaultName))
{
var nameBytes = Encoding.Unicode.GetBytes(defaultName);
var numCharsToCopy = Math.Min(nameBytes.Length, 1024 - 1);
Marshal.Copy(nameBytes, 0, ofn.lpstrFile, numCharsToCopy);
}
StringBuilder file = new StringBuilder(1024);
file.Append(defaultName);
StringBuilder filterBuilder = new StringBuilder();
foreach (var (description, extension) in filters)
filterBuilder.Append($"{description}\0*.{extension}\0");
filterBuilder.Append("\0");
ofn.lpstrFilter = filterBuilder.ToString();
string fileName = "";
if (GetSaveFileName(ref ofn))
fileName = Marshal.PtrToStringUni(ofn.lpstrFile)!;
return fileName;
}
finally
{
Marshal.FreeHGlobal(ofn.lpstrFile);
}
}
}