Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Chapter 5: The Document/View Architecture Visual C++ and MFC Fundamentals


4.3.1 Definition..............................................................................................


One of the main features of a graphical application is to present Windows controls and
resources that allow the user to interact with the machine. Examples of controls that we
will learn are buttons, list boxes, combo boxes, etc. One type of resource we introduced
in the previous lesson is the menu. Such controls and resources can initiate their own
messages when the user clicks them. A message that emanates from a Windows control
or a resource is called a command message.

4.3.2 Creating a Command Message..........................................................


You can start by declaring a framework method. Here is an example:

afx_msg void OnLetItGo();

Then you can define the method as you see fit:

void CMainFrame::OnLetItGo()
{
// Something to do here
}

The framework allows you to associate a member function to a command message. This
is done using the ON_COMMAND macro. Its syntax:

ON_COMMAND(IDentifier, MethodName)

The first argument, IDentifier, of this macro must be the identifier of the menu item or
Windows control you want to associate with a method.
The second argument, MethodName, must be the name of the member function that will
implement the action of the menu item or Windows control such a button.
Imagine you have a menu item IDentified as ID_ACTION_LETITGO that you want to
associate with the above OnLetItGo() method. You can use the ON_COMMAND macro
as follows:

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
ON_COMMAND(ID_ACTION_LETITGO, OnLetItGo)
END_MESSAGE_MAP()

In the same way, you can create as many command messages as necessary.

4.4 Keyboard Messages.....................................................................................


4.4.1 Introduction...........................................................................................


A keyboard is a hardware object attached to the computer. By
default, it is used to enter recognizable symbols, letters, and other
characters on a control. Each key on the keyboard displays a
symbol, a letter, or a combination of those, to give an indication of
what the key could be used for.

The user typically presses a key, which sends a signal to a program.
The signal is analyzed to find its meaning. If the program or control that has focus is
Free download pdf