转载来自:http://blog.csdn.net/u012005313/article/details/46639349
在MFC编程中经常会需要用到选择目录和选择文件的界面,以下总结一下本人常用的这两种对话框的生成方法:
选择目录对话框
-
- void CDcPackerDlg::OnBnClickedDecgen()
- {
- char szPath[MAX_PATH];
- CString str;
-
- ZeroMemory(szPath, sizeof(szPath));
-
- BROWSEINFO bi;
- bi.hwndOwner = m_hWnd;
- bi.pidlRoot = NULL;
- bi.pszDisplayName = szPath;
- bi.lpszTitle = "请选择需要打包的目录:";
- bi.ulFlags = 0;
- bi.lpfn = NULL;
- bi.lParam = 0;
- bi.iImage = 0;
-
- LPITEMIDLIST lp = SHBrowseForFolder(&bi);
-
- if(lp && SHGetPathFromIDList(lp, szPath))
- {
- str.Format("选择的目录为 %s", szPath);
- AfxMessageBox(str);
-
-
- }
- else
- AfxMessageBox("无效的目录,请重新选择");
- }
选择文件对话框
- CString CDcPackerDlg::BootOpenDialog()
- {
- CString strFile = _T("");
-
- CFileDialog dlgFile(TRUE, NULL, NULL, OFN_HIDEREADONLY, _T("Describe Files (*.cfg)|*.cfg|All Files (*.*)|*.*||"), NULL);
-
- if (dlgFile.DoModal())
- {
- strFile = dlgFile.GetPathName();
- }
-
- return strFile;
- }
-
-
- void CDcPackerDlg::OnBnClickedSelectdec()
- {
-
- m_strDescPath = "";
-
-
- m_strDescPath = BootOpenDialog();
-
-
- }
保存对话框:
- void CExample17Dlg::OnBnClickedSaveButton()
- {
-
-
- TCHAR szFilter[] = _T("文本文件(*.txt)|*.txt|Word文件(*.doc)|*.doc|所有文件(*.*)|*.*||");
-
- CFileDialog fileDlg(FALSE, _T("doc"), _T("my"), OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter, this);
- CString strFilePath;
-
-
- if (IDOK == fileDlg.DoModal())
- {
-
- strFilePath = fileDlg.GetPathName();
- SetDlgItemText(IDC_SAVE_EDIT, strFilePath);
- }
- }