Published on

Hello MFC

Authors

Many of the programs used at work were originally started five to ten years ago, and most of their GUIs are built with MFC. We rarely create them from scratch and mostly just maintain existing ones, so I have never become truly comfortable with it.

When I first touched MFC, I did not even know where the program started. When you create an MFC project from Visual C++, a lot of extra code is generated automatically, which makes it hard to understand. But if you strip away the unnecessary parts, creating a window can be written very simply, like this. All it does is create an instance of CMainWindow in InitInstance and show it.

// HelloMFC.h
class CMyApp : public CWinApp {
public:
    virtual BOOL InitInstance();
};

class CMainWindow : public CFrameWnd {
public:
    CMainWindow();

protected:
    DECLARE_MESSAGE_MAP()
};
// HelloMFC.cc
#include <afxwin.h>
#include "HelloMFC.h"

CMyApp myApp;

BOOL CMyApp::InitInstance()
{
    m_pMainWnd = new CMainWindow;

    m_pMainWnd->ShowWindow(m_nCmdShow);
    m_pMainWnd->UpdateWindow();
    return TRUE;
}

BEGIN_MESSAGE_MAP(CMainWindow, CFrameWnd)
END_MESSAGE_MAP()

CMainWindow::CMainWindow()
{
    Create(NULL, _T("Hello MFC"));
}

These days, wrappers around Qt exist for other languages as well, so I would like to move toward Qt-based development. It reduces the learning overhead, and from the Qt books I have been reading recently, it feels easier to understand.