MFC 选择文件夹按钮

CString GetFolderFullpath(LPCTSTR lpszDefault)

{

    TCHAR buffDisplayName[MAX_PATH];

    TCHAR fullpath[MAX_PATH];

    BROWSEINFO  browseinfo;

    LPITEMIDLIST lpitemidlist;

 

    ZeroMemory(&browseinfo, sizeof( BROWSEINFO ));

    browseinfo.pszDisplayName = buffDisplayName ;

    browseinfo.lpszTitle = _T("请选择目录");

    browseinfo.ulFlags = BIF_RETURNONLYFSDIRS;

    browseinfo.lParam = (LPARAM)lpszDefault;

    browseinfo.lpfn = _BrowseCallbackProc;

 

    if(!(lpitemidlist = SHBrowseForFolder(&browseinfo)))

    {

        AfxMessageBox(_T("没有选择目录"));

        return CString(_T(""));

    }

    else

    {

        SHGetPathFromIDList(lpitemidlist, fullpath);      

        CoTaskMemFree(lpitemidlist);

        return CString(fullpath);

    }

}

 

下面代码,可以选择文件夹,会返回文件夹的路径需要自己查找每个文件

char* GetPath(HWND hWnd,char* pBuffer) 

{ BROWSEINFO bf; LPITEMIDLIST lpitem; 

memset(&bf,0,sizeof BROWSEINFO); 

bf.hwndOwner=hWnd; 

bf.lpszTitle= "选择路径"; 

bf.ulFlags=BIF_RETURNONLYFSDIRS; //属性你可自己选择

 lpitem=SHBrowseForFolder(&bf); if(lpitem==NULL) //如果没有选择路径则返回 0 return 0; //如果选择了路径则复制路径,返回路径长度 

SHGetPathFromIDList(lpitem,pBuffer); return pBuffer; 

}


//这个也行


CString CTESTDLG::getPath(void)
{
CString str;
BROWSEINFO bi;
char name[MAX_PATH];
ZeroMemory(&bi,sizeof(BROWSEINFO));
bi.hwndOwner = GetSafeHwnd();
bi.pszDisplayName = name;
bi.lpszTitle = "Select folder";
//bi.ulFlags = BIF_USENEWUI;
bi.ulFlags = BIF_RETURNFSANCESTORS;
LPITEMIDLIST idl = SHBrowseForFolder(&bi);
if(idl == NULL)
{
	MessageBox( "Select Directory error");
	return CString();
 }
SHGetPathFromIDList(idl, str.GetBuffer(MAX_PATH));
str.ReleaseBuffer();
//m_strPath = str;//为对话框中与一编辑框对应的CString型变量,保存并显示选中的路径。
//if(str.GetAt(str.GetLength()-1)!='\\')
//m_strPath+="\\";
//UpdateData(FALSE); 
return str;
}



你可能感兴趣的:(null,BI,mfc,Path)