MFC打开对话框和浏览文件夹操作

MFC打开对话框和浏览文件夹操作

1.打开文件夹操作:

[cpp]

CFileDialog hFileDlg(TRUE,NULL,NULL,OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST|OFN_READONLY,
TEXT(“支持的图像文件 (*.jpg;*.gif;*.bmp;…)|*.jpg;*.gif;*.bmp|专用Tiff图像文件(*.tiff;*.tif)|*.tiff;*.tif|所有文件(*.*)|*.*||”),NULL);
hFileDlg.m_ofn.nFilterIndex=1;
hFileDlg.m_ofn.hwndOwner=m_hWnd;
hFileDlg.m_ofn.lStructSize=sizeof(OPENFILENAME);
hFileDlg.m_ofn.lpstrTitle=TEXT(“打开图像文件…\0”);
hFileDlg.m_ofn.nMaxFile=MAX_PATH;
if(hFileDlg.DoModal() == IDOK)
{
m_path = hFileDlg.GetPathName();
UpdateData(FALSE);
}

[/cpp]

2.保存文件,同上!

注意:CFileDialog hFileDlg(true,NULL 改为 CFileDialog hFileDlg(false,NULL,
或者将GetOpenFileNameA改为GetSaveFileNameA,即可

3.浏览文件夹对话框:

[cpp]

WCHAR szDir[MAX_PATH]; //依旧ansi和unicode更换类型
BROWSEINFO bi;
ITEMIDLIST *pidl;

bi.hwndOwner = this->m_hWnd;
bi.pidlRoot = NULL;
bi.pszDisplayName = szDir;
bi.lpszTitle = L”请选择目录”;
bi.ulFlags = BIF_RETURNONLYFSDIRS;
bi.lpfn = NULL;
bi.lParam = 0;
bi.iImage = 0;

pidl = SHBrowseForFolder(&bi);
if(pidl == NULL)
return;

if(SHGetPathFromIDList(pidl, szDir))
{

CString m_strSavePath.Format(L”%s”, szDir);

}

[/cpp]