MFC使用树控件显示磁盘所有目录
这是我在网上找到的代码,但是只能显示三层目录,怎样能获得磁盘下所有目录并用树控件实现? 代码如下:
BOOL CCFileTreeDlgDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
////////////////////////////////////////////////////////////////////////////////////////////
//读取磁盘文件
m_ImageList.Create(16,16,ILC_COLOR16,0,0);
m_treeFile.SetImageList(&m_ImageList,TVSIL_NORMAL);
m_treeFile.ModifyStyle(0L,TVS_HASLINES|TVS_HASBUTTONS|TVS_LINESATROOT);
size_t alldriver = ::GetLogicalDriveStrings(0,NULL);
_TCHAR *driverstr;
driverstr = new _TCHAR[alldriver + sizeof(_T(""))];
if(GetLogicalDriveStrings(alldriver,driverstr) != alldriver-1)
return FALSE;
_TCHAR *pdriverstr = driverstr;
size_t driversize = strlen(pdriverstr);
HTREEITEM disktree;
while(driversize>0)
{
SHGetFileInfo(pdriverstr,0,&fileinfo,sizeof(fileinfo),SHGFI_ICON);
imindex = m_ImageList.Add(fileinfo.hIcon);
disktree = m_treeFile.InsertItem(pdriverstr,imindex,imindex,TVI_ROOT,TVI_LAST);
pdriverstr += driversize+1;
driversize = strlen(pdriverstr);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
return TRUE; // return TRUE unless you set the focus to a control
}
void CCFileTreeDlgDlg::OnDblclkTREEFile(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
CFileFind filefd;
HTREEITEM parent;
HTREEITEM item = m_treeFile.GetSelectedItem();
if(m_treeFile.GetChildItem(item))return;
parent = item;
CString rootstr = m_treeFile.GetItemText(item);
CString temp;
CString lstr;
if(rootstr.Find("\\") == 2)
{
lstr.Format("%s*.*",rootstr);
}
else
{
CString strparent;
while(1)
{
parent = m_treeFile.GetParentItem(parent);
strparent = m_treeFile.GetItemText(parent);
if(strparent.Find("\\") == 2)
goto end;
//CString root = m_treeFile.GetItemText(parent);
//lstr.Format("%s%s%s\\*.*",root,temp,rootstr);
temp += strparent;
temp += "\\";
}
end:
CString root = m_treeFile.GetItemText(parent);
lstr.Format("%s%s%s\\*.*",root,temp,rootstr);
}
BOOL bfinded = filefd.FindFile(lstr);
while(bfinded)
{
bfinded = filefd.FindNextFile();
if(filefd.IsDirectory()&&!filefd.IsDots())
{
SHGetFileInfo(filefd.GetFilePath(),0,&fileinfo,sizeof(fileinfo),SHGFI_ICON);
imindex = m_ImageList.Add(fileinfo.hIcon);
m_treeFile.InsertItem(filefd.GetFileName(),imindex,imindex,item);
}
}
*pResult = 0;
}