本程序引入CShellTree、CFileName、CFilePidl等类,用于实现在对话框中显示外壳命名空间,参见下图。
CShellTree派生于CTreeCtrl,其函数包括:
// Initializes the treeview with "mycomputer" etc void PopulateTree(); // Initializes the treeview starting with a special folder as root // See the SHGetSpecialFolderLocation() for constants and descriptions void PopulateTree(int SpecialFolderID); // Must be called from OnItemExpanding(), message TVN_ITEMEXPANDING void OnFolderExpanding(NMHDR* pNMHDR, LRESULT* pResult); // Frees memory allocated for the shell object. message TVN_DELETEITEM void OnDeleteShellItem(NMHDR* pNMHDR, LRESULT* pResult); // Must be called from OnRclick(). message NM_RCLICK void GetContextMenu(NMHDR* pNMHDR, LRESULT* pResult); // Must be called from OnSelChanged(), message TVN_SELCHANGED BOOL OnFolderSelected(NMHDR* pNMHDR, LRESULT* pResult, CString &szFolderPath); // Enables Folder Images in the tree control. void EnableImages(); // Retrieves the path of the Selected Folder. BOOL GetSelectedFolderPath(CString &szFolderPath); // Opens the folder of the specified path. Does it's own error checking void TunnelTree(CString szFindPath)
使用上述类的方法如下:
#include "Shelltree.h" #include "shellpidl.h" #include "filename.h"
CShellTree* m_TreeCtl;
BOOL CTreeSelect::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here m_TreeCtl = (CShellTree*) GetDlgItem(IDC_SHELLTREE); //replace IDC_SHELLTREE with your control's ID ASSERT(m_TreeCtl); m_TreeCtl->EnableImages(); //enable images m_TreeCtl->PopulateTree(); //populate for the with Shell Folders for the first time return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE }
注意:CShellTree具备处理图像的能力,不用包含ImageList到CShellTree。
调用
m_TreeCtl->EnableImages();
创建和附加图像列表到树视控件,下面代码初始化树视控件:
m_TreeCtl->PopulateTree();
void CTreeSelect::OnItemexpanding(NMHDR* pNMHDR, LRESULT* pResult) { NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR; // TODO: Add your control notification handler code here m_TreeCtl->FolderExpanding(pNMHDR,pResult); *pResult = 0; }
FolderExpanding()方法添加目录节点到树视控件中,其与OnItemExpanding具有相同的参数。
void CTreeSelect::OnRclick(NMHDR* pNMHDR, LRESULT* pResult) { // TODO: Add your control notification handler code here m_TreeCtl->FolderPopup(pNMHDR,pResult); *pResult = 0; }
>>> DOWN !!! >>>下载源代码及演示程序