Programming Windows: Hello, MFC (Premium)

Before diving into the Microsoft Foundation Class library, I thought it might be a good idea to say hello to MFC with a bit of source code.

For an MFC version of hello, world, I turned to Jeff Prosise’s Programming Windows 95 with MFC, an attempt by Microsoft Press to recapture the magic of Charles Petzold’s classic Programming Windows. A second and final edition, simply called Programming Windows with MFC, was released in 1999, and then Microsoft eventually moved on.

Prosise’s Hello, MFC app consisted of two files, a header file named HelloMFC.h and a C++ source code file named HelloMFC.cpp.

The header file is straightforward and looks like so:
class CMyApp : public CWinApp
{
public:
virtual BOOL InitInstance();
};

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

protected:
    afx_msg void OnPaint();
    DECLARE_MESSAGE_MAP()
};
And the main source code file looks like so.
#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)
     ON_WM_PAINT()
END_MESSAGE_MAP()

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

void CMainWindow::OnPaint()
{
     CPaintDC dc(this);    

CRect rect;
GetClientRect(&rect);

dc.DrawTextW("Hello, MFC!", &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
}
The code you see above is only slightly modified from what’s found in the 1996 book, and the required changes are similar to what I needed to do previously when I reworked Petzold’s Hello, Windows application for Visual Studio. (DrawTextW is a more modern version of DrawText, etc.)

But it wouldn't compile at first. Without getting too bogged down in the details, creating an MFC application with Visual Studio using the MFC App project type requires stepping through a multi-page wizard in which you specify numerous application attributes (SDI vs. MDI, document/view architecture, numerous user interface features, and much, much more. And even when you unselect most options, the resulting project includes several header files and several source code files. So I deleted all of them but the two files noted above, deleted their contents, and then enter the code you see above.

After that, I was getting an error message related to precompiled headers, so I dug around in the project settings and disabled the use of precompiled headers (in Properties > Configuration Properties > C/C++ > Precompiled Headers.)

(There is almost certainly a more elegant way to create a simple C++/MFC application in Visual Studio. But I'm not going to stick around long enough to become an MFC expert.)

And then … it ran. And it looked like this.

...

Gain unlimited access to Premium articles.

With technology shaping our everyday lives, how could we not dig deeper?

Thurrott Premium delivers an honest and thorough perspective about the technologies we use and rely on everyday. Discover deeper content as a Premium member.

Tagged with

Share post

Please check our Community Guidelines before commenting

Windows Intelligence In Your Inbox

Sign up for our new free newsletter to get three time-saving tips each Friday

"*" indicates required fields

This field is for validation purposes and should be left unchanged.

Thurrott © 2024 Thurrott LLC