在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("无效的目录,请重新选择");
}
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();
}
{
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();
}
来源:tzip://Error404/error_ie.html?ecode=403&magic=526494&url=http%3A%2F%2Fgoogleads.g.doubleclick.net%2Fpagead%2Fads%3Fclient%3Dca-pub-4210569241504288%26format%3D180x150_as%26output%3Dhtml%26h%3D150%26w%3D180%26lmt%3D1337569710%26ad_type%3Dtext%26ea%3D0%26flash%3D10.3.183.18%26url%3Dhttp%253A%252F%252Fwww.cppblog.com%252Ffinehai%252Farchive%252F2009%252F08%252F27%252F94578.html%26dt%3D1337569710703%26shv%3Dr20120509%26jsv%3Dr20110914%26saldr%3D1%26correlator%3D1337569708437%26frm%3D20%26adk%3D2791084759%26ga_vid%3D813004607.1335940718%26ga_sid%3D1337569708%26ga_hid%3D1867766327%26ga_fc%3D1%26u_tz%3D480%26u_his%3D0%26u_java%3D1%26u_h%3D1050%26u_w%3D1400%26u_ah%3D1020%26u_aw%3D1400%26u_cd%3D32%26u_nplug%3D0%26u_nmime%3D0%26dff%3Darial%26dfs%3D12%26adx%3D0%26ady%3D0%26biw%3D1383%26bih%3D812%26oid%3D3%26ref%3Dhttp%253A%252F%252Fwww.baidu.com%252Fbaidu%253Fword%253DMFC%252520%2525D1%2525A1%2525D4%2525F1%2525CE%2525C4%2525BC%2525FE%2526sg%253D123%26fu%3D0%26ifi%3D3%26dtd%3D78&ref=http%3A%2F%2Fwww.baidu.com%2Fbaidu%3Fword%3DMFC%2520%25D1%25A1%25D4%25F1%25CE%25C4%25BC%25FE%26sg%3D123&at=%5BMFC%5D%E9%80%89%E6%8B%A9%E7%9B%AE%E5%BD%95%E5%AF%B9%E8%AF%9D%E6%A1%86%E5%92%8C%E9%80%89%E6%8B%A9%E6%96%87%E4%BB%B6%E5%AF%B9%E8%AF%9D%E6%A1%86+-+My+C%2B%2B+-+C%2B%2B%E5%8D%9A%E5%AE%A2#http://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-4210569241504288&format=180x150_as&output=html&h=150&w=180&lmt=1337569710&ad_type=text&ea=0&flash=10.3.183.18&url=http%3A%2F%2Fwww.cppblog.com%2Ffinehai%2Farchive%2F2009%2F08%2F27%2F94578.html&dt=1337569710703&shv=r20120509&jsv=r20110914&saldr=1&correlator=1337569708437&frm=20&adk=2791084759&ga_vid=813004607.1335940718&ga_sid=1337569708&ga_hid=1867766327&ga_fc=1&u_tz=480&u_his=0&u_java=1&u_h=1050&u_w=1400&u_ah=1020&u_aw=1400&u_cd=32&u_nplug=0&u_nmime=0&dff=arial&dfs=12&adx=0&ady=0&biw=1383&bih=812&oid=3&ref=http%3A%2F%2Fwww.baidu.com%2Fbaidu%3Fword%3DMFC%2520%25D1%25A1%25D4%25F1%25CE%25C4%25BC%25FE%26sg%3D123&fu=0&ifi=3&dtd=78