Introduction to Message Boxes
|
|
|
A message box is a bordered rectangular window that
displays a short message to the user. The message can be made of one
sentence, various lines, or a few paragraphs. The user cannot change the
text but can only read. A message box is created either to inform the user
about something or to request his or her decision about an issue. When a
dialog box displays from an application, the user must close it before
continuing using the application. This means that, as long as the message
box is displaying, the user cannot access any other part of the application
until the message box is first dismissed.
|
The simplest message box is used to display a message
to the user. This type of message box is equipped with only one button,
labeled OK. Here is an example:
Figure 34: A Simple Message Box
A more elaborate or detailed message box can display
an icon and/or can have more than one button:
Practical
Learning: Starting an MFC Application
|
|
- Start Microsoft Visual Studio
- On the main menu, click File -> New Project...
- In the Project Types, click Visual C++
- In the Templates list, click Win32 Project
- Set the Name to Exercise2
- Click OK
- On the first page of the wizard, click Next
- On the second page, accept Windows Application and click Empty
Project
- Click Finish
- To make sure that this application uses MFC, on the main menu,
click Project -> Properties...
- In the left list, click Configuration Properties
- In the right list, click User of MFC, click the arrow of its combo
box and select Use MFC In A Shared DLL
- Click OK
- In the Solution Explorer, right-click Source Files, position the
mouse on Add, and click New Item...
- In the Templates lists click C++ File (.cpp)
- Set the Name to Exercise
- Click Add
- In the empty document, type the following:
#include <afxwin.h>
class CExerciseApp : public CWinApp
{
BOOL InitInstance()
{
return TRUE;
}
};
CExerciseApp theApp;
- Save the file
- If the Resource View is not visible, on the main menu, click View
-> Other Windows -> Resource View.
In the Reource View, right-click
Exercise1 -> Add -> Resource...
- In the Add Resource dialog box, click String Table
- Click New
- Under the Type column header, click IDS_STRING101 and edit it to
display IDS_ANNOUNCE and press Enter
- To save, on the Standard toolbar, click the Save button
- Close that window by clicking the Close button
- In the Resource View, expand the Exercise1 node et notice the
Exercise1.rc node
To create a message box, the Win32 library provides a
global function called MessageBox. Its syntax is:
int MessageBox(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType);
If you want to use this version from Win32, because it
is defined outside of MFC, you should start it with the scope access
operator “::” as follows:
::MessageBox(. . .);
As we will see shortly, the Win32's MessageBox()
function requires a handle (not necessarily difficult to provide but
still). To make the creation of a message a little easier, the MFC
library provides its own version of the MessageBox() function. Its
syntax is:
int MessageBox(LPCTSTR lpszText,
LPCTSTR lpszCaption = NULL,
UINT nType = MB_OK);
To still make it easier, the MFC framework provides
the AfxMessageBox function used to create a message box. Although
this function is global, its scope is limited to MFC applications. This
means that you can use the Win32's global MessageBox() function
with any compiler used on the Microsoft Windows operating systems but you
cannot use the MFC's global AfxMessageBox() function with any
compiler other Visual C++. The AfxMessageBox() function is
overloaded with two versions whose syntaxes are:
int AfxMessageBox(LPCTSTR lpszText,
UINT nType = MB_OK,
UINT nIDHelp = 0);
int AFXAPI AfxMessageBox(UINT nIDPrompt,
UINT nType = MB_OK,
UINT nIDHelp = (UINT) -1);
Based on the above functions, a message box can be
illustrated as follows:
The Parameters of a Message Box
|
|
The Owner of a Message Box
|
|
As seen above, you have the choice among three
functions to create a message. There is no valid reason that makes one of
them better than the other. They do exactly the same thing. The choice
will be based on your experience and other factors.
If you decide to use the Win32's MessageBox()
function, you must specify the handle to the application that created the
message box. As we will learn eventually when we study controls, you can
get a handle to a (CWnd-derived) control with a call to m_hWnd.
For example, if a button on a dialog box initiates the message box, you
can start this function as follows:
::MessageBox(m_hWnd, …);
We also saw that you can get a pointer to the main
window by calling the MFC's global AfxGetMainWnd() function. This
function only points you to the application. To get a handle to the
application, you can call the same m_hWnd member variable. In this
case, the message box can be started with:
::MessageBox(AfxGetMainWnd()->m_hWnd, …);
If you are creating a message but do not want a
particular window to own it, pass this hWnd argument as
NULL.
If you decide to call the AfxMessageBox()
function, as seen previously, you have two options. You can pass a string
as argument. Here is an example:
AfxMessageBox(L"Welcome to Microsoft Foundation Class Library.");
An alternative is to pass a string identifier from a
resource file.
Practical
Learning: Creating a Message Box
|
|
- In the Resource View, expand the String Table node and
double-click the String Table file
- Click under Caption (on the right side of 101) et type Make
sure you fill out your time sheet before leaving
- Click the Exercise.cpp tab and change the file as follows:
#include <afxwin.h>
#include "resource.h"
class CExerciseApp : public CWinApp
{
BOOL InitInstance()
{
AfxMessageBox(IDS_ANNOUNCE);
return TRUE;
}
};
CExerciseApp theApp;
- To execute the application, press Ctrl + F5
- When asked to build, click Yes
For all the message box functions, the Text
argument a null-terminated string. It specifies the text that would be
displayed to the user. This argument is required. Here is an example that
uses the MessageBox() function:
AfxMessageBox(L"The name you entered is not in our records");
If you want to display the message on various lines,
you can separate sections with the new line character '\n'. Here is an
example:
AfxMessageBox(L"If you think there is a mistake,\nplease contact Human Resources");
If the message you are creating is too long to fit on
one line, you can separate lines by ending each with a double-quote and
starting the next line with a new double-quote. As long as you have not
closed the function, the string would be considered as one entity. You can
also use string editing and formatting techniques to create a more
elaborate message. This means that you can use functions of the C string
library to create your message.
Practical
Learning: Displaying a Message
|
|
- To create a message box, call the AfxMessageBox() function
as follows:
#include <afxwin.h>
#include "resource.h"
class CExerciseApp : public CWinApp
{
BOOL InitInstance()
{
AfxMessageBox(L"The name you entered is not in our records.\n"
L"If you think there is a mistake, please contact HR.\n"
L"You can also send an email to humanres@functionx.com");
return TRUE;
}
};
CExerciseApp theApp;
- Press Ctrl + F5 to test the application and click Yes
- Click OK to close the message box and return to your programming
environment
The caption of the message box is the text that
displays on its title bar. If you create a message box using the
AfxMessageBox() function, it allows you to specify only one argument,
namely the text to display to the user, which is the value of the Text
argument. In this case, the title bar of the message box would display the
name of the application that "owns" the message box.
If you want to display your own caption on the title
bar, call the MessageBox() function. Specify the argument for the
caption. The Caption argument is a null-terminated string that
would display on the title bar of the message box. Here is an example:
MessageBox(NULL,
L"Due to an unknown internal error, this application will now close.",
L"Regular Warning", 0);
Although the Caption argument is optional in
the MFC,s MessageBox() and the AfxMessageBox() functions, it
is required for the Win32's MessageBox() function. Because it is in
fact a pointer, you can pass it as NULL. Here is an example:
MessageBox(NULL,
L"Due to an unknown internal error, this application will now close.",
NULL, 0);
In this case, the title bar of the message box would
display Error:
This caption may not be friendly on most applications
and could appear frightening to the user. Therefore, unless you are in a
hurry, you should strive to provide a friendly and more appropriate title.
Practical
Learning: Displaying a Message's Caption
|
|
- To display a caption for the message box, change the
MessageBox() call as follows:
#include <afxwin.h>
#include "resource.h"
class CExerciseApp : public CWinApp
{
BOOL InitInstance()
{
MessageBox(NULL,
L"The name you entered is not in our records.\n"
L"If you think there is a mistake, please contact HR.\n"
L"You can also send an email to humanres@functionx.com",
L"Failed Logon Attempt", 0);
return TRUE;
}
};
CExerciseApp theApp;
- Test the application
- Return to your programming environment