开发者

How do I start up an MFC application from scratch?

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 开发者_如何学Pythonis invalid.

Do I need to create MyWinApp in another way?


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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜