vc++中如何调用打开文件对话框
我查了很多网上资料,都没有一个完整能运行的程序,都是一段代码,小弟是初学者,很多不明白,所以希望有一个能运行的程序,然后再分析代码学习。还有能不能不用MFC就是普通的c+...
我查了很多网上资料,都没有一个完整能运行的程序,都是一段代码,小弟是初学者,很多不明白,所以希望有一个能运行的程序,然后再分析代码学习。还有能不能不用MFC就是普通的c++或者c工程实现调用打开文件对话框呢?
展开
3个回答
展开全部
MFC 打开保存文件对话框
一、打开文件夹:
1、CFileDialog实现
CFileDialog hFileDlg(TRUE,NULL,NULL,OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST|OFN_READONLY,
TEXT("支持的图像文件 (*.jpg;*.gif;*.bmp;...)|*.jpg;*.gif;*.bmp|专用Tiff图像文件(*.tiff;*.tif)|*.tiff;*.tif|所有文件(*.*)|*.*||"),NULL);
hFileDlg.m_ofn.nFilterIndex=1;
hFileDlg.m_ofn.hwndOwner=m_hWnd;
hFileDlg.m_ofn.lStructSize=sizeof(OPENFILENAME);
hFileDlg.m_ofn.lpstrTitle=TEXT("打开图像文件...\0");
hFileDlg.m_ofn.nMaxFile=MAX_PATH;
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);
}
设置对话框的标题:
dlg.m_ofn.lpstrTitle = _T("Open Image");
一、打开文件夹:
1、CFileDialog实现
CFileDialog hFileDlg(TRUE,NULL,NULL,OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST|OFN_READONLY,
TEXT("支持的图像文件 (*.jpg;*.gif;*.bmp;...)|*.jpg;*.gif;*.bmp|专用Tiff图像文件(*.tiff;*.tif)|*.tiff;*.tif|所有文件(*.*)|*.*||"),NULL);
hFileDlg.m_ofn.nFilterIndex=1;
hFileDlg.m_ofn.hwndOwner=m_hWnd;
hFileDlg.m_ofn.lStructSize=sizeof(OPENFILENAME);
hFileDlg.m_ofn.lpstrTitle=TEXT("打开图像文件...\0");
hFileDlg.m_ofn.nMaxFile=MAX_PATH;
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);
}
设置对话框的标题:
dlg.m_ofn.lpstrTitle = _T("Open Image");
展开全部
给你一段代码插入到程序中
/****************************************************************************
** getVCFileName: wrapper around MFC CFileDialog
** Suveying Foundation Class Library Liu Renzhao
*****************************************************************************/
bool getVCFileName(BOOL isOpen, LPCTSTR title, LPCTSTR ext, LPCTSTR defFname, CString& fname)
{
DWORD size = 256;
char buffer[256];
CString filter = CString("*.")+ext;
CFileDialog dbox (isOpen, ext, defFname, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, LPCTSTR(filter+"|"+filter+CString("|*.txt|*.txt|*.*|*.*||")));
if (title != NULL)
dbox.m_ofn.lpstrTitle = _T(title);
GetCurrentDirectory(size, buffer); //获取当前目录
dbox.m_ofn.lpstrInitialDir = _T(buffer);
if (dbox.DoModal() == IDOK) {
fname = dbox.GetPathName();
return true;
}
else
return false;
}
/****************************************************************************
** getVCFileName: wrapper around MFC CFileDialog
** Suveying Foundation Class Library Liu Renzhao
*****************************************************************************/
bool getVCFileName(BOOL isOpen, LPCTSTR title, LPCTSTR ext, LPCTSTR defFname, CString& fname)
{
DWORD size = 256;
char buffer[256];
CString filter = CString("*.")+ext;
CFileDialog dbox (isOpen, ext, defFname, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, LPCTSTR(filter+"|"+filter+CString("|*.txt|*.txt|*.*|*.*||")));
if (title != NULL)
dbox.m_ofn.lpstrTitle = _T(title);
GetCurrentDirectory(size, buffer); //获取当前目录
dbox.m_ofn.lpstrInitialDir = _T(buffer);
if (dbox.DoModal() == IDOK) {
fname = dbox.GetPathName();
return true;
}
else
return false;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
主要 要是把GetOpenFileName函数和OPENFILENAME结构体 的用法搞明白
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询