An Overview of MFC, Part 1 - Using the Windows Event Driven Model
(Page 4 of 5 )
When we use MFC we’re provided with placeholders, in the form of member functions of a class. These are invoked when an event occurs, and they are essentially the Event Handlers we’re just talking about.
Say you made a Dialog and created a button for it. How do you handle the clicking of this button? When using C/SDK you’ll have to find a place in the big switch statement under the WM_COMMAND switch to write the handler after checking the wParams and the lParams. But in the MFC world you can make an entry in the MessageMap and implement your handler as a function of the class in which the control (Button) is created.
If you’re not following everything, don’t worry. It will be clear in a little while.
Understanding MFC Code
The best way to see MFC in action is to understand the structure and style of a typical MFC program and see it running. I assume that you have a copy of Visual Studio or Visual Studio .NET to write MFC programs. The code listing below is a simple "hello world" program for MFC. If you’ve never seen any MFC code before it may seem cryptic to you, but you will find it very logical once you understand why is it that way.
//helloworld.cpp
#include <afxwin.h>
// Declare the main application class derived from CWinApp
class CHelloWorldApp : public CWinApp
{
public:
virtual BOOL InitInstance();
};
// Create a Global instance of the application class
CHelloWorldApp HelloWorldApp;
// Declare the main window class derived from CFrameWnd
class CMainWindow : public CFrameWnd
{
CStatic* cs;
public:
// Constructor
CMainWindow ();
};
// The InitInstance function is called each
// time the application first executes.
BOOL CHelloWorldApp::InitInstance()
{
m_pMainWnd = new CMainWindow ();
m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();
return TRUE;
}
// The constructor for the main window class
CMainWindow:: CMainWindow ()
{
// Create the window itself
Create(NULL,
"Hello World App",
WS_OVERLAPPEDWINDOW,
CRect(0,0,200,200));
// Create a static label
cs = new CStatic();
cs->Create("Hello, World",
WS_CHILD|WS_VISIBLE|SS_CENTER,
CRect(50,50,150,75),
this);
}
This 15-20 line program does three things visibly:
- It creates an "application object." Each MFC program needs this in order to handle the initialization details of MFC and Windows.
- The application creates a single window on the screen to act as the main application window for the rest of the application session. If your application does not need user interface elements you may choose a different path, but you’ll do the processing in the constructor itself as shown above.
- Finally, on the main window created in step 2, the application also creates a single static control (like a label in Visual Basic) containing the text "Hello, World."
In the next article we’ll explore these CWinApp and CFrameWnd in great detail -- details that you should know to become a good MFC programmer. But for now we must discuss how to compile and run this little application.
Next: Building our First Application >>
More C# Articles
More By Digvijay Chauhan