在 MFC C++ 中打开对话框

open dialog in MFC C++

我正在尝试通过 visual studio 2013 中 MFC 项目菜单中的默认打开按钮打开文件。我使用了浏览按钮并使用了 "OnBnClickedButton" 函数来获取打开文件的地址,但现在没有这样的功能。 我该怎么办?

请参阅 CWinApp::OnFileOpen

的 MSDN 页面

向导创建的默认 MFC 应用程序(SDI 或 MDI)没有打开(或保存)代码的私有实现,它将调用默认框架代码(参见 ScottMcP-MVP 回答)

通常,您应该在应用程序中为 ID_FILE_OPEN 添加一个处理程序来调用 CFileDialog 并自行处理文件。

CFileDialog 更适合用作模态对话框

CFileDialog dlg(TRUE); // TRUE is to tell the dialog is used as an open CFileDialog.
if ( dlg.DoModal() == IDOK )
{
  CString fullPathName = dlg.GetPathName(); // get the full path name of the selected file.
  //... add some of your own code to open the file and read it.
}