-
Notifications
You must be signed in to change notification settings - Fork 0
/
UiFileDialogWnd.cpp
205 lines (184 loc) · 5.3 KB
/
UiFileDialogWnd.cpp
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
#include "UiFileDialogWnd.h"
//#include <InitGuid.h>
#include <IMzUnknown.h>
#include <IMzUnknown_IID.h>
#include <IFileBrowser.h>
//#include <IFileBrowser_GUID.h>
#include <cMzCommon.h>
using namespace cMzCommon;
#define MZ_IDC_TOOLBAR_MAIN 101
#define MZ_IDC_BUTTON_FOLDER 102
MZ_IMPLEMENT_DYNAMIC(UiFileDialogWnd)
BOOL UiFileDialogWnd::OnInitDialog(){
// Must all the Init of parent class first!
if (!CMzWndEx::OnInitDialog()) {
return FALSE;
}
int y = 0;
m_DialogTitle.SetPos(0,y,GetWidth(),MZM_HEIGHT_HEADINGBAR);
AddUiWin(&m_DialogTitle);
y += MZM_HEIGHT_HEADINGBAR;
m_FileName.SetPos(0,y,GetWidth(),MZM_HEIGHT_SINGLELINE_EDIT);
m_FileName.SetTextSize(20);
m_FileName.SetTip(L"请输入文件名");
if(_filename){
m_FileName.SetText(_filename);
}
m_FileName.SetRightInvalid(60);
AddUiWin(&m_FileName);
y += MZM_HEIGHT_SINGLELINE_EDIT;
m_FolderPath.SetPos(0,y,GetWidth(),MZM_HEIGHT_BUTTONEX);
m_FolderPath.SetText(L"文件夹:");
m_FolderPath.SetTextSize(25);
m_FolderPath.SetTextMaxLen2(0);
m_FolderPath.SetTextSize2(20);
m_FolderPath.SetButtonType(MZC_BUTTON_LINE_BOTTOM);
m_FolderPath.SetID(MZ_IDC_BUTTON_FOLDER);
AddUiWin(&m_FolderPath);
m_Toolbar.SetPos(0, GetHeight() - MZM_HEIGHT_TEXT_TOOLBAR, GetWidth(), MZM_HEIGHT_TEXT_TOOLBAR);
m_Toolbar.SetID(MZ_IDC_TOOLBAR_MAIN);
m_Toolbar.SetButton(0, true, true, L"取消");
m_Toolbar.SetButton(2, true, true, L"保存");
AddUiWin(&m_Toolbar);
m_FileName.SetFocus(true);
return TRUE;
}
void UiFileDialogWnd::OnMzCommand(WPARAM wParam, LPARAM lParam) {
UINT_PTR id = LOWORD(wParam);
switch (id) {
case MZ_IDC_BUTTON_FOLDER:
{
IFileBrowser *pFile = NULL;
CoInitializeEx(NULL, COINIT_MULTITHREADED );
IMzSave *pSave = NULL;
if ( SUCCEEDED( CoCreateInstance( CLSID_FileBrowser, NULL,CLSCTX_INPROC_SERVER ,IID_MZ_FileBrowser,(void **)&pFile ) ) )
{
if( SUCCEEDED( pFile->QueryInterface( IID_MZ_Save, (void**)&pSave ) ) )
{
pFile->SetParentWnd( m_hWnd );
pFile->SetOpenDocumentType(DOCUMENT_SAVE_FILES);
if(m_FolderPath.GetText2().IsEmpty()){
pFile->SetOpenDirectoryPath( L"\\Disk" );
}else{
pFile->SetOpenDirectoryPath( m_FolderPath.GetText2().C_Str() );
}
pFile->SetTitle(L"请选择文件夹");
if( pSave->Invoke() )
{
m_FolderPath.SetText2(pFile->GetSaveFileDirectory());
m_FolderPath.Invalidate();
}
pSave->Release();
}
pFile->Release();
}
CoUninitialize();
}
case MZ_IDC_TOOLBAR_MAIN:
{
int nIndex = lParam;
if(nIndex == 0){ //返回
EndModal(ID_CANCEL);
return;
}
if(nIndex == 2){ //保存
getfileInfo();
if(checkFileName()){
if(checkFileExist()){
if(::MzMessageBoxEx(m_hWnd,L"文件已经存在,是否要覆盖?",L"问题",MZ_YESNO,false) == 1){
EndModal(ID_OK);
}
}else{
EndModal(ID_OK);
}
}
return;
}
}
}
CMzWndEx::OnMzCommand(wParam,lParam);
}
void UiFileDialogWnd::SetTitle(LPCTSTR text){
if(text){
m_DialogTitle.SetText(text);
}
}
void UiFileDialogWnd::SetInitFileName(LPCTSTR text){
if(text){
C::newstrcpy(&_filename,text);
m_FileName.SetText(text);
}
}
void UiFileDialogWnd::SetInitFileSuffix(LPCTSTR text){
if(text){
C::newstrcpy(&_filesuffix,text);
m_FileName.SetTip2(text,true);
}
}
void UiFileDialogWnd::SetInitFolder(LPCTSTR text){
if(text){
m_FolderPath.SetText2(text);
}
}
//将控件中的数据取出
void UiFileDialogWnd::getfileInfo(){
//文件名
if(!m_FileName.GetText().IsEmpty()){
C::newstrcpy(&_filename,m_FileName.GetText().C_Str());
}else{
delete [] _filename;
_filename = NULL;
}
//文件名
if(!m_FolderPath.GetText2().IsEmpty()){
C::newstrcpy(&_folder,m_FolderPath.GetText2().C_Str());
}else{
delete [] _folder;
_folder = NULL;
}
}
/*
文件名中不允许使用下列字符:< > / \ | : " * ?
*/
bool UiFileDialogWnd::checkFileName(){
bool bRet = false;
if(_folder == NULL || lstrlen(_folder) == 0) return bRet;
int szfilename = lstrlen(_filename);
if(_filename == NULL || szfilename == 0) return bRet;
wchar_t forbiddenchars[] = {'<','>','/','\\','|',':','\"','*','?'};
int szconst = sizeof(forbiddenchars)/sizeof(forbiddenchars[0]);
wchar_t* pouter;
wchar_t* pinner;
int szouter,szinner;
if(szfilename > szconst){
pouter = _filename; szouter = szfilename;
pinner = forbiddenchars; szinner = szconst;
}else{
pinner = _filename; szinner = szfilename;
pouter = forbiddenchars; szouter = szconst;
}
for(int i = 0; i < szouter; i++){
for(int j = 0; j < szinner; j++){
if(pinner[j] == pouter[i]){
return bRet;
}
}
}
bRet = true;
return bRet;
}
/*
检查文件是否已经存在,如果存在则弹出确认对话框
*/
bool UiFileDialogWnd::checkFileExist(){
return File::FileExists(GetFullFileName());
}
LPTSTR UiFileDialogWnd::GetFullFileName() {
CMzString spath = _folder;
spath = spath + L"\\";
spath = spath + _filename;
spath = spath + _filesuffix;
LPTSTR file = NULL;
C::newstrcpy(&file,spath.C_Str());
return file;
}