Your browser doesn't support JavaScript mfc Archives - Page 3 of 3 - Windows Programming

Processing Messages

Since Windows is a message-oriented operating system an application needs to deal with a continual stream of notifications. Each time an event such as a keystroke or mouse click occurs, a message is sent to the application, which must then handle the event. To respond to Windows messages, a MFC application must have a message map. Message maps contain one or more macros that specify which messages will be handled by which function when an application event occurs. MFC then executes the code in the function associated with the message map

The first step in adding message mapping is to declare a message map in the class’s header file using the following macro declaration DECLARE_MESSAGE_MAP(). After declaring the message map in the class header file, the message map can be defined in the class’s implementation file. This message map will start with the macro BEGIN_MESSAGE_MAP and end with the macro END_MESSAGE_MAP. The message map macro takes two arguments: the class name, which implements the message map, and the base class for it. Between the begin and end message map will be listed the macros which represent the categories of messages the operating system will receive. MFC provides macros for over 100 different Windows messages.

As a general rule, the name of a message map prototype will consist of the name of the message preceded by the word ‘on’ and end with a pair of parenthesis which may contain an event handler for this message. For further information on messages that have a system-defined message handler such as ‘OnPaint()’, consult with the MFC documentation. All message handlers are prototyped with the afx_msg type specifier.

Example

In the example below a simple “hello world” application is created by adding a message map to the previous simple MFC program with a handler for the OnPaint message.

Download Code

A Simple MFC Window

The simple CMyApp application below creates a standard window including a title, a system menu and the standard minimise, maximise and a close box. No data member functions are declared and just one virtual function, InitInstance, is overwritten.

#include <afxwin.h>
class CSimpleApp : public CWinApp
{
public:
BOOL InitInstance();
};

class CMainFrame : public CFrameWnd
{
public:
CMainFrame();
};

CSimpleApp MFCApp1;

BOOL CSimpleApp::InitInstance()
{
m_pMainWnd = new CMainFrame();
m_pMainWnd->ShowWindow(m_nCmdShow);
return TRUE;
}

CMainFrame::CMainFrame()
{
Create(NULL, TEXT("MFC Basic Window"));
}

Download Code

Line 1 – AFXWIN.H contains all the core and standard components and must be included in any MFC application.

Line 2 to 6 – To create the most basic application requires that a class must first be derived from the MFC’s CWinApp base class. CWinApp contains functions for initialising and running the application.  The InitInstance member function is inherited from CWinApp and must be overridden by default. Returning true from InitInstance signals that application initialisation can proceed; returning FALSE shuts down the application. InitInstance is the place to perform any initialisations necessary each time the program starts

Line 8 to 12 – The basic functionality of a window is defined in a class called CWnd. The CWnd class gives birth to a derived class called CFrameWnd which describes a window and defines what a window looks like. The description of a window includes items such as its name, size, location, etc.

Line 14 – Instantiates a global application object. There must be only one object of type CWinApp for each application.

16 to 21 – The InitInstance member function creates a new CMainFrame object on the heap. A pointer to this new window object is stored in the public variable m_pMainWnd and it’s through this pointer that the application class maintains contact with its main window. After instantiating the window object, the window is displayed by calling the window object’s ShowWindow() function. ShowWindow accepts just one integer parameter that specifies whether the window should initially be shown minimized, maximized, or neither minimized nor maximized.

23 to 26 – The CMainFrame class constructor, creates the main window by executing the CFrameWnd member function Create. Create returns non-zero if it’s successful and zero if the window cannot be created. 

Further information about the Create function can be found in the link below.
https://docs.microsoft.com/en-us/cpp/mfc/reference/cframewnd-class?view=vs-2019#create