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

发布于 2024-11-30 05:54:38 字数 968 浏览 0 评论 0原文

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

这就是我所在的位置:

预处理器定义: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\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 吗?

In other words from a blank win32 project (no wizard).

This is where I am at:

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\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();
}

Skipping over the CWinApp::Run() because it looks for a main window.

In CWinThread::Run() however, the ASSERT_VALID fails. At the top of quickwatch for this it says MyWinApp is invalid.

Do I need to create MyWinApp in another way?

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

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

发布评论

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

评论(1

给我一枪 2024-12-07 05:54:38

您可能会失败,因为您在调用 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;
}

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\n"));
    return nRetCode = 1;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文