资源描述:
《vc控件treectrl与listctrl演示》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、VC控件TreeCtrl与ListCtrl演示这个例子类似于Windows的资源管理器,程序运行界面如图一所示:图一主要用到的类有:CListCtrl,CTreeCtrl,CImageList,CFileFind和函数SHGetFileInfo()简述步骤如下:1、增加TreeCtrl的TVS_HASBUTTONS,TVS_HASLINES、TVS_LINESATROOTStyle,代码如下:1.DWORDdwStyle=GetWindowLong(m_tree.m_hWnd,GWL_STYLE);2.dwS
2、tyle
3、=TVS_HASBUTTONS
4、TVS_HASLINES
5、TVS_LINESATROOT;3.SetWindowLong(m_tree.m_hWnd,GWL_STYLE,dwStyle);2、为TreeCtrl添加Root项:1.m_hRoot=m_tree.InsertItem("我的电脑");2.InsertItem()的函数原形为3.HTREEITEMInsertItem(LPCTSTRlpszItem,HTREEITEMhParent=TVI_ROOT, 4.HTREEITEMhInsert
6、After=TVI_LAST);3、获取本地逻辑驱动器,并添加:01.voidCTreeViewDlg::GetLogicalDrives(HTREEITEMhParent)02.{03. size_tszAllDriveStrings=GetLogicalDriveStrings(0,NULL);04.char*pDriveStrings=newchar[szAllDriveStrings+sizeof(_T(""))];05.GetLogicalDriveStrings(szAllDriveStr
7、ings,pDriveStrings);06.size_tszDriveString=strlen(pDriveStrings);07.while(szDriveString>0)08.{09. m_tree.InsertItem(pDriveStrings,hParent);10. pDriveStrings+=szDriveString+1;11. szDriveString=strlen(pDriveStrings);12.}13.}4、添加TVN_EXPANDED消息处理函数,当一项
8、展开时,为其子项添加下一级目录:01.voidCTreeViewDlg::OnItemexpandedTree(NMHDR*pNMHDR,LRESULT*pResult) 02.{03.NM_TREEVIEW*pNMTreeView=(NM_TREEVIEW*)pNMHDR;04.//TODO:Addyourcontrolnotificationhandlercodehere05.TVITEMitem=pNMTreeView->itemNew;06.if(item.hItem==m_hRoot)07.retu
9、rn;08. HTREEITEMhChild=m_tree.GetChildItem(item.hItem);09.while(hChild)10.{11. AddSubDir(hChild);12. hChild=m_tree.GetNextItem(hChild,TVGN_NEXT);13.}14.*pResult=0;15.}AddSubDir函数功能添加子项,具体代码见示例。5、添加TVN_SELCHANGED消息处理函数,在这个函数里,用GetFullPath()取得选中项的绝路径
10、(GetFullPath()具体代码看示例),在ListCtrl中添加文件而非文件夹的图标:01.voidCTreeViewDlg::OnSelchangedTree(NMHDR*pNMHDR,LRESULT*pResult) 02.{03.m_list.DeleteAllItems();04.NM_TREEVIEW*pNMTreeView=(NM_TREEVIEW*)pNMHDR;05.TVITEMitem=pNMTreeView->itemNew;06.if(item.hItem==m_hRoot)07.
11、return;08.CStringstr=GetFullPath(item.hItem);09. if(str.Right(1)!="\")10. str+="\";11.str+="*.*";12.CFileFindfile;13.BOOLbContinue=file.FindFile(str);14.while(bContinue)15.{16. bContinue=file