1. 功能:打开文件选择对话框,选择某个数据文件;或者弹出文件另存为对话框,保存数据文件。
2. 实现:
2.1 平台: WinCE6.0 + VS2005
2.2 实现方法:打开或者另存为对话框,要使用CFileDialog。函数解释为:The destruction of CFileDialog objects is handled automatically. It is not necessary to call CDialog::EndDialog.
// 相关参数的设置
CString FileFlt = _T("data file(*.");
FileFlt += FILE_EXT_1; // 获取数据文件的拓展名
FileFlt += _T(")|*.");
FileFlt += FILE_EXT_1;
FileFlt += _T("||");
CFileDialog FileDlg(TRUE,NULL,NULL,OFN_HIDEREADONLY,FileFlt,this,0);
FileDlg.m_ofn.lpstrInitialDir = DataFileSaveDir;
FileDlg.m_ofn.lpstrTitle = L"请选择数据文件...";
INT_PTR nResponse = FileDlg.DoModal();
if(nResponse == IDCANCEL)
return;
运行效果为:
如果要显示保存数据文件提示,修改FileDlg.m_ofn.lpstrTitle = L"另存数据文件...";即可。
还有一种方法也可实现这两个功能,即使用GetOpenFileName和GetSaveFileName。
GetOpenFileName的函数定义为:
BOOL GetOpenFileName( LPOPENFILENAME lpofn);
Remarks
Not all members of the OPENFILENAME structure are defined in Windows CE. For information about the defined members, see the reference topic for the OPENFILENAME structure.Windows CE modifies the meaning of the following members of the OPENFILENAME structure when it is passed into the GetOpenFileName function.
参数lpofn是一结构体,这个结构体的参数列表如下:
typedef struct tagOFN { /* ofn */
DWORD lStructSize; HWND hwndOwner; HINSTANCE hInstance; LPCSTR lpstrFilter; LPSTR lpstrCustomFilter; DWORD nMaxCustFilter; DWORD nFilterIndex; LPSTR lpstrFile; DWORD nMaxFile; LPSTR lpstrFileTitle; DWORD nMaxFileTitle; LPSTR lpstrInitialDir; LPCSTR lpstrTitle; DWORD Flags; WORD nFileOffset; WORD nFileExtension; LPCSTR lpstrDefExt; DWORD lCustData; LPOFNHOOKPROC lpfnHook; LPCSTR lpTemplateName;} OPENFILENAME;GetSaveFileName函数的参数及使用说明与GetOpenFileName()基本一致,不再赘述了。