VC++開啟、儲存檔案對話方塊和瀏覽資料夾對話方塊
一。開啟資料夾:
1。CFileDialog實現
CFileDialog hFileDlg(true,NULL ,
NULL,
OFN_FILEMUSTEXIST | OFN_READONLY | OFN_PATHMUSTEXIST,
TEXT("驅動檔案 (*.sys)|*.sys|所有檔案(*.*)|*.*|"),
NULL);
if(hFileDlg.DoModal() == IDOK)
{
m_path = hFileDlg.GetPathName();
UpdateData(FALSE);
}
2。API實現
OPENFILENAMEA ofn;
char szFile[260];
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = NULL;
ofn.lpstrFile = szFile;
ofn.lpstrFile[0] = '/0';
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = "音訊檔案/0*.wma/0";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = 0;
if (GetOpenFileNameA(&ofn)==FALSE)
return S_FALSE;
CString c_save_path=ofn.lpstrFile;
二、儲存檔案
同上,只不過將“ CFileDialog hFileDlg(true,NULL ”改為“ CFileDialog hFileDlg(false,NULL ”,或者將GetOpenFileNameA改為GetSaveFileNameA,即可。
三、瀏覽資料夾對話方塊
//回撥函式
int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
if(uMsg==BFFM_SELCHANGED||uMsg==BFFM_INITIALIZED)
{
if(uMsg==BFFM_INITIALIZED)
{
::SendMessage(hwnd,BFFM_SETSELECTION,TRUE,
LPARAM(lpData));
}
}
return 0;
}
TCHAR chPath[255]; //用來儲存路徑的字串
CString strPath = "";
BROWSEINFO bInfo;
GetModuleFileName(NULL,chPath,MAX_PATH);
strPath=chPath;
ZeroMemory(&bInfo, sizeof(bInfo));
bInfo.hwndOwner = m_hWnd;
bInfo.lpszTitle = _T("請選擇路徑: ");
bInfo.ulFlags = BIF_RETURNONLYFSDIRS|BIF_EDITBOX;
bInfo.lpfn = BrowseCallbackProc;
bInfo.lParam = (LPARAM)strPath.GetBuffer(strPath.GetLength());
LPITEMIDLIST lpDlist; //用來儲存返回資訊的IDList
lpDlist = SHBrowseForFolder(&bInfo) ; //顯示選擇對話方塊
if(lpDlist != NULL) //使用者按了確定按鈕
{
SHGetPathFromIDList(lpDlist, chPath);//把專案標識列表轉化成字串
strPath = chPath; //將TCHAR型別的字串轉換為CString型別的字串
m_save_path=strPath;
UpdateData(FALSE);
}