http://xinxing124.blog.163.com/blog/static/3017019520103911406972/
VC++:打开、保存文件对话框和浏览文件夹对话框_一切随缘_新浪博客
一。打开文件夹:
1。CFileDialog实现
CFileDialog hFileDlg(true,NULL ,
NULL,
OFN_FILEMUSTEXIST | OFN_READONLY | OFN_PATHMUSTEXIST,
TEXT("驱动文件 (*.sys)|*.sys|所有文件(*.*)|*.*|"),
NULL);
if(hFileDlg.DoModal() == IDOK)
{
m_path = hFileDlg.GetPathName();
UpdateData(FALSE);
}
2。API实现
OPENFILENAMEA ofn;
char szFile[260];
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = NULL;
ofn.lpstrFile = szFile;
ofn.lpstrFile[0] = '/0';
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = "音频文件/0*.wma/0";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = 0;
if (GetOpenFileNameA(&ofn)==FALSE)
return S_FALSE;
CString c_save_path=ofn.lpstrFile;
二、保存文件
同上,只不过将“ CFileDialog hFileDlg(true,NULL ”改为“ CFileDialog hFileDlg(false,NULL ”,或者将GetOpenFileNameA改为GetSaveFileNameA,即可。
三、浏览文件夹对话框
//回调函数
int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
if(uMsg==BFFM_SELCHANGED||uMsg==BFFM_INITIALIZED)
{
if(uMsg==BFFM_INITIALIZED)
{
::SendMessage(hwnd,BFFM_SETSELECTION,TRUE,
LPARAM(lpData));
}
}
return 0;
}
TCHAR chPath[255]; //用来存储路径的字符串
CString strPath = "";
BROWSEINFO bInfo;
GetModuleFileName(NULL,chPath,MAX_PATH);
strPath=chPath;
ZeroMemory(&bInfo, sizeof(bInfo));
bInfo.hwndOwner = m_hWnd;
bInfo.lpszTitle = _T("请选择路径: ");
bInfo.ulFlags = BIF_RETURNONLYFSDIRS|BIF_EDITBOX;
bInfo.lpfn = BrowseCallbackProc;
bInfo.lParam = (LPARAM)strPath.GetBuffer(strPath.GetLength());
LPITEMIDLIST lpDlist; //用来保存返回信息的IDList
lpDlist = SHBrowseForFolder(&bInfo) ; //显示选择对话框
if(lpDlist != NULL) //用户按了确定按钮
{
SHGetPathFromIDList(lpDlist, chPath);//把项目标识列表转化成字符串
strPath = chPath; //将TCHAR类型的字符串转换为CString类型的字符串
m_save_path=strPath;
UpdateData(FALSE);
}
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。
新浪BLOG意见反馈留言板 不良信息反馈 电话:95105670 提示音后按2键(按当地市话标准计费) 欢迎批评指正
新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 会员注册 | 产品答疑
Copyright © 1996 - 2010 SINA Corporation, All Rights Reserved