【代码备忘】MFC遍历文件夹、删除文件夹、CTreeCtrl使用方法
2014-04-02 10:45
405 查看
欢迎加入我们的QQ群,无论你是否工作,学生,只要有c / vc / c++ 编程经验,就来吧!158427611
所贴代码皆有一些工程代码,会对应标出!
【遍历文件夹 & CtreeCtrl插入节点】
【删除文件夹】
【CtreeCtrl的右键响应 & 获取右键对应节点】
欢迎加入我们的QQ群,无论你是否工作,学生,只要有c / vc / c++ 编程经验,就来吧!158427611
所贴代码皆有一些工程代码,会对应标出!
【遍历文件夹 & CtreeCtrl插入节点】
void CDirTraversal::FindDir(HTREEITEM pItem , const CString &dirPath )//HTREEITEM 为一个CtreeCtel节点,此处实现将文件夹映射到CtreeCtrl控件中去 { CFileFind tempFind; BOOL bFound = tempFind.FindFile(dirPath + "\\*.*"); HTREEITEM pItem2; if(pItem == NULL) { pItem2 = m_treeCtrl->InsertItem(dirPath,0,1,TVI_ROOT,TVI_LAST);//插入根节点 } else { pItem2 = m_treeCtrl->InsertItem(dirPath.Right(dirPath.GetLength() - dirPath.ReverseFind('\\') - 1),0,1,pItem,TVI_LAST);//插入子节点 } while(bFound) { bFound=tempFind.FindNextFile(); if(tempFind.IsDots()) continue; tempFind.IsDirectory() ? FindDir(pItem2,tempFind.GetFilePath()) : //找到的是文件夹 FindFile(pItem2,tempFind.GetFileName()); //找到的是文件 } tempFind.Close(); } void CDirTraversal::FindFile( HTREEITEM pItem ,const CString &filePath ) { m_treeCtrl->InsertItem(filePath,0,1,pItem,TVI_LAST); //插入子节点 }
【删除文件夹】
BOOL COEMToolDlg::DeleteDirectory(const CString &DirName) { CFileFind tempFind; BOOL IsFinded = tempFind.FindFile(DirName + "\\*.*"); while(IsFinded) { IsFinded = tempFind.FindNextFile(); if(tempFind.IsDots()) continue; if(tempFind.IsDirectory()) { DeleteDirectory(tempFind.GetFilePath()); } else { DeleteFile(tempFind.GetFilePath()); } } tempFind.Close(); if(!RemoveDirectory(DirName)) { MessageBox(L"删除目录失败!",L"警告信息",MB_OK); return FALSE; } return TRUE; }
【CtreeCtrl的右键响应 & 获取右键对应节点】
void COEMToolDlg::OnNMRClickTree_RCLICK(NMHDR *pNMHDR, LRESULT *pResult) { CPoint point; GetCursorPos(&point); //获取鼠标坐标 m_dirTree_treeCtrl.ScreenToClient(&point);//映射到CtreeCtrl中 UINT nFlags; HTREEITEM hItems= m_dirTree_treeCtrl.HitTest(point, &nFlags);//根据坐标获取节点 m_treeChangeFile = m_dirTree_treeCtrl.GetItemText(hItems); hItems = m_dirTree_treeCtrl.GetParentItem(hItems); while(hItems) //循环获取父节点,获取节点全路径 { m_treeChangeFile = m_dirTree_treeCtrl.GetItemText(hItems) + L"\\" + m_treeChangeFile; hItems = m_dirTree_treeCtrl.GetParentItem(hItems); } // 菜单显示 CMenu menu; menu.LoadMenu(IDR_MENU_TREE); CMenu *pPopup=menu.GetSubMenu(0); POINT pt; ::GetCursorPos((LPPOINT)&pt); pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, pt.x, pt.y,this); *pResult = 0; }
欢迎加入我们的QQ群,无论你是否工作,学生,只要有c / vc / c++ 编程经验,就来吧!158427611
相关文章推荐
- 递归的一些方法使用:深度遍历路径中的所有文件和目录,删除一个有文件的文件夹
- ANT使用 - 用for和foreach的方法遍历一个文件夹,查找到某个文件并删除
- [置顶] MFC树形控件CTreeCtrl使用方法、遍历、SetCheck无效、根据名称获取节点、获取选中节点等问题
- MFC ComboBox 使用方法-初始化,添加,得到选择的数据,得到数据的位置,删除
- Ant步步为营(5)用for和foreach的方法遍历一个文件夹,查找到某个文件并删除
- Win10中使用CMD命令遍历文件夹删除重复文件
- 【转】MFC和批处理方法遍历文件夹
- 使用批处理按要求批量删除文件夹方法汇总
- WSS V3 中用代码遍历列表的方法(支持文件夹的列表的项目遍历)
- PHP使用glob方法遍历文件夹下所有文件的实例
- PHP使用glob函数遍历目录或文件夹的方法
- Android第一行代码阅读笔记001 主要文件夹介绍、activity的注册、资源引用的方法、Log日志打印的使用
- Java中使用遍历文件夹的2种方法
- Python 使用os.remove删除文件夹时报错的解决方法
- 使用批处理按要求批量删除文件夹方法汇总
- 代码遍历列表的方法(支持文件夹的列表的项目遍历)
- VC MFC 常用控件使用方法-- CTreeCtrl
- Eclipse恢复已删除的文件和代码、svn使用了还原,但本地的没有提交找回没提交代码的方法
- 使用PHP遍历文件夹与子目录的函数代码
- Java中使用遍历文件夹的2种方法