包括自定义存储路径和存储名字格式等
怎样用C++实现文件另存为功能
- 提问者网友:我的未来我做主
- 2021-07-21 01:41
- 五星知识达人网友:几近狂妄
- 2021-07-21 02:19
C++的WinSDK\MFC可以实现:
MFC方法(要在工程属性里设置MFC支持,最好只在MFC工程里使用):
#include "afxdlgs.h"
CFileDialog fileDlg( 0 , 0, 0, 0, "自设文件类型(*.abc)|*.abc|所有文件(*.*)|*.*||", 0);
if (fileDlg.DoModal()==1)
{
AfxMessageBox(fileDlg.GetPathName());
}
WinSDK方法:
#include "windows.h"
char lpFilePath[255]={'\0'};
OPENFILENAME ofn ;
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof (OPENFILENAME) ;
ofn.hwndOwner = hwnd ;
ofn.hInstance = NULL ;
ofn.lpstrFilter = "自设文件类型(*.abc)\0*.abc\0所有文件(*.*)\0*.*\0\0", ;
ofn.lpstrCustomFilter = NULL ;
ofn.nMaxCustFilter = 0 ;
ofn.nFilterIndex = 0 ;
ofn.lpstrFile = lpFilePath ; // 设置存放文件路径缓冲区
ofn.nMaxFile = MAX_PATH ; //缓冲区大小;
ofn.lpstrFileTitle = NULL ; // 设置存放文件名称缓冲区
ofn.nMaxFileTitle = MAX_PATH ; //缓冲区大小;
ofn.lpstrInitialDir = NULL ;
ofn.lpstrTitle = "另寸为" ;
ofn.Flags = 0;
ofn.nFileOffset = 0 ;
ofn.nFileExtension = 0 ;
ofn.lpstrDefExt = 0 ;
//ofn.lCustData = 0L ;
ofn.lpfnHook = NULL ;
ofn.lpTemplateName = NULL ;
GetOpenFileName (&ofn) ;