创建一个名为SelectFileandFoderDialog的MFC的应用程序
CSelectFileandFoderDialog::OnBnClickedBtnSelectFile()
{
CFileDialog cfdlg(TRUE,"",NULL,OFN_FILEMUSTEXIST|OFN_OVERWRITEPROMPT|OFN_HIDEREADONLY,"All Files(*.*)|*.*",this);
cfdlg.m_ofn.lpstrTitle = _T("弹出对话框名称");
if(cfdlg.DoModal() == IDCANCEL)
{
return;
}
CString sFPath = cfdlg.GetPathName();//包含文件名
SetDlgItemText(IDC_EDIT_FILEPATH,sFPath);
}
//选择文件夹名
CSelectFileandFoderDialog::OnBnClickedBtnSelectFolder()
{
CString sFolderPath;
BROWEINFO binfo;
WCHAR buffer[MAX_PATH] = {0};
//初始化bi参数
bi.hwndOwner = NULL;
bi.pidlRoot = NULL;
bi.pszDisplayName = buffer;//如果参数为NULL,则不能显示选择对话框
bi.lpszTitle = _T("自定义对话框名称");
bi.ulFlag = BIF_EDITBOX;//包括文件
bi.lpfn = NULL;
bi.iImalge = IDR_MAINFRAME;
//初始化结束
LPITEMIDLIST lpIDlist = SHBroweForFolder(&bi)
if(lpIDlist)
{
SHGetPathFromIDList(lpIDlist,buffer);//获取文件夹路径,并将返回值存放到buffer中
sFolderPath = buffer;
}
LPMALLOC lpMalloc;
if(FAILED(SHGetMalloc(&lpMalloc)))
return;
// release 内存
lpMalloc->Free(lpMalloc);
lpMalloc->Release();
SetDlgItemText(IDC_EDIT_FOLDER,sFolderPath);
}