gpt4 book ai didi

c++ - 如何从头开始启动 MFC 应用程序?

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:05:20 24 4
gpt4 key购买 nike

换句话说,来自一个空白的 win32 项目(没有向导)。

这是我所在的地方:

预处理器定义:WIN32

链接器->系统->子系统=控制台

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\n"));
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() 因为它寻找主窗口。

然而,在 CWinThread::Run() 中,ASSERT_VALID 失败。在快速观察的顶部,它说 MyWinApp 无效。

我是否需要以其他方式创建 MyWinApp?

最佳答案

您失败的原因可能是您正在调用 AfxWinInit 之后 创建 CWinApp。在常规 MFC 应用程序中,CWinApp 是一个全局变量,它在 main 之前构造。这样,当 MFC 初始化时,它就有一个有效的全局 CWinApp。尝试:

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\n"));
return nRetCode = 1;
}

关于c++ - 如何从头开始启动 MFC 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7124307/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com