Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
SSW_8_11.doc
Скачиваний:
2
Добавлен:
01.05.2025
Размер:
5 Мб
Скачать
☆
    1. Message boxes and menus in mfc-programs

Message boxes

The Message Box outputs the message for user and waits for submission. To make message boxes there is used function MessageBox().MessageBox() creates and displays a window that contains an application-supplied message and caption, plus a combination of the predefined icons and pushbuttons described in the Message-Box Styles list. Use the global function AfxMessageBox instead of this member function to implement a message box in your application.

int MessageBox( LPCTSTR lpszText, LPCTSTR lpszCaption = NULL, UINT nType = MB_OK );

Returning value specifies the outcome of the function. It returns the id of pressed button or 0. That values are IDABORT, IDRETRY, IDIGNORE, IDCANCEL, IDNO, IDYES, IDOK. It is 0 if there is not enough memory to create the message box.

Parameters:

lpszText   Points to a CString object or null-terminated string containing the message to be displayed.

lpszCaption   Points to a CString object or null-terminated string to be used for the message-box caption. If lpszCaption is NULL, the default caption "Error" is used.

nType   Specifies the contents and behavior of the message box.

The following shows the various system icons that can be used in a message box:

Listing 1.9 The map function implementation on left button pressing with message box.

afx_msg void CMainWin::OnLBuittonDown(UINT flags, CPoint loc)

{int i = MessageBox(“Press One”,”Left Button”,MB_ABORTRETRYIGNORE);

switch(i)

{case IDABORT: MessageBox(“”,”Abort”);break;

case IDRETRY: MessageBox(“”,”Retry”);break;

case IDIGNORE: MessageBox(“”,”Ignore”);break;

}

}

If it’s need to output the message box before main window creating there is used a global function AfxMessageBox(). It can be positioned in InitInstance() before CMainWin object creating.

Menus using

Although creating and manipulating MFC menus takes quite a bit of program code, the process is fairly simple. You need only to create the menu with the Visual C++ built-in menu editor, add message response functions to your window class, and add the appropriate entries to your message map. Then, after you load the menu in your program, the menu takes care of itself, and the appropriate message response functions are called whenever the user selects a menu item. The steps for adding menu support to your MFC application are as follows:

  1. Create your menu resource using the Visual C++ menu editor. This process defines the menu titles that will appear in the menu bar as well as the menu commands that'll be in each pop-up menu.

  2. Add menu state variables and message map functions to your window class's declaration. The message map functions can be conventional message response functions or special functions that control the appearance of menu commands.

  3. Add the appropriate entries (using the predefined macros) to your window class's message map table. There are four commonly used macros: ON_COMMAND, ON_COMMAND_RANGE, ON_UPDATE_COMMAND_UI, and ON_UPDATE_COMMAND_UI_RANGE.

  4. Write the message respond and update-command functions that you have listed in your message map. The message response functions respond to the user's commands, whereas the update-command functions determine whether commands are enabled, checked, bulleted, and so on.

As it was already said, menu are included by Create() function. There is used a MAKEINTRESOURCE macro. For instance, if there was declared menu with identification IDR_MENU1, it can be linked with the main program in following way:

Listing 1.9 The constructor of program window with menu.

CMainWin::CMainWin()

{Create(NULL,"Main Window",WS_OVERLAPPEDWINDOW,

rectDefault,NULL,MAKEINTRESOURCE(IDR_MENU1));}

Every menu contains items. The selection of items means sending WM_COMMAND message. In the message map for WM_COMMAND there is used macro ON_COMMAND(): ON_COMMAND(ID, Handler name);

Here id -  The command ID; memberFxn  - The name of the message-handler function to which the command is mapped.

For correct reaction on item selection, we should use following actions:

  1. To include macro ON_COMMAND() into the message map;

  2. To add a prototype of handler to the class declaration;

  3. To add the implementations for all of the handlers.

The creation of menu will be provided by resource editor (see picture below).

For instance, if we have two items in main menu: “First” and “Second” with corresponding ids ID_FIRST and ID_SECOND we should include such macro:

BEGIN_MESSAGE_MAP(CMainWin,CFrameWnd)

ON_COMMAND(ID_FIRST, OnFirst)

ON_COMMAND(ID_SECOND, OnSecond)

END_MESSAGE_MAP()

And now – the implementation of handlers:

afx_msg void CMainWin::OnFirst()

{MessageBox(“First”,”First”);}

afx_msg void CMainWin::OnSecond()

{ MessageBox(“Second”,”Second”);}

Listing 1.11 The full text of simple program to work with left mouse button pressing and mouse moving.

// Main.h: interface for the CMain class.

class CMainWin : public CFrameWnd

{public:

void OnMouseMove(UINT f,CPoint loc);

void OnLButtonDown();

CMainWin();

DECLARE_MESSAGE_MAP()};

class CApp : public CWinApp

{public:

BOOL InitInstance();};

// Main.cpp: implementation of the CMain class.

#include <afxwin.h>

#include "Main.h"

CMainWin::CMainWin ()

{Create(NULL,"Main");} // No Menu defined. For menu see Listing 1.10.

BEGIN_MESSAGE_MAP(CMainWin,CFrameWnd)

ON_WM_LBUTTONDOWN()

ON_WM_MOUSEMOVE()

END_MESSAGE_MAP()

BOOL CApp::InitInstance()

{m_pMainWnd=new CMainWin;

m_pMainWnd->ShowWindow(m_nCmdShow);

m_pMainWnd->UpdateWindow();

return TRUE;

}

CApp App;

void CMainWin::OnLButtonDown()

{MessageBox("Left button is clicked","No Error",MB_ICONEXCLAMATION);}

void CMainWin::OnMouseMove(UINT f, CPoint loc)

{CClientDC dc(this);

dc.TextOut(loc.x,loc.y,"Mouse is here!");}

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