我应该在哪里声明 c++ 中的实例?
我知道这样的新手问题,但我似乎无法在网上找到任何答案。基本上我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我对“无法将参数 5 从...转换”错误的赌注是,您将应用程序编译为 Unicode(这是一件好事)。然后,您必须在代码中使用支持 Unicode 的字符串文字作为字符串参数:
您还可以决定使用
TEXT()
宏或其_T()< 使其与 ANSI/Unicode 兼容。 /代码> 快捷方式。
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:
You could also decide to make it both ANSI/Unicode compatible using the
TEXT()
macro or its_T()
shortcut.答案是都不是 -
CFileDialog
类已经在afxdlgs.h
中为您声明(根据CFileDialog
文档),所以只是:然后您可以在代码中使用
CFileDialog
。The answer is neither - the
CFileDialog
class is already declared for you inafxdlgs.h
(according to theCFileDialog
documentation), so just:Then you can use
CFileDialog
in your code.我建议您在本地创建一个新实例,设置其属性,然后以模式方式打开它。例如:
I would suggest that you create a new instance locally, set its properties and then open it modally. For example: