-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResExplorer.cpp
More file actions
345 lines (313 loc) · 8.87 KB
/
ResExplorer.cpp
File metadata and controls
345 lines (313 loc) · 8.87 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
// CResExplorer.cpp : 实现文件
//
#include "stdafx.h"
#include "MyExplorer2.h"
#include "ResExplorer.h"
// CResExplorer
IMPLEMENT_DYNAMIC(CResExplorer, CTreeCtrl)
CResExplorer::CResExplorer()
{
}
CResExplorer::~CResExplorer()
{
}
BEGIN_MESSAGE_MAP(CResExplorer, CTreeCtrl)
ON_NOTIFY_REFLECT(TVN_SELCHANGED, OnSelChanged)
ON_NOTIFY_REFLECT(TVN_ITEMEXPANDED, OnItemexpandedTreedir)
END_MESSAGE_MAP()
// CResExplorer 消息处理程序
void CResExplorer::Initial()
{
//创建ImageList
m_ImageList.Create(16,16,ILC_COLOR32,16,16);
//设置背景色
m_ImageList.SetBkColor( GetSysColor(COLOR_WINDOW) );
this->SetImageList(&m_ImageList,TVSIL_NORMAL); //设置TreeCtrl与ImageList关联
m_hRoot = this->InsertItem(_T("我的电脑"),0,0,TVI_ROOT);
GetLogicalDrives(m_hRoot); //获取本地驱动器加载到目录树
GetDriveDir(m_hRoot); //获取驱动器下一级文件夹到目录树
//Expand(GetRootItem(), TVE_EXPAND);
Expand(m_hRoot, TVE_EXPAND);
}
HTREEITEM CResExplorer::FindItemText(CTreeCtrl* tree,HTREEITEM item, CString strtext)
{
//深度遍历树
HTREEITEM hfind;
//空树,直接返回NULL
if(item == NULL)
return NULL;
//遍历查找
while(item!=NULL)
{
//当前节点即所需查找节点
if(tree->GetItemText(item) == strtext)
return item;
//查找当前节点的子节点
if(tree->ItemHasChildren(item))
{
item = tree->GetChildItem(item);
//递归调用查找子节点下节点
hfind = FindItemText(tree,item,strtext);
if(hfind)
{
return hfind;
}
else //子节点中未发现所需节点,继续查找兄弟节点
item = tree->GetNextSiblingItem(tree->GetParentItem(item));
}
else
{ //若无子节点,继续查找兄弟节点
item = tree->GetNextSiblingItem(item);
}
}
return item;
}
HTREEITEM CResExplorer::FindItem(HTREEITEM item,CString strPath)
{
//广度遍历树
HTREEITEM hfind;
if(strPath.Right(1) == "\\") //去除右侧'\'
{
strPath.Delete(strPath.GetLength()-1);
}
CString DirName;
int pos = strPath.Find('\\');
if(pos>0)
DirName = strPath.Left(pos);
else
DirName = strPath;
//遍历查找
while(item!=NULL)
{
CString curDir = this->GetItemText(item);
//去除最右边的"\"
if(curDir.Right(1) == "\\")
{
curDir.Delete(curDir.GetLength()-1);
}
//当前节点即所需查找节点
if(curDir.CompareNoCase(DirName) == 0)
{
if(curDir.CompareNoCase(strPath) == 0)
return item;
//如果找到节点,继续往下找子节点
else if(this->ItemHasChildren(item))
{
item = this->GetChildItem(item);
//递归调用查找子节点下节点
hfind = FindItem(item,strPath.Mid(pos+1));
if(hfind)
{
return hfind;
}
}
return NULL; //未查找到
}
else
{
//查找兄弟节点
if(this->GetNextSiblingItem(item) != NULL)
{
item = this->GetNextSiblingItem(item);
}
//若无兄弟节点,继续查找子节点
else if(this->ItemHasChildren(item))
{
item = this->GetChildItem(item);
}
else
return NULL; //未查找到
hfind = FindItem(item,strPath);
if(hfind)
{
return hfind;
}
}
}
return item;
}
// 获取盘符到目录树,作为根目录下一级节点
void CResExplorer::GetLogicalDrives(HTREEITEM hRoot)
{
size_t szAllDrivesStrings = GetLogicalDriveStrings(0,NULL); //获取驱动器字符长度
TCHAR* pDriveStrings = new TCHAR[szAllDrivesStrings + sizeof(_T(""))]; //定义字符串存储驱动器字符pDriveStrings
GetLogicalDriveStrings((DWORD)szAllDrivesStrings,pDriveStrings); //获取驱动器字符到pDriveStrings
//截取单个驱动器字符出来添加到目录树上
//定义临时字符指针指向驱动器字符信息
//(为了最后可以释放驱动器字符串方便,如果直接移动字符串指针,后面调用delete释放字符串资源的时候会报错)
TCHAR* pTempDrive = pDriveStrings;
size_t szDriveString = _tcslen(pTempDrive);
while(szDriveString>0)
{
//this->InsertItem(pTempDrive,hRoot); //加载解析后的一个驱动器信息到目录树
InsertItemToTree(hRoot,pTempDrive,pTempDrive);
pTempDrive += szDriveString + 1;
szDriveString = _tcslen(pTempDrive);
}
delete pDriveStrings;
}
// 获取驱动器下一级目录到目录树上显示
void CResExplorer::GetDriveDir(HTREEITEM hRoot)
{
HTREEITEM hChild = this->GetChildItem(hRoot); //获取根节点下的第一个盘符节点
while(hChild)
{
//获取查找路径
CString strText = this->GetItemText(hChild);
if(strText.Right(1) != _T("\\"))
{
strText += _T("\\");
}
strText += "*.*";
CFileFind fileFind;
BOOL bContinue = fileFind.FindFile(strText);
while(bContinue)
{
bContinue = fileFind.FindNextFile();
//磁盘下的文件是目录并且不是[.]或[..]的时候,加载到目录树
if(fileFind.IsDirectory() && !fileFind.IsDots())
{
CString tempPath = strText;
int index = tempPath.Find(_T("*.*"));
tempPath.Delete(index,3);
InsertItemToTree(hChild,tempPath+fileFind.GetFileName(),fileFind.GetFileName());
}
}
//到下一个驱动器
hChild = this->GetNextItem(hChild,TVGN_NEXT);
}
}
// 根据目录树中任一节点获取其所有子目录
void CResExplorer::AddSubDir(HTREEITEM hParent)
{
CString strPath = GetFullPath(hParent);
if(strPath.Right(1) != "\\")
{
strPath += "\\";
}
strPath += "*.*";
CFileFind fileFind;
BOOL bContinue = fileFind.FindFile(strPath);
while(bContinue)
{
bContinue = fileFind.FindNextFile();
if(fileFind.IsDirectory() && !fileFind.IsDots()&&!fileFind.IsHidden())
{
//this->InsertItem(fileFind.GetFileName(),hParent);
CString tempPath = strPath;
int index = tempPath.Find(_T("*.*"));
strPath.Delete(index,3);
CString sname = fileFind.GetFileName();
InsertItemToTree(hParent,tempPath+sname,sname);
}
}
}
// 获取某节点的文件路径
CString CResExplorer::GetFullPath(HTREEITEM hCurrent)
{
CString strTemp = _T("");
CString strFullPath = _T("");
while(hCurrent != m_hRoot)
{
//从当前节点找起,依次找出其父节点,到根节点结束
strTemp = this->GetItemText(hCurrent);
if(strTemp.Right(1) != _T("\\"))
strTemp += _T("\\");
strFullPath = strTemp + strFullPath;
hCurrent = this->GetParentItem(hCurrent);
}
return strFullPath;
}
// 展开某节点触发事件
void CResExplorer::OnItemexpandedTreedir(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);
// TODO: 在此添加控件通知处理程序代码
TVITEM item = pNMTreeView->itemNew; //获取当前展开节点
//如果当前展开节点为根节点,则返回
if(item.hItem == m_hRoot)
return;
HTREEITEM hChild = this->GetChildItem(item.hItem);
if(pNMTreeView->action == 1) //收起
{
while(hChild!=NULL)
{
while(this->ItemHasChildren(hChild))
{
HTREEITEM item = this->GetChildItem(hChild);
this->DeleteItem(item);
}
hChild = this->GetNextItem(hChild,TVGN_NEXT);
}
}
else if(pNMTreeView->action == 2) //展开
{
//轮训展开节点的每个子节点,加载文件目录信息到子节点上
while(hChild!=NULL)
{
AddSubDir(hChild);
hChild = this->GetNextItem(hChild,TVGN_NEXT);
}
}
*pResult = 0;
}
void CResExplorer::OnSelChanged(NMHDR* pNMHDR, LRESULT* pResult)
{
this->Expand(this->GetSelectedItem(),TVE_EXPAND);
LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);
// TODO: 在此添加控件通知处理程序代码
// m_lstRight->DeleteAllItems(); //删除ListView中所有元素
// //获取当前目录路径
// TVITEM item = pNMTreeView->itemNew;
// if(item.hItem == m_hRoot)
// {
// m_edtAddress->SetWindowText(_T(""));
// return;
// }
// CString strDirPath = GetFullPath(item.hItem);
// m_edtAddress->SetWindowText(strDirPath); //返回现实到地址栏
// if(strDirPath.Right(1)=="\\")
// {
// strDirPath += "*.*";
// }
// //查找当前目录下的所有文件添加到ListView中
// CFileFind fileFind;
// BOOL bContinue = fileFind.FindFile(strDirPath);
// int iNum = 0;
// while(bContinue)
// {
// bContinue = fileFind.FindNextFile();
// if(/*!fileFind.IsDirectory() && */!fileFind.IsDots()&&!fileFind.IsHidden())
// {
// SHFILEINFO fileInfo;
// CString tempPath = strDirPath;
// int index = tempPath.Find(_T("*.*"));
// tempPath.Delete(index,3);
// SHGetFileInfo(tempPath + fileFind.GetFileName(),0,&fileInfo,sizeof(&fileInfo),SHGFI_ICON|SHGFI_DISPLAYNAME|SHGFI_TYPENAME);
// int iIcon = m_lstRight->GetImageList(LVSIL_SMALL)->Add(fileInfo.hIcon);
// int ret = m_lstRight->InsertItem(iNum,fileInfo.szDisplayName,iIcon);
//
// m_lstRight->SetItemText(iNum,2,fileInfo.szTypeName);
//
// //声明文件属性变量
// CFileStatus status;
// CFile::GetStatus(tempPath + fileFind.GetFileName(), status);
// CString strSize;
// strSize.Format(_T("%d B"),status.m_size);
// m_lstRight->SetItemText(iNum,1,strSize);
//
// CString strModifyTime = status.m_ctime.Format("%Y-%m-%d %H:%M:%S");
// m_lstRight->SetItemText(iNum,3,strModifyTime);
//
// iNum++;
// }
// }
*pResult = 0;
}
HTREEITEM CResExplorer::InsertItemToTree(HTREEITEM hParent, CString strPath,CString DisplayName)
{
SHFILEINFO fileInfo;
SHGetFileInfo(strPath,0,&fileInfo,sizeof(&fileInfo),SHGFI_ICON|SHGFI_DISPLAYNAME|SHGFI_TYPENAME);
int iIcon = m_ImageList.Add(fileInfo.hIcon);
return this->InsertItem(/*fileInfo.szDisplayName*/DisplayName,iIcon,iIcon,hParent);
}