Introduction To MFC

Written by:bitshift
Published by:bb
Published on:2004-07-01 05:07:08
Topic:C++

This Article Provides An Introduction To Using The Microsoft Foundation Class or MFC.

Using MFC:
============

Many people think that MFC is a very complex and difficult API. So I've decided to write this article to demystify it.

The first thing you must do when writing an MFC application is define a class for the window you will be creating, so allow me to show you the source code, and then explain it:

#include <afxwin.h>
#include <atlbase.h>
#include <afxtempl.h>

class CSampleWindow : public CFrameWnd {
public:
    CSampleWindow();
protected:
    int OnCreate(LPCREATESTRUCT lpCreateStruct);
    void OnClose();

    DECLARE_MESSAGE_MAP();
};

BEGIN_MESSAGE_MAP(CSampleWindow, CFrameWnd)
        ON_WM_CREATE()
        ON_WM_CLOSE()
END_MESSAGE_MAP()

CSampleWindow::COpenGLWindow() {
    Create(NULL, "Sample MFC Window", WS_OVERLAPPEDWINDOW, rectDefault);
}

int CSampleWindow::OnCreate(LPCREATESTRUCT lpCreateStruct) {
    if(CFrameWnd::OnCreate(lpCreateStruct) == -1) return -1;

    ShowWindow(SW_SHOW);
    return 0;
}

void CSampleWindow::OnClose() {
    DestroyWindow();
}


This code is fairly simple, and much of the Win32 API obfuscation is hidden in the code:

BEGIN_MESSAGE_MAP(CSampleWindow, CFrameWnd)
        ON_WM_CREATE()
        ON_WM_CLOSE()
END_MESSAGE_MAP()


This says that our CSampleWindow application which is a form of a CFrameWnd will handle the messages WM_CREATE and WM_CLOSE. These messages are handled through the pre-defined functions OnCreate(LPCREATESTRUCT lpCreateStruct) and OnClose(). Those of you who have programmed Win32 API will probably recognize these messages.

Now Lets Examine The Constructor and OnCreate().

The constructor calls the CFrameWnd function Create which is inherited by our class. Then we declare our window name, its properties, and a CRect structure defining its size. The variable rectDefault is also inherited from CFrameWnd, and makes up a 320x240 window, of course you can modify this to whatever you'd like.

The OnCreate function handles the create window message. All our version of this function does is pass off window creation to CFrameWnd::OnCreate and then set the window Show Window mode to SW_SHOW, this is to make the window visible.

Now that was quite simple and painless, was it not? In fact you probably didn't even need my explanations of the code. Now lets see how we can get a window showing. Lets append a little something to our previous source code:

#include <afxwin.h>
#include <atlbase.h>
#include <afxtempl.h>

class CSampleWindow : public CFrameWnd {
public:
    CSampleWindow();
protected:
    int OnCreate(LPCREATESTRUCT lpCreateStruct);
    void OnClose();

    DECLARE_MESSAGE_MAP();
};

BEGIN_MESSAGE_MAP(CSampleWindow, CFrameWnd)
        ON_WM_CREATE()
        ON_WM_CLOSE()
END_MESSAGE_MAP()

CSampleWindow::COpenGLWindow() {
    Create(NULL, "Sample MFC Window", WS_OVERLAPPEDWINDOW, rectDefault);
}

int CSampleWindow::OnCreate(LPCREATESTRUCT lpCreateStruct) {
    if(CFrameWnd::OnCreate(lpCreateStruct) == -1) return -1;

    ShowWindow(SW_SHOW);
    return 0;
}

void CSampleWindow::OnClose() {
    DestroyWindow();
}

/////////////////////////////////////////

class CSampleApp : public CWinApp {
public:
    virtual BOOL InitInstance();
};

BOOL CSampleApp::InitInstance() {
    m_pMainWnd = new CSampleWindow();
    return TRUE;
}

CSampleApp sampleApp;


In the function InitInstance, which is called from the CSampleApp constructor, we create an instance of our CSampleWindow which we place into the variable m_pMainWnd which is inherited from CWinApp. Finally we create an instance of CSampleApp. This is officially enough source code to display a window.

NOTE: When trying to compile this in MSVC you may come up against a linker error. Simply go to Project -> Settings, and make sure you are using "Use MFC in a shared DLL".

Hopefully this has helped you get started with MFC.

This is an article from http://www.osix.net - view the original at: http://www.osix.net/modules/article/?id=537