-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
377 lines (361 loc) · 18.1 KB
/
MainWindow.xaml.cs
File metadata and controls
377 lines (361 loc) · 18.1 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
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Imaging;
using Microsoft.UI.Xaml.Navigation;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using static WinUI3_IFileDialog.CFileDialog;
using GlobalStructures;
using static GlobalStructures.GlobalTools;
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
namespace WinUI3_IFileDialog
{
/// <summary>
/// An empty window that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainWindow : Window
{
public delegate int SUBCLASSPROC(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam, IntPtr uIdSubclass, uint dwRefData);
[DllImport("Comctl32.dll", SetLastError = true)]
public static extern bool SetWindowSubclass(IntPtr hWnd, SUBCLASSPROC pfnSubclass, uint uIdSubclass, uint dwRefData);
[DllImport("Comctl32.dll", SetLastError = true)]
public static extern int DefSubclassProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam);
COMDLG_FILTERSPEC[] cdfs = new COMDLG_FILTERSPEC[]
{
new COMDLG_FILTERSPEC("All files (*.*)", "*.*"),
new COMDLG_FILTERSPEC("JPG Joint Photographic Experts Group (*.jpg)", "*.jpg"),
new COMDLG_FILTERSPEC("PNG Portable Network Graphics (*.png)", "*.png"),
new COMDLG_FILTERSPEC("GIF Graphics Interchange Format (*.gif)", "*.gif"),
new COMDLG_FILTERSPEC("BMP Windows Bitmap (*.bmp)", "*.bmp"),
new COMDLG_FILTERSPEC("TIF Tagged Image File Format (*.tif)", "*.tif")
};
private IntPtr hWndMain = IntPtr.Zero;
private Microsoft.UI.Windowing.AppWindow _apw;
private SUBCLASSPROC SubClassDelegate;
public const int ID_PUSHBUTTON = 601;
ObservableCollection<File> files = new ObservableCollection<File>();
public MainWindow()
{
this.InitializeComponent();
hWndMain = WinRT.Interop.WindowNative.GetWindowHandle(this);
Microsoft.UI.WindowId myWndId = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(hWndMain);
_apw = Microsoft.UI.Windowing.AppWindow.GetFromWindowId(myWndId);
_apw.Resize(new Windows.Graphics.SizeInt32(1000, 530));
_apw.Move(new Windows.Graphics.PointInt32(400, 300));
this.Title = "WinUI 3 - Test IFileDialog";
// For custom Button
SubClassDelegate = new SUBCLASSPROC(WindowSubClass);
bool bRet = SetWindowSubclass(hWndMain, SubClassDelegate, 0, 0);
}
private async void btnChooseFiles_Click(object sender, RoutedEventArgs e)
{
// Documents
string sFolder = "shell:::{A8CDFF1C-4878-43be-B5FD-F8091C1C60D0}";
FOS nOptions = (FOS.FOS_FORCESHOWHIDDEN | FOS.FOS_ALLOWMULTISELECT | FOS.FOS_NODEREFERENCELINKS);
string sFile = null;
object oFileNames = new System.Collections.ObjectModel.Collection<string>();
IShellItem si = null;
IShellItemArray sia = null;
CFileDialog cfd = new CFileDialog(nOptions, "Choose files", sFolder);
HRESULT hr = cfd.SetFileTypes(cdfs, 1);
if ((cfd.ShowDialog(hWndMain, true) == DialogResult.OK))
{
files.Clear();
Windows.Storage.StorageFile file = null;
Windows.Storage.FileProperties.StorageItemThumbnail iconThumbnail = null;
if (cfd.FileName != null)
{
sFile = cfd.FileName;
si = cfd.ShellItem;
}
if (cfd.FileNames != null)
{
sia = cfd.ShellItemArray;
oFileNames = cfd.FileNames;
int nCount = ((System.Collections.ObjectModel.Collection<CFileDialog.FileNameType>)oFileNames).Count;
for (int n = 0; n < nCount; n++)
{
file = null;
var fn = ((System.Collections.ObjectModel.Collection<CFileDialog.FileNameType>)oFileNames).ElementAt(n);
try
{
file = await Windows.Storage.StorageFile.GetFileFromPathAsync(fn.FileName);
}
catch (Exception ex)
{
// For example for Hidden files
// Message = "Accès refusé. (0x80070005 (E_ACCESSDENIED))"
file = null;
}
if (file != null)
{
iconThumbnail = await file.GetThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.SingleItem, 32);
}
else
{
iconThumbnail = null;
}
var bi = new BitmapImage();
if (iconThumbnail != null)
bi.SetSource(iconThumbnail);
files.Add(new File(fn.FileName, fn.IsFolder?"Folder":"File", bi));
}
}
}
}
private async void btnChooseDirectory_Click(object sender, RoutedEventArgs e)
{
// This PC
string sFolder = "shell:::{20D04FE0-3AEA-1069-A2D8-08002B30309D}";
FOS nOptions = (FOS.FOS_FORCESHOWHIDDEN | FOS.FOS_PICKFOLDERS | FOS.FOS_NODEREFERENCELINKS);
string sFile = null;
object oFileNames = new System.Collections.ObjectModel.Collection<string>();
IShellItem si = null;
CFileDialog cfd = new CFileDialog(nOptions, "Choose directory", sFolder);
if ((cfd.ShowDialog(hWndMain, true) == DialogResult.OK))
{
files.Clear();
Windows.Storage.StorageFolder folder = null;
Windows.Storage.FileProperties.StorageItemThumbnail iconThumbnail = null;
if (cfd.FileName != null)
{
sFile = cfd.FileName;
si = cfd.ShellItem;
}
if (cfd.FileNames != null)
{
oFileNames = cfd.FileNames;
var fn = ((System.Collections.ObjectModel.Collection<CFileDialog.FileNameType>)oFileNames).ElementAt(0);
try
{
folder = await Windows.Storage.StorageFolder.GetFolderFromPathAsync(fn.FileName);
}
catch (Exception ex)
{
folder = null;
}
if (folder != null)
{
iconThumbnail = await folder.GetThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.SingleItem, 32);
}
else
{
iconThumbnail = null;
}
var bi = new BitmapImage();
if (iconThumbnail != null)
bi.SetSource(iconThumbnail);
files.Add(new File(fn.FileName, fn.IsFolder ? "Folder" : "File", bi));
}
}
}
private async void btnChooseFilesDirectories_Click(object sender, RoutedEventArgs e)
{
// This PC
string sFolder = "shell:::{20D04FE0-3AEA-1069-A2D8-08002B30309D}";
FOS nOptions = (FOS.FOS_FORCESHOWHIDDEN | FOS.FOS_ALLOWMULTISELECT | FOS.FOS_NODEREFERENCELINKS);
string sFile = null;
object oFileNames = new System.Collections.ObjectModel.Collection<string>();
IShellItem si = null;
IShellItemArray sia = null;
CFileDialog cfd = new CFileDialog(nOptions, "Choose files & directories", sFolder);
cfd.AddPushButton(ID_PUSHBUTTON, " Return selected items ");
if ((cfd.ShowDialog(hWndMain, true) == DialogResult.OK))
{
files.Clear();
Windows.Storage.StorageFile file = null;
Windows.Storage.StorageFolder folder = null;
Windows.Storage.FileProperties.StorageItemThumbnail iconThumbnail = null;
if (cfd.FileName != null)
{
sFile = cfd.FileName;
si = cfd.ShellItem;
}
if (cfd.FileNames != null)
{
sia = cfd.ShellItemArray;
oFileNames = cfd.FileNames;
int nCount = ((System.Collections.ObjectModel.Collection<CFileDialog.FileNameType>)oFileNames).Count;
for (int n = 0; n < nCount; n++)
{
file = null;
folder = null;
var fn = ((System.Collections.ObjectModel.Collection<CFileDialog.FileNameType>)oFileNames).ElementAt(n);
try
{
if (fn.IsFolder)
folder = await Windows.Storage.StorageFolder.GetFolderFromPathAsync(fn.FileName);
else
file = await Windows.Storage.StorageFile.GetFileFromPathAsync(fn.FileName);
}
catch (Exception ex)
{
file = null;
folder = null;
}
if (file != null || folder != null)
{
if (file != null)
iconThumbnail = await file.GetThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.SingleItem, 32);
if (folder != null)
iconThumbnail = await folder.GetThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.SingleItem, 32);
}
else
{
iconThumbnail = null;
}
var bi = new BitmapImage();
if (iconThumbnail != null)
bi.SetSource(iconThumbnail);
files.Add(new File(fn.FileName, fn.IsFolder ? "Folder" : "File", bi));
}
}
}
}
private async void btnSaveFile_Click(object sender, RoutedEventArgs e)
{
// Documents
string sFolder = "shell:::{A8CDFF1C-4878-43be-B5FD-F8091C1C60D0}";
FOS nOptions = (FOS.FOS_FORCESHOWHIDDEN | FOS.FOS_NODEREFERENCELINKS);
string sFile = null;
object oFileNames = new System.Collections.ObjectModel.Collection<string>();
IShellItem si = null;
CFileDialog cfd = new CFileDialog(nOptions, "Save file", sFolder);
HRESULT hr = cfd.SetFileTypes(cdfs, 1);
hr = cfd.SetDefaultExtension("jpg");
if ((cfd.ShowDialog(hWndMain, false) == DialogResult.OK))
{
files.Clear();
Windows.Storage.StorageFile file = null;
Windows.Storage.FileProperties.StorageItemThumbnail iconThumbnail = null;
if (cfd.FileName != null)
{
sFile = cfd.FileName;
si = cfd.ShellItem;
}
if (cfd.FileNames != null)
{
oFileNames = cfd.FileNames;
var fn = ((System.Collections.ObjectModel.Collection<CFileDialog.FileNameType>)oFileNames).ElementAt(0);
try
{
file = await Windows.Storage.StorageFile.GetFileFromPathAsync(fn.FileName);
}
catch (Exception ex)
{
file = null;
}
if (file != null)
{
iconThumbnail = await file.GetThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.SingleItem, 32);
}
else
{
iconThumbnail = null;
}
var bi = new BitmapImage();
if (iconThumbnail != null)
bi.SetSource(iconThumbnail);
files.Add(new File(fn.FileName, fn.IsFolder ? "Folder" : "File", bi));
}
}
}
private int WindowSubClass(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam, IntPtr uIdSubclass, uint dwRefData)
{
switch (uMsg)
{
case WM_APP_FILEDIALOG:
{
if (wParam == (IntPtr)ID_PUSHBUTTON)
{
CDialogEventSink cdes = Marshal.GetObjectForIUnknown(lParam) as CDialogEventSink;
CFileDialog pcfd = cdes.parentCFD;
IFileDialogCustomize pfdc = pcfd.m_pfdc;
IShellItemArray psia = null;
IntPtr ppvOut = IntPtr.Zero;
Guid SID_SFolderView = new Guid("cde725b0-ccc9-4519-917e-325d72fab4ce");
Guid IID_IForlderView = typeof(IFolderView2).GUID;
IntPtr pUnknown = Marshal.GetIUnknownForObject(pfdc);
HRESULT hr = IUnknown_QueryService(pUnknown, ref SID_SFolderView, ref IID_IForlderView, out ppvOut);
if ((hr == HRESULT.S_OK))
{
IFolderView2 pfv2 = (IFolderView2)Marshal.GetObjectForIUnknown(ppvOut);
hr = pfv2.GetSelection(true, ref psia);
if ((hr == HRESULT.S_OK))
{
int nCount = 0;
hr = psia.GetCount(ref nCount);
for (int i = 0; i <= nCount - 1; i++)
{
IShellItem si = null;
hr = psia.GetItemAt(i, ref si);
System.Text.StringBuilder sbResult = new System.Text.StringBuilder(MAX_PATH);
hr = si.GetDisplayName(SIGDN.SIGDN_FILESYSPATH, ref sbResult);
if ((hr == HRESULT.S_OK))
{
bool bFolder = false;
uint nAttributes = 0;
hr = si.GetAttributes(SFGAO_FOLDER | SFGAO_FILESYSTEM | SFGAO_STREAM, ref nAttributes);
if ((nAttributes & SFGAO_FOLDER) == SFGAO_FOLDER & (nAttributes & SFGAO_FILESYSTEM) == SFGAO_FILESYSTEM & (nAttributes & SFGAO_STREAM) != SFGAO_STREAM)
bFolder = true;
else
bFolder = false;
pcfd.FileNames.Add(new FileNameType() { FileName = sbResult.ToString(), IsFolder = bFolder });
}
else
{
hr = si.GetDisplayName(SIGDN.SIGDN_NORMALDISPLAY, ref sbResult);
if ((hr == HRESULT.S_OK))
{
bool bFolder = false;
uint nAttributes = 0;
hr = si.GetAttributes(SFGAO_FOLDER | SFGAO_FILESYSTEM | SFGAO_STREAM, ref nAttributes);
if ((nAttributes & SFGAO_FOLDER) == SFGAO_FOLDER & (nAttributes & SFGAO_FILESYSTEM) == SFGAO_FILESYSTEM & (nAttributes & SFGAO_STREAM) != SFGAO_STREAM)
bFolder = true;
else
bFolder = false;
pcfd.FileNames.Add(new FileNameType() { FileName = sbResult.ToString(), IsFolder = bFolder });
}
}
}
Marshal.ReleaseComObject(psia);
}
Marshal.ReleaseComObject(pfv2);
IFileDialog pfd;
pfd = (IFileDialog)pfdc;
pfd.Close((int)HRESULT.S_OK);
}
}
}
break;
}
return DefSubclassProc(hWnd, uMsg, wParam, lParam);
}
}
public class File
{
#region Properties
public string Name { get; set; }
public string Type { get; set; }
public BitmapImage biFileThumbnail { get; set; }
#endregion
public File(string sName, string sType, BitmapImage biThumbnail)
{
Name = sName;
Type = sType;
biFileThumbnail = biThumbnail;
}
}
}