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;
}