我应该在哪里声明 c++ 中的实例?

发布于 2024-12-11 15:25:22 字数 1075 浏览 0 评论 0原文

我知道这样的新手问题,但我似乎无法在网上找到任何答案。基本上我正在使用 CFile 对话框,不确定是否应该将其放入 .cpp 文件或头文件中。提前致谢。

CFileDialog( BOOL bOpenFileDialog, 
             LPCTSTR lpszDefExt = NULL, 
             LPCTSTR lpszFileName = NULL, 
             DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, 
             LPCTSTR lpszFilter = NULL, 
             CWnd* pParentWnd = NULL ); 

由 ChrisBD 编辑

好的,我已将包含内容添加到我的 FileDialogDlg.cpp 中并添加了代码:

CFileDialog fileDlg( TRUE, 
                     NULL, 
                     NULL, 
                     OFN_ALLOWMULTISELECT | OFN_HIDEREADONLY, 
                     "All Files (.)|*.*||", 
                     this); 

// Initializes m_ofn structure 
fileDlg.m_ofn.lpstrTitle = "My File Dialog"; 

// Call DoModal 
if ( fileDlg.DoModal() == IDOK) 
{ 
    CString szlstfile = fileDlg.GetPathName(); // This is your selected file 
                                               // name with path

    AfxMessageBox("Your file name is :" +szlstfile ); 
} 

我的编译器仍然显示大量错误

Such a newbie question I know but I can't seem to find any answers online. Basically I am using the CFile Dialog and not sure if I should put it in the .cpp file or the header file. Thanks in advance.

CFileDialog( BOOL bOpenFileDialog, 
             LPCTSTR lpszDefExt = NULL, 
             LPCTSTR lpszFileName = NULL, 
             DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, 
             LPCTSTR lpszFilter = NULL, 
             CWnd* pParentWnd = NULL ); 

edit by ChrisBD

Okay, so I have added the includes to my FileDialogDlg.cpp and added the code:

CFileDialog fileDlg( TRUE, 
                     NULL, 
                     NULL, 
                     OFN_ALLOWMULTISELECT | OFN_HIDEREADONLY, 
                     "All Files (.)|*.*||", 
                     this); 

// Initializes m_ofn structure 
fileDlg.m_ofn.lpstrTitle = "My File Dialog"; 

// Call DoModal 
if ( fileDlg.DoModal() == IDOK) 
{ 
    CString szlstfile = fileDlg.GetPathName(); // This is your selected file 
                                               // name with path

    AfxMessageBox("Your file name is :" +szlstfile ); 
} 

My compiler is still showing a load of errors

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

宣告ˉ结束 2024-12-18 15:25:22

我对“无法将参数 5 从...转换”错误的赌注是,您将应用程序编译为 Unicode(这是一件好事)。然后,您必须在代码中使用支持 Unicode 的字符串文字作为字符串参数:

CFileDialog fileDlg( TRUE,  
                     NULL,  
                     NULL,  
                     OFN_ALLOWMULTISELECT | OFN_HIDEREADONLY,  
                     L"All Files (.)|*.*||", // <-- I Added the leading L  
                     this);  

您还可以决定使用 TEXT() 宏或其 _T()< 使其与 ANSI/Unicode 兼容。 /代码> 快捷方式。

CFileDialog fileDlg( TRUE,  
                     NULL,  
                     NULL,  
                     OFN_ALLOWMULTISELECT | OFN_HIDEREADONLY,  
                     _T("All Files (.)|*.*||"), // <-- _T("blah")
                     this);  

My bet regarding the "cannot convert parameter 5 from ..." error is that you compile your app as Unicode (which is a good thing). You must then use Unicode-aware string literals in your code for string parameters:

CFileDialog fileDlg( TRUE,  
                     NULL,  
                     NULL,  
                     OFN_ALLOWMULTISELECT | OFN_HIDEREADONLY,  
                     L"All Files (.)|*.*||", // <-- I Added the leading L  
                     this);  

You could also decide to make it both ANSI/Unicode compatible using the TEXT() macro or its _T() shortcut.

CFileDialog fileDlg( TRUE,  
                     NULL,  
                     NULL,  
                     OFN_ALLOWMULTISELECT | OFN_HIDEREADONLY,  
                     _T("All Files (.)|*.*||"), // <-- _T("blah")
                     this);  
妄司 2024-12-18 15:25:22

答案是都不是 - CFileDialog 类已经在 afxdlgs.h 中为您声明(根据 CFileDialog文档),所以只是:

#include <afxdlgs.h>

然后您可以在代码中使用CFileDialog

The answer is neither - the CFileDialog class is already declared for you in afxdlgs.h (according to the CFileDialog documentation), so just:

#include <afxdlgs.h>

Then you can use CFileDialog in your code.

少年亿悲伤 2024-12-18 15:25:22

我建议您在本地创建一个新实例,设置其属性,然后以模式方式打开它。例如:

// Create an Open dialog; the default file name extension is ".txt".
   CFileDialog fileDlg (TRUE, "txt", "*.txt", 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.
      ...
   }

I would suggest that you create a new instance locally, set its properties and then open it modally. For example:

// Create an Open dialog; the default file name extension is ".txt".
   CFileDialog fileDlg (TRUE, "txt", "*.txt", 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.
      ...
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文