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 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 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:
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:
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:
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:
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:
If you provide the MB_HELP as the only button, the message box would display with an OK and a Help buttons. |
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:
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:
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:
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:
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;
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:
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 | |
|