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

Writing Message Map Functions

Now that you have your message map declared and defined, you're missing only one important element: the message map functions themselves! In the example message map, there's only a single message map entry, which is ON_WM_LBUTTONDOWN(). As you now know, this is the macro for the WM_LBUTTONDOWN Windows message. In order to complete the message mapping, you must now write the matching OnLButtonDown() function.

The OnLButtonDown() prototype looks like this:

afx_msg void OnLButtonDown(UINT nFlags, CPoint point);

The afx_msg part of the prototype does nothing more than mark the function as a message map function. You could leave the afx_msg off, and the function would still compile and execute just fine. However, Microsoft programming conventions suggest that you use afx_msg to distinguish message map functions from other types of member functions in your class. To make a message processing you should:

  • To include the macro command into the message map;

  • The prototype of message map function should be included into the class description;

  • The code of message map function is included to the program.

Listing 1.4 The class description with the included map function prototype

class CMainWin : public CFrameWnd

{public: CMainWin();

// Message map functions.

afx_msg void OnLButtonDown(UINT nFlags, CPoint point); // the prototype

DECLARE_MESSAGE_MAP()

};

Listing 1.5 The map function implementation

void CMainFrame::OnLButtonDown(UINT nFlags, CPoint point)

{ MessageBox("Got the click!", "MFC App2");}

As a result you will receive the program reply on your left button click with special message window appearance.

Other example – the map function for program answering on key pressing. Here, we have to react on WM_CHAR message of operational system.

The prototype of the function has the following format:

afx_msg void OnChar( UINT nChar, UINT nRepCnt, UINT nFlags );

The parameters:

nChar   Contains the character code value of the key.

nRepCnt   Contains the repeat count, the number of times the keystroke is repeated when user holds down the key.

nFlags   Contains the scan code, key-transition code, previous key state, and context code.

The next function outputs symbols on the screen.

Listing 1.6 The map function implementation to react on key pressing.

char str[80]; // the string to input

afx_msg void CMainWin::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)

{CClientDC dc(this);

dc.TextOut(1,1,” “, 3);

wsprintf(str,”%c”, ch) ;

dc.TextOut(1, 1, str, strlen(str));}

In this case Windows should link program and the screen. That is a device context or DC (device context). They are provided by declaration of CClientDC type (origined from CDC). Function TextOut() maps the symbol.

The device context is a way, on which the Windows application, using the device driver, outputs the information into the working part of the window, it is a structure, which defines completely the state, driver and the form of information output.

The Map Functions to process mouse messages

The one of most usable messages are WM_LBUTTONDOWN and WM_RBUTTONDOWN. And the prototypes of map functions:

afx_msg void OnLButtonDown(UINT nFlags, CPoint point);

afx_msg void OnRButtonDown(UINT nFlags, CPoint point);

nFlags parameter – points to possible pressing of Ctrl, Alt, Shift button (or other mouse buttons) mutual pressing. The parameters have values: MK_CONTROL, MK_SHIFT, MK_MBUTTON, MK_RBUTTON, MK_LBUTTON.

Loc parameter – contains information on mouse cursor coordinates in the moment of pressing. It’s an object of CPoint class, originated from POINT structure.

typedef struct tagPOINT {

LONG x; // Specifies the x-coordinate of a point

LONG y; // Specifies the y-coordinate of a point

} POINT;

The POINT structure defines the x- and y-coordinates of a point.

Listing 1.7 The map function implementation to react on left mouse button pressing.

afx_msg void CMainWin::OnLButtonDown(UINT flags, CPoint Loc)

{CClientDC dc(this);

wsprintf(str,”Left button is pressed”) ;

dc.TextOut(Loc.x, Loc.y, str, strlen(str));}

The Timer Functions processing

To work with timer MFC uses several functions. Among them are the functions to run or to kill timer.

The timer running is provided by SetTimer finction:

UINT CWnd::SetTimer( UINT nIDEvent, UINT nElapse, void (CALLBACK EXPORT* lpfnTimer)(HWND, UINT, UINT, DWORD) );

nIDEvent   Specifies a nonzero timer identifier.

nElapse   Specifies the time-out value, in milliseconds.

lpfnTimer   Specifies the address of the application-supplied TimerProc callback function that processes the WM_TIMER messages. If this parameter is NULL, the WM_TIMER messages are placed in the application's message queue and handled by the CWnd object.

The timer messages are processed by OnTimer() map function (certainly you should include the corresponding macro command into the Message Map – ON_WM_TIMER()).

Listing 1.8 The map function implementation for timer.

afx_msg void CMainWin::OnTimer(UINT ID)

{CTime curtime = CTime::GetCurrentTime(); // returns the system time value

struct tm *newtime;

newtime = curtime.GetLocalTm();

wsprintf(str, asctime(newtime));

str[strlen(str)-1] = ’\0’;

InvalidateRect(NULL,0); // Invalidates the client area

}

If application finish its work (program send WM_DESTROY message) there will be called new map function – OnDestroy(). So timer will be deleted.

afx_msg void CMainWin::OnDestroy()

{KillTimer(1);}

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