Part IV: Message mapping
Any user interface object that an application puts in a window has two controllable features: 1 its appearance, and 2 it responds to the behavior of the event. In the last lecture, you've learned about CStatic control and how to use style attributes to customize the appearance of user interface objects. These concepts can be used for all the different control classes in MFC.
In this lecture, we will introduce CButton control to understand message mapping and simple event handling. Then you will also introduce a slightly more complex example of using CScrollBar control.
Understanding Message Mappings
In the second lecture, the MFC program does not include the primary function or the time loop. All event handling is handled in the background as part of the CWinApp. Because they are hidden, we need a way to tell an invisible time loop about events that are of interest to our application. This requires a mechanism called message mapping. Message maps identify events of interest and then call functions to respond to these events.
For example, if you are writing a program, quit the application when the user presses the button labeled "Exit". In a program, you write code to create a button: You indicate how the button should act. Then, a message map is created for its parent window when the user clicks the button, and it attempts to pass the message to its parent window. To create a message for the parent window, you create a mechanism to intercept the message map and use the message of the button. When a specified button event occurs, the message map requests MFC to call a specified function. In this case, the single repel out button is the event of interest. Then you put the code that exits the application into the specified function.
The rest of the work is done by MFC. When the program executes, the button highlights itself when the user clicks the Exit button. MFC then automatically calls the corresponding function, and the program terminates. With only a few lines of code, you respond to user events.
CButton class
The CStatic control discussed in the last lecture is the only control that does not respond to user time. All other controls in Windows can respond to user events. First, when users work with them, they automatically update their appearance (for example, when a user clicks a button, the button lightens itself to give the user a feedback). Second, each control has to send a message to your code so that the program can respond to the user's needs. For example, when a button is clicked, the button sends a command message. If you write code to receive messages, your code can respond to user events.
To understand this process, we start with CButton control. The following code illustrates the process of creating a button:
Button1.cpp
#include
#define Idb_button 100
Declare the Application class
Class Cbuttonapp:public CWINAPP
{
Public
Virtual BOOL InitInstance ();
};
Create an instance of the application class
Cbuttonapp Buttonapp;
Declare the main window class
Class Cbuttonwindow:public CFrameWnd
{
CButton *button;
Public
Cbuttonwindow ();
};
The InitInstance function is called once
When the application-executes
BOOL cbuttonapp::initinstance ()
{
m_pMainWnd = new Cbuttonwindow ();
M_pmainwnd->showwindow (m_nCmdShow);
M_pmainwnd->updatewindow ();
return TRUE;
}
The constructor for the window class
Cbuttonwindow::cbuttonwindow ()
{
CRect R;
Create the window itself
Create (NULL,
"CButton Tests",
Ws_overlappedwindow,
CRect (0,0,200,200));
Get the size of the client rectangle
GetClientRect (&R);
R.inflaterect ( -20,-20);
Create a button
button = new CButton ();
Button->create ("Push Me",
ws_child| Ws_visible|bs_pushbutton,
R
This
Idb_button);
}