这个例子类似于 Windows 的资源管理器程序运行界面如图一所示 主要用到的类有 CListCtrlCTreeCtrlCImageListCFileFind 和函数SHGetFileInfo() 简述步骤如下 增加 TreeCtrl 的 TVS_HASBUTTONSTVS_HASLINESTVS_LINESATROOT Style代码如下 DWORD dwStyle = GetWindowLong(m_treem_hWndGWL_STYLE); dwStyle |= TVS_HASBUTTONS | TVS_HASLINES | TVS_LINESATROOT; SetWindowLong(m_treem_hWndGWL_STYLEdwStyle); 为TreeCtrl添加Root项 m_hRoot = m_treeInsertItem(我的电脑); InsertItem()的函数原形为 HTREEITEM InsertItem( LPCTSTR lpszItem HTREEITEM hParent = TVI_ROOT HTREEITEM hInsertAfter = TVI_LAST ); 获取本地逻辑驱动器并添加void CTreeViewDlg::GetLogicalDrives(HTREEITEM hParent) { size_t szAllDriveStrings = GetLogicalDriveStrings(NULL); char *pDriveStrings = new char[szAllDriveStrings + sizeof(_T())]; GetLogicalDriveStrings(szAllDriveStringspDriveStrings); size_t szDriveString = strlen(pDriveStrings); while(szDriveString > ) { m_treeInsertItem(pDriveStringshParent); pDriveStrings += szDriveString + ; szDriveString = strlen(pDriveStrings); } } 添加TVN_EXPANDED消息处理函数当一项展开时为其子项添加下一级目录void CTreeViewDlg::OnItemexpandedTree(NMHDR* pNMHDR LRESULT* pResult) { NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR; // TODO: Add your control notification handler code here TVITEM item = pNMTreeView>itemNew; if(itemhItem == m_hRoot) return; HTREEITEM hChild = m_treeGetChildItem(itemhItem); while(hChild) { AddSubDir(hChild); hChild = m_treeGetNextItem(hChildTVGN_NEXT); } *pResult = ; } AddSubDir函数功能添加子项具体代码见示例 添加TVN_SELCHANGED消息处理函数在这个函数里用GetFullPath()取得选中项的绝 路径(GetFullPath()具体代码看示例)在ListCtrl中添加文件而非文件夹的图标 void CTreeViewDlg::OnSelchangedTree(NMHDR* pNMHDR LRESULT* pResult) { m_listDeleteAllItems(); NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR; TVITEM item = pNMTreeView>itemNew; if(itemhItem == m_hRoot) return; CString str = GetFullPath(itemhItem); if(strRight() != \\) str += \\; str += **; CFileFind file; BOOL bContinue = fileFindFile(str); while(bContinue) { bContinue = fileFindNextFile(); if(!fileIsDirectory() && !fileIsDots()) { SHFILEINFO info; CString temp = str; int index = tempFind(**); tempDelete(index); SHGetFileInfo(temp + fileGetFileName() &infosizeof(&info) SHGFI_DISPLAYNAME | SHGFI_ICON); int i = m_ImageListAdd(infohIcon); m_listInsertItem(iinfoszDisplayNamei); } } *pResult = ; } 这只是一个简单的例子你可以在 ListCtrl 中添加鼠标双击消息的处理函数用 Process 打开该选中的文件; 该示例在VCxp下编译通过 VC初学者如有不足之处请来信指教(way) |