Turn: Common usage of the MessageBox in VC

Source: Internet
Author: User

First, about the MessageBox
Message box is a very common control, the property is more, this article lists some of its common methods, and pointed out some of its applications.
1.MessageBox ("This is one of the simplest message boxes!") ");
2.MessageBox ("This is a message box with a caption!") "," title ");
3.MessageBox ("This is an OK cancellation message box! "," title ", Mb_okcancel);
4.MessageBox ("This is a warning message box! "," title ", Mb_iconexclamation);
5.MessageBox ("This is a message box with two properties! "," title ", mb_iconexclamation| Mb_okcancel);

6.if (MessageBox ("A Common Application", "title", mb_iconexclamation| Mb_okcancel) ==idcancel)
Return

Other common properties are attached:

system default icon, which can be displayed on a message box
X Error Mb_iconhand, Mb_iconstop, and Mb_iconerror
? Ask Mb_iconquestion
! Warning Mb_iconexclamation and mb_iconwarning
I information Mb_iconasterisk and mb_iconinformation

        MB_OK default
         mb_okcancel OK to cancel
         mb_yesno
         mb_yesnocancel whether to cancel

return value
IDCANCEL Cancel is selected
Idno No selected
IDOK determined to be selected
Idyes was chosen

NOTE!!! :
The usage of the message box above is applied in the child class of CWnd, if not, then to MessageBox (NULL, "ddd", "ddd", MB_OK); Or MessageBox (HWnd, "ddd", "ddd", MB_OK); The HWND is a handle to a window, or directly with AfxMessageBox.

MessageBox Dialog is a more commonly used Information dialog box, which can not only define the displayed information content, information hint icon, but also can define the button combination and dialog box title, is a full-featured Information dialog box information hint icon, and can define the button combination and dialog box title, is a full-featured information on the box.
1. Function prototype and Parameters
function MessageBox (hwnd:hwnd; Text, Caption:pchar; Type:word): Integer;
hWnd: Dialog Parent window handle, dialog box appears in the Delphi window, you can use the form's handle property, otherwise 0, make it directly as a child window of the desktop window.
Text: The string of information to display.
Caption: Dialog box title string.
Type : Constant for the dialog box.
The return value of the function is an integer that is used for the recognition of the dialog button.
2. Type Constants
The Type constants for dialog boxes can be combined by a combination of buttons, default buttons, display icons, and four constants of run mode.
(1) button combination constants
MB_OK = $00000000; A OK button
mb_okcancel = $00000001; A OK button, a Cancel button
Mb_abortretryignore = $00000002; An abort button, a retry button, a ignore button
mb_yesnocancel = $00000003; One is a button, a No button, a Cancel button
Mb_yesno = $00000004; One is a button, a No button
mb_retrycancel = $00000005; A retry button, a Cancel button
(2) Default button constants
mb_defbutton1 = $00000000; The first button is the default button
mb_defbutton2 = $00000100; The second button is the default button
Mb_defbutton3 = $00000200; The third button is the default button
mb_defbutton4 = $00000300; The fourth button is a default button
(3) icon Constants
Mb_iconhand = $00000010; "x" number icon
mb_iconquestion = $00000020; “? "Number icon
mb_iconexclamation = $00000030; “! "Number icon
mb_iconasterisk = $00000040; "I" Icon
Mb_usericon = $00000080; User Icon
mb_iconwarning = mb_iconexclamation; “! "Number icon
mb_iconerror = Mb_iconhand; "x" number icon
mb_iconinformation = mb_iconasterisk;//"I" Icon
mb_iconstop = Mb_iconhand; "x" number icon
(4) Operating mode constants
mb_applmodal = $00000000; Application mode, which can be switched to another application before the dialog box is closed
mb_systemmodal = $00001000; System mode, you must close the dialog box before you can do other things
mb_taskmodal = $00002000; Task mode, you can switch to another application before the dialog box is closed
mb_help = $00004000; Help Button
3. function return value
0//dialog box failed to build
IDOK = 1/Press the OK button
IDCANCEL = 2/Press the Cancel button
idabout = 3//Press the Abort button
idretry = 4//Press Retry button
Idignore = 5/Press the Ignore button
Idyes = 6/Press Yes button
Idno = 7//Press no button

Second, display the value of the variable in the MessageBox

CString string;

String.Format ("%d%s", M_age, m_address); To assemble a variable into a string

MessageBox (String, "message box caption", message box type);

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

functions < int > MessageBox (< string > Text, < string > Title, < integer > NType);
the function description pops up a message box.
"Syntax"
Parameters:
Text < string; The body of the message box;
Title < string;, the caption of the message box;
NType < integer; the type of the message box.
returns the value of the:< integer; The user clicks the Select button on the message box when it is closed.
"description"
the corresponding dialog box for Ntype is shown in the following table:
0: OK
1: OK, cancel
2: Terminate, retry, ignore
3: Yes, no, cancel
4: Yes, no
5: Retry, Cancel
6: Cancel, try again, continue (note: not supported under Windows NT)

The value corresponding to the return value is as follows:
1: OK
2: Cancel
3: Termination
4: Retry
5: Ignore
6: Yes
7: No
10: Try Again

the message processing program in the application is a very important part of the user to know that the information he entered is not correct

, or if the application has some hints to tell the user, it must display the appropriate information through the prompt information processing program,

Let the user know how to configure the next step. There are many kinds of information, including warnings, errors, prompts, confirmations, retries, etc. No

The same kind of informational boxes are suitable for use in a variety of different situations.

The development of the C++builder in the message box only need to call the MessageBox message box function on the line, here is the

function Description:

int __fastcall MessageBox (const char * text,//The contents of the Hint message box

Const char * Caption,//caption for message box

int Flags); Category of the Hint message box

The values of the flags are the categories used to flag the message boxes, and the following are some common categories:

parameter value style meaning
mb_iconwarning with icon, fixed button warning message box
mb_iconquestion with icon, fixed button question information box
mb_iconerror with icon, fixed button error message box
mb_iconasterisk with icon, fixed button prompt message box
Mb_help does not contain icons, can change the button description information box
MB_OK does not contain icons, can change the button prompt message box
Mb_okcancel does not contain icons, can change the button confirmation message box
Mb_retrycancel does not contain icons, can change the button retry message box
Mb_yesno does not contain icons, can change the button whether the message box
Mb_yesnocancel does not contain icons, can change whether the button cancels the message box

as can be seen from the above table, the message box category can be divided into icons and no icons, including the meaning of the icon refers to the hint

The message box will have a graphical representation of the meaning, but the buttons for this type of message box are fixed, all "OK" buttons,

Do not add or omit the ToolTip without the icon, although there is no corresponding graphic displayed on the prompt message box, but you

in this type of message box, you can add various combinations of buttons for a variety of different situations.


The MessageBox function returns an integer value that represents the button that the user has just clicked on the cue message box, each

The representative meaning of the return value is in the following table:

return value numeric meaning
IDOK 1 OK
IDCANCEL 2 Cancel
Idabort 3 Interrupts
idretry 4 Retry
Idignore 5 ignored
Idyes 6 is
Idno 7 No


The following is a sample program for the prompt message box:

1. Warning message box

Application->messagebox ("warning message box", "warning message box", mb_iconwarning);

2. Question Information Box

Application->messagebox ("Question Information box", "Question Information box", mb_iconquestion);

3. Error Prompt Box

Application->messagebox ("error message box", "Error message box", mb_iconerror);

4. Prompt message box

Application->messagebox ("Prompt message box", "Prompt message box", Mb_iconasterisk);

5. Description Information Box

Application->messagebox ("Description Information box", "Description information box", mb_help);

6. Prompt message box

Application->messagebox ("Prompt message box", "Prompt message box", MB_OK);

7. Confirmation Information Box

Application->messagebox ("Confirmation Information box", "Confirmation Information box", mb_okcancel);

8. Retry message Box

application->messagebox ("Retry message box", "Retry message box", mb_retrycancel);

9. Whether the information box

Application->messagebox ("Whether Information box", "Whether Information box", Mb_yesno);

10. Whether to cancel the message box

Application->messagebox ("Whether to cancel the message box", "whether to cancel the message box", mb_yesnocancel);

in C++builder, there is also a very simple informational cue box function, which is showmessage. This function only

there is a string parameter, you can put the string to be displayed in this string parameter, ShowMessage back to show

this information, but there is no icon, there is no button, only simple string information only.

The following is a description of the ShowMessage function:

extern package void __fastcall showmessage (const ansistring MSG);

Example:

ShowMessage ("This is the informational cue box generated by the ShowMessage function!") ");


Application->messagebox ("AA", "BB", MB_OK | Mb_iconstop);
or
MessageBox (Application->handle, "AA", "BB", MB_OK | Mb_iconstop);
This is the highest level of writing: if you pop up such a dialog box, do not point it out, application all the form
can't "move".

MessageBox (Form1->handle, "AA", "BB", MB_OK | Mb_iconstop);
this writing "level" second: if you pop up such a dialog box, do not point off, only From1 can not "move", the other
Show () from can also point.

MessageBox (NULL, "AA", "BB", MB_OK | Mb_iconstop);
this writing "level" lowest: If you pop up such a dialog box, do not point off, From1 can also "move", other
the From of Show () can also be ordered.

Go: Common use of the MessageBox in VCs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.