用对话框浏览显示目录的树型结构


日期: 2002-01-03 14:00 | 联系我
关注我: Telegram, Twitter

本程序引入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)

使用上述类的方法如下:

  1. 在工程中包含shelltree.cpp和shelltree.h, shellpidl.cpp和shellpidl.h,filename.cpp和 filename.h

    #include "Shelltree.h" #include "shellpidl.h" #include "filename.h"

  2. 创建对话框。

  3. 在对话框中添加ShellTree类成员:

    CShellTree* m_TreeCtl;

  4. 在OnInitDialog()中,初始化树视控件:

    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();

  5. 必须创建回调TVN_ITEMEXPANDING的处理函数,否则,树视将会死掉。

    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具有相同的参数。

  6. 如果需要弹出菜单激活,创建OnRclick(),其弹出与Windows Explorer一样:

    void CTreeSelect::OnRclick(NMHDR* pNMHDR, LRESULT* pResult) { // TODO: Add your control notification handler code here m_TreeCtl->FolderPopup(pNMHDR,pResult); *pResult = 0; }

>>> DOWN !!! >>>下载源代码及演示程序


 文章评论
目前没有任何评论.

↓ 快抢占第1楼,发表你的评论和意见 ↓

当前页面是本站的 Google AMP 版本。
欲查看完整版本和发表评论请点击:完整版 »

 

程序员小辉 建站于 1997
Copyright © XiaoHui.com; 保留所有权利。