VC 选择文件路径窗口

界面样式 : [文件路径文本框] + [浏览按钮]

相应事件 : 

// 浏览
void DecSendData::OnBnClickedBtnScan()
{
    // TODO: 在此添加控件通知处理程序代码
    CString szPath = GetDirectoryPathA();
    if (szPath != "")
    {
        szDecSendDataPath = szPath;
        UpdateData(FALSE);
    }
}

内部函数 : 

// 获取选择的路径
CString DecSendData::GetDirectoryPathA()
{
    LPITEMIDLIST pidlRoot=NULL;
    SHGetSpecialFolderLocation(m_hWnd,CSIDL_DRIVES,&pidlRoot);
    BROWSEINFO bi;   //the input parameters needed
    CString strDisplayName;  
    bi.hwndOwner=GetSafeHwnd();   //get handle value of parent window 
    bi.pidlRoot=pidlRoot;   
    bi.pszDisplayName=strDisplayName.GetBuffer(MAX_PATH+1);   //get buffer pointer
    char szLan[32] = {0};
    sprintf(szLan, "%s", "文件夹");
    bi.lpszTitle=szLan;   //set title
    bi.ulFlags=BIF_RETURNONLYFSDIRS;   //set flag
    bi.lpfn=NULL;
    bi.lParam=0;
    bi.iImage=0;   //The above parameters are not related to the settings, better set up,
    LPITEMIDLIST lpIDList= SHBrowseForFolder(&bi);    //open dialog
    strDisplayName.ReleaseBuffer();   //correspond to GetBuffer()
    char pPath[MAX_PATH];
    CString Str;
    if (lpIDList)
    {
        SHGetPathFromIDList (lpIDList, pPath);
        Str=pPath;
    }
    return Str;
}

转载于:https://www.cnblogs.com/xuejianhui/articles/2741149.html

你可能感兴趣的:(VC 选择文件路径窗口)