-
Notifications
You must be signed in to change notification settings - Fork 149
/
FileDialog2.h
351 lines (241 loc) · 7.18 KB
/
FileDialog2.h
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
#pragma once
#include <atlbase.h>
#include <atlapp.h>
#include <atlwin.h>
#include <atldlgs.h>
#include <atlstr.h>
#include <vector>
using namespace std;
#include <atlcrack.h>
#include "BaseFunction.h"
#define ShellFileOperator (FOS_OVERWRITEPROMPT|FOS_NOCHANGEDIR|FOS_NOVALIDATE|FOS_ALLOWMULTISELECT|\
FOS_PATHMUSTEXIST|FOS_FILEMUSTEXIST|FOS_CREATEPROMPT|FOS_SHAREAWARE|FOS_NOREADONLYRETURN|\
FOS_NOTESTFILECREATE|FOS_NODEREFERENCELINKS|FOS_DONTADDTORECENT|FOS_FORCESHOWHIDDEN)
class CFileDialog2 : public CFileDialog
{
private:
CString lpstrFilter;
UINT FilterSpecCount;
const COMDLG_FILTERSPEC* ArrFilterSpec;
CString GetFilte(const COMDLG_FILTERSPEC* arrFilterSpec, UINT uFilterSpecCount)
{
CString lpstrFilter;
if (uFilterSpecCount)
{
for (int i = 0; i != uFilterSpecCount; i++)
{
lpstrFilter += arrFilterSpec[i].pszName;
lpstrFilter += L"(";
lpstrFilter += arrFilterSpec[i].pszSpec;
lpstrFilter += L")|";
lpstrFilter += arrFilterSpec[i].pszSpec;
lpstrFilter += L"|";
}
lpstrFilter.Replace(L'|', L'\0');
//return lpstrFilter;
}
/*else
{
return L"";
}*/
return lpstrFilter;
}
public:
CFileDialog2(BOOL bOpenFileDialog, // TRUE for FileOpen, FALSE for FileSaveAs
LPCTSTR lpszDefExt = L"",
LPCTSTR lpszFileName = L"",
DWORD dwFlags = 0/*OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT*/,
const COMDLG_FILTERSPEC* arrFilterSpec = NULL,
UINT uFilterSpecCount = 0U)
:ArrFilterSpec(arrFilterSpec)
, FilterSpecCount(uFilterSpecCount)
, CFileDialog(bOpenFileDialog, lpszDefExt, lpszFileName, dwFlags)
{
}
CString FilePath;
INT_PTR DoModal(HWND hWndParent = ::GetActiveWindow())
{
ComInitializeWrapper ComInitialize;
FilePath.ReleaseBufferSetLength(0);
//DWORD dwOptions = m_ofn.Flags&(OFN_OVERWRITEPROMPT | OFN_ALLOWMULTISELECT | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_CREATEPROMPT | OFN_SHAREAWARE);
INT_PTR ret = -1;
if (m_bOpenFileDialog)
{
//OpenFile
CShellFileOpenDialog OpenFile(m_szFileName, m_ofn.Flags&ShellFileOperator, m_ofn.lpstrDefExt, ArrFilterSpec, FilterSpecCount);
if (OpenFile.m_spFileDlg)
{
OpenFile.m_spFileDlg->SetFileTypeIndex(m_ofn.nFilterIndex);
ret = OpenFile.DoModal(hWndParent);
if (ret != IDOK)
return ret;
OpenFile.m_spFileDlg->GetFileTypeIndex((UINT*)&m_ofn.nFilterIndex);
CComPtr<IShellItemArray> pIShellItemArray;
OpenFile.m_spFileDlg->GetResults(&pIShellItemArray);
DWORD Count;
pIShellItemArray->GetCount(&Count);
IShellItem* pIShellItem;
for (int i = 0; i != Count; i++)
{
pIShellItemArray->GetItemAt(i, &pIShellItem);
CString Temp;
OpenFile.GetFileNameFromShellItem(pIShellItem, SIGDN_FILESYSPATH, Temp);
pIShellItem->Release();
FilePath += Temp;
FilePath.AppendChar(NULL);
}
return IDOK;
}
}
else
{
//SaveFile
CShellFileSaveDialog SaveFile(m_szFileName, m_ofn.Flags&ShellFileOperator, m_ofn.lpstrDefExt, ArrFilterSpec, FilterSpecCount);
if (SaveFile.m_spFileDlg)
{
SaveFile.m_spFileDlg->SetFileTypeIndex(m_ofn.nFilterIndex);
ret = SaveFile.DoModal(hWndParent);
if (ret != IDOK)
return ret;
SaveFile.m_spFileDlg->GetFileTypeIndex((UINT*)&m_ofn.nFilterIndex);
CString Temp;
SaveFile.GetFilePath(Temp);
FilePath += Temp;
FilePath.AppendChar(NULL);
return IDOK;
}
}
auto rFilter = GetFilte(ArrFilterSpec, FilterSpecCount);
m_ofn.lpstrFilter = rFilter;
m_ofn.lpstrFile = FilePath.GetBuffer((MAX_PATH + 1) * 1000);
m_ofn.nMaxFile = (MAX_PATH + 1) * 1000;
return CFileDialog::DoModal(hWndParent);
}
};
static void* Ptr;
class CFolderDialogEx : public CFileDialogImpl <CFolderDialogEx>
{
LONG_PTR pWindProcOld;
static LRESULT CALLBACK WindProcNew(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
CFolderDialogEx* pThis = (CFolderDialogEx*)Ptr;
LRESULT lResult = 1;
pThis->ProcessWindowMessage(hWnd, uMsg, wParam, lParam, lResult, 1);
if (lResult)
{
return CallWindowProcW((WNDPROC)pThis->pWindProcOld, hWnd, uMsg, wParam, lParam);
}
return 0;
}
public:
CString FolderPath;
CFolderDialogEx(LPCWSTR lpstrTitle = NULL, UINT uFlags = OFN_HIDEREADONLY | OFN_FORCESHOWHIDDEN)
: CFileDialogImpl<CFolderDialogEx>(TRUE, L"", L"", uFlags, L"1\0..")
{
m_ofn.lpstrTitle = lpstrTitle;
}
BEGIN_MSG_MAP_EX(CFolderDialogEx)
//NOTIFY_CODE_HANDLER(CDN_SELCHANGE, _OnFolderChange)
MSG_WM_INITDIALOG(OnInitDialog)
CHAIN_MSG_MAP(CFileDialogImpl<CFolderDialogEx>)
ALT_MSG_MAP(1)
COMMAND_HANDLER(IDOK, BN_CLICKED, OnOk)
break;
END_MSG_MAP()
void OnSelChange(LPOFNOTIFY lpon)
{
auto cch = GetFilePath(FolderPath.GetBuffer(1024), 1025) - 1;
if (cch > 0)
{
FolderPath.ReleaseBufferSetLength(cch);
GetParent().GetDlgItem(cmb13).SetWindowTextW(FolderPath);
}
return;
}
//LRESULT _OnFolderChange(int /*idCtrl*/, LPNMHDR pnmh, BOOL& /*bHandled*/)
//{
// OnFolderChange((LPOFNOTIFY)pnmh);
// return 0;
//}
BOOL OnInitDialog(CWindow wndFocus, LPARAM lInitParam)
{
SetMsgHandled(0);
CString FileTitle;
if (m_ofn.lpstrFileTitle == NULL || (*m_ofn.lpstrFileTitle) == NULL)
{
LoadString_s(L"Comdlg32.dll", 439, FileTitle);
/*if (FileTitle.GetLength() == 0)
{
FileTitle = L"Ñ¡ÔñÎļþ¼Ð";
}*/
GetParent().SetWindowTextW(FileTitle);
GetParent().GetDlgItem(IDOK).SetWindowTextW(FileTitle);
}
LoadString_s(L"Comdlg32.dll", 438, FileTitle);
/*if (FileTitle.GetLength() == 0)
FileTitle = L"Îļþ¼Ð";*/
GetParent().GetDlgItem(stc3).SetWindowTextW(FileTitle);
//GetParent().GetDlgItem(ctl1).ShowWindow(0);
GetParent().GetDlgItem(cmb1).ShowWindow(0);
//GetParent().GetDlgItem(cmb13).ShowWindow(0);
GetParent().GetDlgItem(stc2).ShowWindow(0);
//GetParent().GetDlgItem(stc3).ShowWindow(0);
Ptr = this;
pWindProcOld = GetParent().SetWindowLongPtr(GWLP_WNDPROC, (LONG_PTR)(&WindProcNew));
return TRUE;
}
LRESULT OnOk(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
//GetDlgItem(edt1).EnableWindow(0);
GetParent().GetDlgItem(cmb13).GetWindowTextW(FolderPath);
/*CString FilePath;
GetParent().GetDlgItem(cmb13).GetWindowTextW(FilePath);
if (FilePath.GetLength())
{
if (FolderPath[FolderPath.GetLength() - 1] != L'\\')
FolderPath += L'\\';
FolderPath += FilePath;
}*/
if (GetFileType(FolderPath)==PathIsDir)
{
::EndDialog(GetParent(), IDOK);
return 0;
}
else
{
return 0;
}
}
};
class CFolderDialog2 :public CFolderDialogEx
{
public:
CFolderDialog2(LPCWSTR lpstrTitle = NULL, UINT uFlags = OFN_HIDEREADONLY | OFN_FORCESHOWHIDDEN)
:CFolderDialogEx(lpstrTitle, uFlags)
{
}
INT_PTR DoModal(HWND hWndParent = ::GetActiveWindow())
{
ComInitializeWrapper ComInitialize;
CShellFileOpenDialog OpenFile(L"", FOS_PICKFOLDERS | (m_ofn.Flags&ShellFileOperator), L"");
if (OpenFile.m_spFileDlg == NULL)
{
return CFolderDialogEx::DoModal(hWndParent);
}
else
{
OpenFile.m_spFileDlg->SetTitle(m_ofn.lpstrTitle);
if (OpenFile.DoModal(hWndParent) == IDOK)
{
OpenFile.GetFilePath(FolderPath);
if(FolderPath.IsEmpty())
return IDCANCEL;
return IDOK;
}
else
{
return IDCANCEL;
}
}
}
};