白雪公主的后妈 2023-04-09 17:30 采纳率: 26.3%
浏览 55
已结题

mfc如何通过树控件显示各种文件图标

mfc如何通过树控件显示各种文件图标,

img


BOOL CTree_fileDlg::OnInitDialog()
{
    CDialogEx::OnInitDialog();
    // TODO: 在此添加额外的初始化代码
///////////////////////////////////

    //Left_cwnd = this;
    m_imageList.Create(32, 32, ILC_COLOR32|ILC_MASK, 0, 0);
    m_imageList.SetBkColor(RGB(255,255,255));
    m_TreeControl.SetImageList(&m_imageList, TVSIL_NORMAL);
///////////////////////////////////
    return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
}

/////////////////////////////////

CString CTree_fileDlg::SelectDirectory(HWND hwnd)
{
    CString strPath;
    TCHAR szBuf[1024] = { 0 };
    memset(szBuf, 0, sizeof(szBuf));
    BROWSEINFO bInfo;
    bInfo.hwndOwner = hwnd;
    bInfo.pidlRoot = NULL;
    bInfo.pszDisplayName = szBuf;
    bInfo.lpszTitle = _T("选择位置");
    bInfo.ulFlags= BIF_EDITBOX;
    bInfo.lpfn = NULL;
    bInfo.iImage = 0;
    LPITEMIDLIST lp = SHBrowseForFolder(&bInfo);
    if (lp == NULL)
        return NULL;
    if (lp&&SHGetPathFromIDList(lp, szBuf))
    {
        strPath = szBuf;
    }
    else
    {
        strPath = _T("");
    }
    m_Path = strPath;
    SetDlgItemText(IDC_EDIT_CHOOSE, m_Path);
    return strPath;
}

void CTree_fileDlg::CreateTreemanager(HTREEITEM Root,CString path)
{
    CFileFind finder;
    HTREEITEM hFatherItem, hSonItem;
    BOOL exit = finder.FindFile(path + _T("\\*.*"));
    while (exit)
    {
        exit = finder.FindNextFile();
        if (finder.IsDirectory() && !finder.IsDots())
        {
            CString mDir = finder.GetFileTitle();
            SHGetFileInfo(finder.GetFilePath(),0,&shfileInfo,sizeof(shfileInfo),SHGFI_ICON);
            //SHGetFileInfo(finder.GetFilePath(), 0, &shfileInfo, sizeof(shfileInfo), SHGFI_ICON| SHGFI_SMALLICON | SHGFI_SYSICONINDEX | SHGFI_USEFILEATTRIBUTES );
            index = m_imageList.Add(shfileInfo.hIcon);
            hFatherItem = m_TreeControl.InsertItem(mDir, index, index, Root, TVI_LAST);
            mDir = path + _T("\\") + mDir;
            CreateTreemanager(hFatherItem, mDir);            
        }
        else if (!finder.IsDirectory() && !finder.IsDots())
        {
            CString mDir = finder.GetFileTitle();
            //SHGFI_SMALLICON | SHGFI_SYSICONINDEX | SHGFI_USEFILEATTRIBUTES | (bSelected ? SHGFI_OPENICON : 0
            //SHGetFileInfo(mDir, 0, &shfileInfo, sizeof(shfileInfo), SHGFI_USEFILEATTRIBUTES|SHGFI_SYSICONINDEX|SHGFI_ICON);
            SHGetFileInfo(mDir, 0, &shfileInfo, sizeof(shfileInfo), SHGFI_ICON | SHGFI_SMALLICON | SHGFI_SYSICONINDEX | SHGFI_USEFILEATTRIBUTES);
            index = m_imageList.Add(shfileInfo.hIcon);
            hSonItem = m_TreeControl.InsertItem(mDir, index, index, Root, TVI_LAST);
        }
    }
    finder.Close();
}

void CTree_fileDlg::OnBnClickedButtonOnloard()
{
    // TODO: 在此添加控件通知处理程序代码
    HTREEITEM hRoot;
    m_TreeControl.DeleteAllItems();
    m_Path.Empty();
    CString path = SelectDirectory(m_hWnd);
    if (path == "NULL")
        return;
    m_Path = path;
    UpdateData(FALSE);
    SHGetFileInfo(m_Path, 0, &shfileInfo, sizeof(shfileInfo), SHGFI_SYSICONINDEX | SHGFI_ICON);
    index = m_imageList.Add(shfileInfo.hIcon);
    hRoot = m_TreeControl.InsertItem(path, index, index, TVI_LAST);
    CreateTreemanager(hRoot, m_Path);
    UpdateData(FALSE);
}


  • 写回答

2条回答 默认 最新

  • 阿里嘎多学长 2023-04-09 18:07
    关注

    以下内容部分参考ChatGPT模型:


    通过使用SHGetFileInfo函数获取文件的系统图标,将其添加到ImageList中,再通过树控件的InsertItem函数将文件夹和文件的名称和对应的图标插入到树控件中即可。示例代码如下:

    BOOL CMyDlg::OnInitDialog()
    {
        CDialogEx::OnInitDialog();
    
        // 创建ImageList
        m_ImageList.Create(16, 16, ILC_COLOR32 | ILC_MASK, 0, 0);
        SHFILEINFO shfi;
        SHGetFileInfo(_T("C:\\"), FILE_ATTRIBUTE_DIRECTORY, &shfi, sizeof(SHFILEINFO), SHGFI_ICON | SHGFI_SMALLICON);
        m_ImageList.Add(shfi.hIcon);
    
        // 绑定ImageList到树控件
        m_TreeCtrl.SetImageList(&m_ImageList, TVSIL_NORMAL);
    
        // 加载根目录下的所有文件和文件夹
        LoadDirectory(_T("C:\\"), TVI_ROOT);
    
        return TRUE;
    }
    
    void CMyDlg::LoadDirectory(LPCTSTR lpszPath, HTREEITEM hParent)
    {
        CFileFind finder;
        BOOL bWorking = finder.FindFile(lpszPath + _T("\\*.*"));
        while (bWorking)
        {
            bWorking = finder.FindNextFile();
    
            if (finder.IsDots())
                continue;
    
            if (finder.IsDirectory())
            {
                // 获取文件夹的系统图标
                SHFILEINFO shfi;
                SHGetFileInfo(finder.GetFilePath(), FILE_ATTRIBUTE_DIRECTORY, &shfi, sizeof(SHFILEINFO), SHGFI_ICON | SHGFI_SMALLICON);
                int nImage = m_ImageList.Add(shfi.hIcon);
    
                // 在树控件中插入文件夹
                HTREEITEM hItem = m_TreeCtrl.InsertItem(finder.GetFileName(), nImage, nImage, hParent);
                LoadDirectory(finder.GetFilePath(), hItem);
            }
            else
            {
                // 获取文件的系统图标
                SHFILEINFO shfi;
                SHGetFileInfo(finder.GetFilePath(), 0, &shfi, sizeof(SHFILEINFO), SHGFI_ICON | SHGFI_SMALLICON);
                int nImage = m_ImageList.Add(shfi.hIcon);
    
                // 在树控件中插入文件
                m_TreeCtrl.InsertItem(finder.GetFileName(), nImage, nImage, hParent);
            }
        }
    
        finder.Close();
    }
    

    如果我的建议对您有帮助、请点击采纳、祝您生活愉快

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 4月9日
  • 创建了问题 4月9日