赞
踩
最近要做一个文件打开和保存的对话框,现将相关的代码记录如下,用以备忘!
所用控件:
2个静态标签(Static Text):用以显示功能标签;
2个文本框(Edit):用以显示打开的文件路径全名要保存的文件路径全名,设置其Read Only属性为true将文本框设置为不可编辑;
2个按钮(Button):用以浏览文件;
打开文件对话框
CFileDialog打开的对话框
代码如下:
- //打开文件
- void COpenSaveFileDialogDlg::OnBnClickedButtonOpen()
- {
- BOOL isOpen = TRUE; //是否打开(否则为保存)
- CString defaultDir = L"E:\\FileTest"; //默认打开的文件路径
- CString fileName = L""; //默认打开的文件名
- CString filter = L"文件 (*.doc; *.ppt; *.xls)|*.doc;*.ppt;*.xls||"; //文件过虑的类型
- CFileDialog openFileDlg(isOpen, defaultDir, fileName, OFN_HIDEREADONLY|OFN_READONLY, filter, NULL);
- openFileDlg.GetOFN().lpstrInitialDir = L"E:\\FileTest\\test.doc";
- INT_PTR result = openFileDlg.DoModal();
- CString filePath = defaultDir + "\\test.doc";
- if(result == IDOK) {
- filePath = openFileDlg.GetPathName();
- }
- CWnd::SetDlgItemTextW(IDC_EDIT_SRC, filePath);
- }
-
- //保存文件
- void COpenSaveFileDialogDlg::OnBnClickedButtonSave()
- {
- BOOL isOpen = FALSE; //是否打开(否则为保存)
- CString defaultDir = L"E:\\FileTest"; //默认打开的文件路径
- CString fileName = L"test.doc"; //默认打开的文件名
- CString filter = L"文件 (*.doc; *.ppt; *.xls)|*.doc;*.ppt;*.xls||"; //文件过虑的类型
- CFileDialog openFileDlg(isOpen, defaultDir, fileName, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, filter, NULL);
- openFileDlg.GetOFN().lpstrInitialDir = L"E:\\FileTest\\test.doc";
- INT_PTR result = openFileDlg.DoModal();
- CString filePath = defaultDir + "\\" + fileName;
- if(result == IDOK) {
- filePath = openFileDlg.GetPathName();
- }
- CWnd::SetDlgItemTextW(IDC_EDIT_DEST, filePath);
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。