CFileDialog 打开文件

先来看看参数

 

 

Parameters
bOpenFileDialog

TRUE表示打开文件 FALSE表示另存文件.

lpszDefExt

文件扩展名. If the user does not include an extension in the Filename edit box, the extension specified by lpszDefExt is automatically appended to the filename. If this parameter is NULL, no file extension is appended.

lpszFileName

默认文件名。The initial filename that appears in the filename edit box. If NULL, no filename initially appears.

dwFlags

A combination of one or more flags that allow you to customize the dialog box. For a description of these flags, see the OPENFILENAME structure in the Platform SDK. If you modify the m_ofn.Flags structure member, use a bitwise-OR operator in your changes to keep the default behavior intact.

lpszFilter

文件过滤器A series of string pairs that specify filters you can apply to the file. If you specify file filters, only selected files will appear in the Files list box. See the Remarks section for more information on how to work with file filters.

pParentWnd

A pointer to the file dialog-box object's parent or owner window.

dwSize

The size of the OPENFILENAME structure. This value is dependent on the operating system version, so MFC can determine the appropriate kind of dialog box to create (for example, new Windows 2000 dialogs as opposed to NT4 dialogs). The default size of 0 means that the MFC code will determine the proper dialog box size to use based on the operating system version on which the program is run.

void CChildFrame::OnFileOpen() 
{
   // szFilters is a text string that includes two file name filters:
   // "*.my" for "MyType Files" and "*.*' for "All Files."
   char CChildFrame::szFilters[]=
      "MyType Files (*.my)|*.my|All Files (*.*)|*.*||";

   // Create an Open dialog; the default file name extension is ".my".
   CFileDialog fileDlg (TRUE, "my", "*.my",
      OFN_FILEMUSTEXIST| OFN_HIDEREADONLY, szFilters, this);
   
   // Display the file dialog. When user clicks OK, fileDlg.DoModal() 
   // returns IDOK.
   if( fileDlg.DoModal ()==IDOK )
   {
      CString pathName = fileDlg.GetPathName();
   
      // Implement opening and reading file in here.
      ...
      //Change the window's title to the opened file's title.
      CString fileName = fileDlg.GetFileTitle ();
   
      SetWindowText(fileName);
   }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++ MFC中的文件打开和保存对话框可以通过CFileDialog类来实现。CFileDialog类是MFC提供的一个对话框类,用于选择文件打开和保存操作。 要使用CFileDialog类,首先需要包含头文件<afxdlgs.h>。然后可以创建一个CFileDialog对象,并使用DoModal()函数来显示对话框。 下面是一个简单的示例代码,演示如何使用CFileDialog类来实现文件打开和保存对话框: ```cpp #include <afxdlgs.h> // 文件打开对话框示例 void OpenFile() { CFileDialog dlg(TRUE); // 创建一个打开文件对话框 if (dlg.DoModal() == IDOK) // 显示对话框并检查用户是否点击了“打开”按钮 { CString filePath = dlg.GetPathName(); // 获取用户选择的文件路径 // 在这里可以对文件进行处理,比如读取文件内容等 } } // 文件保存对话框示例 void SaveFile() { CFileDialog dlg(FALSE); // 创建一个保存文件对话框 if (dlg.DoModal() == IDOK) // 显示对话框并检查用户是否点击了“保存”按钮 { CString filePath = dlg.GetPathName(); // 获取用户选择的文件路径 // 在这里可以将数据保存到文件中 } } ``` 在上面的示例代码中,OpenFile()函数演示了如何使用CFileDialog类来实现文件打开对话框。首先创建一个CFileDialog对象,参数TRUE表示打开文件对话框。然后调用DoModal()函数显示对话框,并检查用户是否点击了“打开”按钮。如果用户点击了“打开”按钮,可以通过GetPathName()函数获取用户选择的文件路径。 SaveFile()函数演示了如何使用CFileDialog类来实现文件的保存对话框。与打开对话框类似,首先创建一个CFileDialog对象,参数FALSE表示保存文件对话框。然后调用DoModal()函数显示对话框,并检查用户是否点击了“保存”按钮。如果用户点击了“保存”按钮,可以通过GetPathName()函数获取用户选择的文件路径。 需要注意的是,CFileDialog类还提供了其他一些函数来设置对话框的标题、过滤器、默认文件名等属性,可以根据实际需求进行设置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值