How do I start up an MFC application from scratch?(如何从头开始启动 MFC 应用程序?)
问题描述
换句话说,来自一个空白的 win32 项目(没有向导).
In other words from a blank win32 project (no wizard).
这就是我所在的地方:
预处理器定义:WIN32
Preprocessor Definitions: WIN32
链接器->系统->子系统=控制台
Linker->System->Subsystem = Console
int _tmain()
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed
"));
return nRetCode = 1;
}
MyWinApp* app = new MyWinApp();
app->InitApplication();
app->InitInstance();
app->Run();
AfxWinTerm();
return 0;
}
class MyWinApp: public CWinApp
{
public:
BOOL InitInstance();
int Run();
};
BOOL MyWinApp::InitInstance()
{
return TRUE;
}
int MyWinApp::Run()
{
return CWinThread::Run();
}
跳过 CWinApp::Run(),因为它会寻找一个主窗口.
Skipping over the CWinApp::Run() because it looks for a main window.
然而,在 CWinThread::Run() 中,ASSERT_VALID 失败.在 quickwatch 的顶部,它说 MyWinApp 无效.
In CWinThread::Run() however, the ASSERT_VALID fails. At the top of quickwatch for this it says MyWinApp is invalid.
我需要以其他方式创建 MyWinApp 吗?
Do I need to create MyWinApp in another way?
推荐答案
你可能失败了,因为你正在创建 CWinApp 在你调用 AfxWinInit.在常规的 MFC 应用程序中,CWinApp 是一个全局变量,它在 main 之前构造.这样,当 MFC 初始化时,它就有一个有效的全局 CWinApp.试试:
You're probably failing because you're creating the CWinApp after you're calling AfxWinInit. In a regular MFC app, the CWinApp is a global variable, which is constructed before main. This way, when MFC is initialized, it has a valid global CWinApp in place. Try:
MyWinApp* app = new MyWinApp(); // ^moved up^
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed
"));
return nRetCode = 1;
}
这篇关于如何从头开始启动 MFC 应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何从头开始启动 MFC 应用程序?
基础教程推荐
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- CString 到 char* 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
