【MFC】BROWSEINFO 设置路径并显示,默认引导至源路径


void CTestDlg::SetFolderPath(UINT uid)
{
    BROWSEINFO   bi;   
    char   szPath[MAX_PATH];   
    LPITEMIDLIST   pList   =   NULL;   
    ZeroMemory(szPath,   MAX_PATH);  

    //获取当前路径
    GetDlgItemText(uid, szPath, MAX_PATH);
     // 配置路径对话框
    memset(&bi, 0, sizeof(BROWSEINFO));

    bi.hwndOwner   =  m_hWnd;
    bi.pidlRoot   =   pList;   
    bi.pszDisplayName   =   szPath;   

    bi.lpszTitle   =  "选择路径";   
    bi.ulFlags   =   BIF_EDITBOX;   
    bi.lpfn   =   BrowseCallbackProc;   
    bi.lParam   =   (LPARAM)szPath; 
    bi.iImage   =   0;

    //弹出选择目录对话框 
    if   ((pList   =   SHBrowseForFolder(&bi))   !=   NULL)   
    {    
        if (SHGetPathFromIDList(pList,   szPath))
        {
            SetDlgItemText(uid, szPath);
        }
        
    }
}

// 回调函数,系统默认的是如果该路径下有子路径,则默认展开子路径,但是其实大部分时间我们是不需要的

//这里根据某网友的例子修改的, 不展开路径的子路径

static int CALLBACK BrowseCallbackProc(HWND hWnd, UINT uMsg, LPARAM , LPARAM lpData)   
{
    if(uMsg == BFFM_INITIALIZED)
    {
        CTreeCtrl	treePath;
        HTREEITEM	hItemSel;
        ::SendMessage(hWnd, BFFM_SETSELECTION, TRUE, lpData);
        treePath.SubclassWindow(::GetDlgItem(hWnd, 0x3741));
        hItemSel	= treePath.GetSelectedItem();
        treePath.Expand(hItemSel, TVE_COLLAPSE);
        treePath.UnsubclassWindow();
    } 
    return 0;  
}
调用方式

SetFolderPath(IDC_EDIT_LOG_PATH);    



你可能感兴趣的:(【MFC】BROWSEINFO 设置路径并显示,默认引导至源路径)