本文介绍了创建类似目录树的 Windows 资源管理器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

目前,我正在使用 wxWidgets 的 wxGenericDirCtrl,它为我提供了目录树的用户界面.它看起来像这样:

Currently, I am using wxWidgets' wxGenericDirCtrl, which gives me a user interface to the directory tree. It looks like this:

但是,我更喜欢它看起来像这样:

However, I would prefer it to look like this:

与 wxWidgets 开发人员交谈时,他们建议继续进行的一种好方法是修改 wxGenericDirCtrl 类以使用本机 Windows 目录控件.我想自己将这个功能贡献给 wxWidgets,但我不确定从哪里开始,可以使用一些建议来帮助我开始.

Talking with the wxWidgets developers, they suggest that one good way to proceed is to modify the wxGenericDirCtrl class to use the native Windows directory control. I would like to contribute this functionality to wxWidgets myself, but I'm not sure where to start, and could use some suggestions to get me started.

问题:

  1. 在 C++ 中创建此类控件的正确本机方式是什么.我使用 MFC、.NET 还是其他格式?
  2. 是否有任何关键字可以帮助我找到这些信息?
  3. 是否有任何示例代码显示如何执行此操作?

推荐答案

自 Windows Vista 以来,创建类似于目录树的 Window Explorer(称为shell 命名空间树控件")变得非常容易.可以通过名为 INameSpaceTreeControl.Windows 7 添加了此类的较新版本,名为 INameSpaceTreeControl2.

Creating a Window Explorer like directory tree (known as "shell namespace tree control") has become quite easy since Windows Vista. This control can be created through a COM class called INameSpaceTreeControl. Windows 7 added a newer version of this class called INameSpaceTreeControl2.

可以从父窗口的 WM_CREATE 处理程序调用以下示例代码.它创建一个命名空间树控件,其根设置为桌面文件夹.其他根文件夹也是可能的,甚至可以插入多个根.

The following sample code could be called from the WM_CREATE handler of the parent window. It creates a namespace tree control that has its root set to the desktop folder. Other root folders are possible, even multiple roots can be inserted.

#include <ShlObj.h>   //Shell COM API
#include <atlbase.h>  //CComPtr

static CComPtr<INameSpaceTreeControl> pTree;
if( SUCCEEDED( pTree.CoCreateInstance( CLSID_NamespaceTreeControl ) ) )
{
    RECT rc{ 0, 0, 444, 333 }; // Client coordinates of the tree control
    if( SUCCEEDED( pTree->Initialize( hWndParent, &rc,
            NSTCS_HASEXPANDOS | NSTCS_AUTOHSCROLL | NSTCS_FADEINOUTEXPANDOS ) ) )
    {
        CComPtr<IShellItem> pItem;
        if( SUCCEEDED( SHCreateItemInKnownFolder( FOLDERID_Desktop, 0, nullptr,
                                                  IID_PPV_ARGS( &pItem ) ) ) )
        {
            pTree->AppendRoot( pItem, SHCONTF_FOLDERS, NSTCRS_EXPANDED, nullptr );
        }
    }
}

当父窗口被销毁时,通过调用 COM 对象的 Release() 方法来销毁命名空间树控件,通常在父窗口的 WM_DESTROY 处理程序中:

Destroy the namespace tree control by calling the Release() method of the COM object when the parent window gets destroyed, typically in the WM_DESTROY handler of the parent window:

pTree.Release();  // Releases the COM object and sets the pointer to nullptr

不要忘记在程序启动时CoInitialize(nullptr)和关闭前CoUninitialize().

Don't forget to CoInitialize(nullptr) once at startup of your program and CoUninitialize() before shutdown.

这篇关于创建类似目录树的 Windows 资源管理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 01:59