Home

The Win32 Message Box

   

Introduction

To support message boxes, the Win32 library provides a function named MessageBox(). A message box in Microsoft Windows appears as follows

 

Because the TApplication class represents Win32 in a VCL application, this function can be called by qualifying it to the Application global variable.

The Message of the Box

The MessageBox() function takes three arguments. The first argument, Message, is a string representing the message that the user would read. The Message string could be a static sentence. It could be constructed from another control. Or it could be a combination of different string functions and operations.

 

You can create a simple message box similar to one implemented using the ShowMessage() function to display a simple message with an OK button. In this case, you would provide only the Message argument. Passed the other two arguments as nil. Here is an example:

procedure TForm1.btnMessageBoxClick(Sender: TObject);
begin
    Application.MessageBox('This operation can only be ' +
			   'performed by an administrator.',
			   0, 0);
end;

The Caption of the Message Box

The second argument, also a string, is the caption that would display on the title bar of the dialog box. You can also set it when creating the message box or you can build it from what would be available at runtime. If you do not have a caption, you can set the value of this argument as 0. In that case the title bar would display Error. For example, the above code would produce:

Message Box

Therefore, to create a less boring message box, provide the Caption argument. Here is an example:

procedure TForm1.btnMessageBoxClick(Sender: TObject);
begin
    Application.MessageBox('Make sure the music is playing.',
			   'CD PLayer Instructions', 0)
end;

This would produce:

Message Box

The Buttons on the Message Box

The third argument specifies the flags that would display on the dialog box: one or more buttons and an optional picture. Besides, or apart from, the OK button, if you want to display other buttons, you can pass the third argument with one of the following constants:

Constant Buttons
MB_OK OK
MB_OKCANCEL OK Cancel
MB_ABORTRETRYIGNORE Abort Retry Ignore
MB_YESNOCANCEL
MB_YESNO Yes No
MB_RETRYCANCEL Cancel
MB_HELP Help

You can create a simple message box with OK as the only button. In that case, set the third argument as MB_OK. Here is an example:

procedure TForm1.btnMessageBoxClick(Sender: TObject);
begin
    Application.MessageBox('Make sure the music is playing.',
			   'CD PLayer Instructions', MB_OK)
end;

This would produce:

Message Box

To create a message box that displays the Yes and No buttons, you could write:

procedure TForm1.btnMessageBoxClick(Sender: TObject);
begin
    Application.MessageBox('Do you hear any music now or any sound at all?',
			   'CD PLayer Instructions', MB_YESNO)
end;

This would produce:

Message Box

If you provide the MB_HELP as the only button, the message box would display with an OK and a Help buttons.

The Icon on the Message Box

To enhance a dialog box and accentuate its message, you can display an icon using one of the Win32 defined integer constants. Although you can use any icon with any button, you should be selective and make sure that the appearance of the icon you use is in accordance with the message. To specify the icon, you use one of the following constants:

Value Icon Suited when
MB_ICONEXCLAMATION
MB_ICONWARNING
Warning the user of an action performed on the application
MB_ICONINFORMATION
MB_ICONASTERISK
Informing the user of a non-critical situation
MB_ICONQUESTION Asking a question that expects a Yes or No, or a Yes, No, or Cancel answer
MB_ICONSTOP
MB_ICONERROR
MB_ICONHAND 
A critical situation or error has occurred. This icon is appropriate when informing the user of a termination or deniability of an action

The icons are used in conjunction with the buttons constant. To combine these two flags, use the bitwise Or operator. Here is an example:

procedure TForm1.btnMessageBoxClick(Sender: TObject);
begin
    Application.MessageBox('Do you hear any music now or any sound at all?',
			   'CD PLayer Instructions',
			   MB_YESNOCANCEL Or MB_ICONQUESTION)
end;

This would produce:

Message Box

The Default Button on the Message Box

When a message box is configured to display more than one button, the operating system is set to decide which button is the default. The default button has a thick border that sets it apart from the other button(s). If the user presses Enter, the message box would behave as if the user had clicked the default button. Fortunately, if the message box has more than one button, you can decide what button would be the default. To specify the default button, use one of the following constants:

Value If the message box has more than one button, the default button would be
MB_DEFBUTTON1 The first button
MB_DEFBUTTON2 The second button
MB_DEFBUTTON3 The third button
MB_DEFBUTTON4 The fourth button

To specify the default button, use the bitwise OR operator to combine the constant integer of the desired default button with the button's constant and the icon. Here is an example:

procedure TForm1.btnMessageBoxClick(Sender: TObject);
begin
    Application.MessageBox('Do you hear any music now or any sound at all?',
	  		    'CD PLayer Instructions',
	                    MB_YESNOCANCEL Or MB_ICONQUESTION Or MB_DEFBUTTON2)
end;

This would produce:

Message Box

Since the combination of these buttons is using the Or bitwise operator to construct the Flags argument, it does not make a difference which constant appears first:

procedure TForm1.btnMessageBoxClick(Sender: TObject);
begin
    Application.MessageBox('Do you hear any music now or any sound at all?',
			   'CD PLayer Instructions',
			   MB_YESNOCANCEL Or MB_DEFBUTTON3 Or MB_ICONQUESTION)
end;

The Returned Value of the Message Box

After reading the message displaying on the dialog box, the user would click one of the buttons and the dialog would be closed. Each one of the buttons has a constant number that is assigned and recognized by the compiler. You can use this number to find out what button the user had clicked. This means that the MessageBox() function returns an integer value as in the following table:

Displayed Button(s) If the user clicked The return value is
OK OK IDOK
OK Cancel OK IDOK
OK Cancel Cancel IDCANCEL
Abort Retry Ignore Abort IDABORT
Abort Retry Ignore Retry IDRETRY
Abort Retry Ignore Ignore IDIGNORE
Yes No Cancel Yes IDYES
Yes No Cancel No IDNO
Yes No Cancel Cancel IDCANCEL
Yes No Yes IDYES
Yes No No IDNO
Retry Cancel Retry IDRETRY
Retry Cancel Cancel IDCANCEL

Therefore, you can use one of these integers to act depending on the button clicked:

procedure TForm1.btnMessageBoxClick(Sender: TObject);
begin
    if Application.MessageBox(
			'Do you hear any music now or any sound at all?',
			'CD PLayer Instructions',
			MB_YESNOCANCEL Or MB_ICONQUESTION) = IDNO then
	ShowMessage('We will stop these tests now. ' +
		    'Let the machine rest!')
end;
 
 
 
 

Home Copyright © 2010-2016, FunctionX