资源描述:
《Visual-C++遍历本地磁盘所有文件夹代码.docx》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、【例Ex_Tree】遍历本地磁盘所有的文件夹①用MFCAppWizard创建一个默认的单文档应用程序Ex_Tree,但在创建的第6步将视图的基类选择为CTreeView。②为CEx_TreeView类添加下列成员变量:classCEx_TreeView:publicCTreeView{public:CImageListm_ImageList;CStringm_strPath;//文件夹路径③为CEx_TreeView类添加成员函数InsertFoldItem,其代码如下:voidCEx_TreeView
2、::InsertFoldItem(HTREEITEMhItem,CStringstrPath){CTreeCtrl&treeCtrl=GetTreeCtrl();if(treeCtrl.ItemHasChildren(hItem))return;CFileFindfinder;BOOLbWorking=finder.FindFile(strPath);while(bWorking){bWorking=finder.FindNextFile();if(finder.IsDirectory()&&!find
3、er.IsHidden()&&!finder.IsDots())treeCtrl.InsertItem(finder.GetFileTitle(),0,1,hItem,TVI_SORT);}}④为CEx_TreeView类添加成员函数GetFoldItemPath,其代码如下:CStringCEx_TreeView::GetFoldItemPath(HTREEITEMhItem){CStringstrPath,str;strPath.Empty();CTreeCtrl&treeCtrl=GetTreeC
4、trl();HTREEITEMfolderItem=hItem;while(folderItem){intdata=(int)treeCtrl.GetItemData(folderItem);if(data==0)str=treeCtrl.GetItemText(folderItem);elsestr.Format("%c:\",data);strPath=str+"\"+strPath;folderItem=treeCtrl.GetParentItem(folderItem);}strPath=s
5、trPath+"*.*";returnstrPath;}①用ClassWizard为CEx_TreeView类添加TVN_SELCHANGED(当前选择节点改变后)消息处理,并增加下列代码:voidCEx_TreeView::OnSelchanged(NMHDR*pNMHDR,LRESULT*pResult){NM_TREEVIEW*pNMTreeView=(NM_TREEVIEW*)pNMHDR;HTREEITEMhSelItem=pNMTreeView->itemNew.hItem;//获取当前选择
6、的节点CTreeCtrl&treeCtrl=GetTreeCtrl();CStringstrPath=GetFoldItemPath(hSelItem);if(!strPath.IsEmpty()){InsertFoldItem(hSelItem,strPath);treeCtrl.Expand(hSelItem,TVE_EXPAND);}*pResult=0;}①在CEx_TreeView::PreCreateWindow函数中添加设置树控件样式代码:BOOLCEx_TreeView::PreCrea
7、teWindow(CREATESTRUCT&cs){cs.style
8、=TVS_HASLINES
9、TVS_LINESATROOT
10、TVS_HASBUTTONS;returnCTreeView::PreCreateWindow(cs);}②在CEx_TreeView::OnInitialUpdate函数中添加下列代码:voidCEx_TreeView::OnInitialUpdate(){CTreeView::OnInitialUpdate();CTreeCtrl&treeCtrl=GetTreeCtrl
11、();m_ImageList.Create(16,16,ILC_COLOR8
12、ILC_MASK,2,1);m_ImageList.SetBkColor(RGB(255,255,255));//消除图标黑色背景treeCtrl.SetImageList(&m_ImageList,TVSIL_NORMAL);//获取Windows文件夹路径以便获取其文件夹图标CStringstrPath;GetWindowsDirectory((LPTSTR)