Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
SSW_8_11.doc
Скачиваний:
0
Добавлен:
01.05.2025
Размер:
5.22 Mб
Скачать

Declaring a Message Map and instantiation of application object global instance

Any class that has CCmdTarget as an ancestor can respond to Windows messages. These include application, frame-window, view-window, document, and control classes. Where you decide to intercept a message depends a great deal upon your program's design.

The first step in adding message mapping is to declare a message map in the class's header file. Placing the line

DECLARE_MESSAGE_MAP()

at the end of the class's declaration, just before the closing brace, takes care of this easy task. DECLARE_MESSAGE_MAP() is a macro defined by MFC.

After you have declared your message map in your class's header, it's time to define the message map in your class's implementation file.

What does a message map definition look like? The one that follows maps WM_LBUTTONDOWN messages to a message map function called OnLButtonDown() (the reaction on Left mouse button pressing):

BEGIN_MESSAGE_MAP(CMainWnd, CFrameWnd)

ON_WM_LBUTTONDOWN()

END_MESSAGE_MAP()

The instantiation of application object global instance can be supplied in very simple way. In our case there was special application class CApp, so you can origin your application instance:

CApp App;

So, from that program point any MFC-program begin it’s working.

Defining a Message Map

After you have declared your message map in your class's header, it's time to define the message map in your class's implementation file. Usually, programmers place the message map definition near the top of the file, right after the include lines. However, you can place it just about anywhere in the file as long as it's not nested inside a function or other structure.

What does a message map definition look like? The one that follows maps WM_LBUTTONDOWN messages to a message map function called OnLButtonDown():

BEGIN_MESSAGE_MAP(CMainWin, CFrameWnd)

ON_WM_LBUTTONDOWN()

END_MESSAGE_MAP()

The first line in the preceding code is a macro defined by MFC that takes care of initializing the message map definition. The macro's two arguments are the class for which you're defining the message map and that class's immediate base class.

    1. Messages and their processing in mfc-programs

Windows as operational system interacts with program through messages mechanism. When event, for which program has to answer, occurs, it is received the message. Certainly, there is processed that messages, which are relating to the program, for the rest of messages there are standard message map functions.

Here are the most “popular” messages: WM_CHAR, WMPAINT, WM_MOVE, WM_CLOSE, WM_LBUTTONDOWN, WM_LBUTTONUP, WM_COMMAND, WM_SIZE.

To provide the message processing, you should:

  • The processing macro command is included to the message map;

  • The prototype of map function is included to the class description (class responsible for messages processing);

  • The map functions (it’s implementation) is included to the program.

After the starting macro, you place the message map entries, which match messages with their message map functions. How you do this depends upon the type of message you're trying to map. For example, MFC has previously defined message map functions for all of the Windows system messages, such as WM_LBUTTONDOWN. To write a message map for such a message, you simply add an ON_ prefix to the message name and tack on the parentheses. So WM_LBUTTONDOWN becomes ON_WM_LBUTTONDOWN(); WM_MOUSEMOVE becomes ON_WM_MOUSEMOVE(); WM_KEYDOWN becomes ON_WM_KEYDOWN(), and so on. (Notice that you don't place semicolons after message map entries). To determine the matching message map function for the map entry, you throw away the _WM_ in the macro name and then spell the function name in uppercase and lowercase. For example, ON_WM_LBUTTONDOWN() becomes OnLButtonDown(); ON_WM_MOUSEMOVE() ends up as OnMouseMove(); and ON_WM_KEYDOWN() matches up with OnKeyDown().

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]