VC++------------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() { // TODO: Add your control notification handler code here m_strDescPath = ""; //类的成员变量

//"打开文件"对话框,选择文件,返回其路径 m_strDescPath = BootOpenDialog();

}


经验分享 程序员 微信小程序 职场和发展