如何从头开始启动MFC应用程序? [英] How do I start up an MFC application from scratch?

查看:142
本文介绍了如何从头开始启动MFC应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这是我在哪里:

预处理器定义:WIN32

Preprocessor Definitions: WIN32

Linker-> System-> Subsystem = Console

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(),因为它查找主窗口。

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

但是,在CWinThread :: Run()中,ASSERT_VALID失败。

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

这篇关于如何从头开始启动MFC应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆