|
|
|
- Start Microsoft Visual C++ and open the MsgBoxes
project.
- Click the ResourceView tab. Expand the MsgBoxes resources, if necessary. Expand the
Dialog folder. Double-click IDD_MSGBOX_SIMPLE.
- Drop a Button on the dialog. Change the identifier of the button to
IDC_SIMPLEST_BTN and its caption to &Simples Message
Box.
- Double-click the new button. That create a function for the BN_CLICKED
message with a suggested name. Accept the name of the function and implement
it as follows:
void CMsgBoxSimple::OnSimplestBtn()
{
MessageBox("This is the simplest message box");
}
- Add another button on the IDD_MSGBOX_SIMPLE
page identified as IDC_SIMPLE_BTN with a
caption of A Simple &Message Box.
Double-click the new button, accept the suggested name of the function and
implement it as follows:
void CMsgBoxSimple::OnSimpleBtn()
{
AfxMessageBox("Another simple message box");
}
- In the ResourceView, double the IDD_MSGBOX_OPTIONS page to open it. Add the following buttons
to the property page:
Identifier |
Caption |
IDC_OK_BTN |
OK |
IDC_OKCANCEL_BTN |
OK or Cancel |
IDC_YESNO_BTN |
Yes or No |
IDC_YESNOCANCEL_BTN |
Yes, No, or Cancel |
IDC_RETRYCANCEL_BTN |
Retry or Cancel |
IDC_ABORTRETRYIGNORE_BTN |
Abort, Retry, or Cancel |
- Add an edit box at the bottom of the page with IDC_MESSAGE as the
identifier.
- Access the ClassWizard property sheet. In MFC ClassWizard, click Member
Variables, double-click IDC_MESSAGE, and add a CString
variable named m_Message.
- Still in MFC AppWizard, click the Message Maps property page, and make sure that
MsgBoxes is selected in the Project combo box. In the Class Name combo box,
select the CMsgBoxOptions class, if necessary. In the Object IDs list box, click
IDC_OK_BTN, in the Messages list box, double-click BN_CLICKED, accept the
suggested name for the function.
- Add a function for the following buttons: IDC_OKCANCEL_BTN,
IDC_YESNO_BTN, IDC_YESNOCANCEL_BTN,
IDC_RETRYCANCEL, IDC_ABORTRETRYCANCEL_BTN.
- Implement the functions as follows:
void CMsgBoxOptions::OnOkBtn()
{
MessageBox("Message with only one button, OK?",
"Exploring Message Boxes",
MB_OK|MB_ICONINFORMATION);
m_Message = "You didn't have any choice";
UpdateData(FALSE);
}
void CMsgBoxOptions::OnOkcancelBtn()
{
int Response;
Response =
MessageBox("In this message, you can accept by clicking OK"
"or dismiss with Cancel"
"\n\nYou can write a long message on multiple lines"
"\non any message box",
"Exploring Message Boxes",
MB_OKCANCEL|MB_ICONASTERISK);
if( Response == IDOK )
{
m_Message = "So you accept the mission.";
UpdateData(FALSE);
}
else
{
m_Message = "You didn't even try.";
UpdateData(FALSE);
}
}
void CMsgBoxOptions::OnYesnoBtn()
{
int Response;
Response =
MessageBox("Did you eat already, I mean since yesterday?",
"Amazing Stomach To Feed", MB_YESNO|MB_ICONQUESTION);
if( Response == IDYES )
{
m_Message = "Then, I don't have to give you anything.";
UpdateData(FALSE);
}
else
{
m_Message = "Well, sorry,
I ain't got no food for your stomach!.";
UpdateData(FALSE);
}
}
void CMsgBoxOptions::OnYesNoCancelBtn()
{
int Response;
Response =
MessageBox("This one would come up when you delete a folder"
"\nor perform an action that needs"
"confirmation of the action.",
"Folder Or File Deletion Message",
MB_YESNOCANCEL|MB_ICONWARNING);
if( Response == IDYES )
{
m_Message = "Now the action will proceed.";
UpdateData(FALSE);
}
else if( Response == IDNO )
{
m_Message = "Mission dismissed.";
UpdateData(FALSE);
}
else
{
m_Message = "Abort Action. See you next time";
UpdateData(FALSE);
}
}
void CMsgBoxOptions::OnRetryCancelBtn()
{
int Response;
Response =
MessageBox("I know you can, I know you can, I know you can."
"\nJust keep trying",
"Trying Various Alternatives",
MB_RETRYCANCEL|MB_ICONWARNING);
if( Response == IDRETRY )
{
m_Message = "Yes, keep trying, until...";
UpdateData(FALSE);
}
else
{
m_Message = "Excuse me? Finish what you have started.";
UpdateData(FALSE);
}
}
void CMsgBoxOptions::OnAbortRetryIgnoreBtn()
{
int Response;
Response =
MessageBox("This is the nastiest message box,
"it usually means trouble.",
"Trouble On Horizon",
MB_ABORTRETRYIGNORE|MB_ICONSTOP);
if( Response == IDYES )
{
m_Message = "Thank God. You got me worried!";
UpdateData(FALSE);
}
else if( Response == IDNO )
{
m_Message = "You are stubborn, aren't you?";
UpdateData(FALSE);
}
else
{
m_Message = "Right, right, right. Change your mind.";
UpdateData(FALSE);
}
}
- By default, the first button on a message box is the default button.
Fortunately, using the SDK's MessageBox function, you can change the default
button as MB_DEFBUTTON1 (the default), MB_DEFBUTTON2, MB_DEFBUTTON3, or
MB_DEFBUTTON4.
In the ResourceView, double-click the IDD_MSGBOX_CUSTOM. Add a button
identified as IDC_SECONDDEFAULT_BTN and with
the caption Second Button Default.
- Add a second button identified as IDC_THIRDDEFAULT_BTN
and with a caption of Third Button Default.
- Double-click the IDC_SECONDDEFAULT_BTN button, accept the suggested name
of the function and implement it as follows:
void CMsgBoxCustom::OnSeconddefaultBtn()
{
int BtnClicked;
BtnClicked =
MessageBox("If you press Enter, the default button will be"
"accessed.\nOtherwise, click one of the buttons.",
"Changing Default Button",
MB_YESNOCANCEL|MB_ICONINFORMATION|MB_DEFBUTTON2);
if( BtnClicked == IDYES )
MessageBox("You clicked the first button: YES");
else if( BtnClicked == IDNO )
MessageBox("You clicked No or
you accepted the default button.");
else
MessageBox("You clicked the third button");
}
- Implement the function of the other button as follows:
void CMsgBoxCustom::OnThirddefaultBtn()
{
int BtnClicked;
BtnClicked =
MessageBox("If you press Enter, the default button will be"
"accessed.\nOtherwise, click one of the buttons.",
"Changing Default Button",
MB_ABORTRETRYIGNORE|MB_ICONSTOP|MB_DEFBUTTON3);
if( BtnClicked == IDABORT )
MessageBox("You clicked the first button: ABORT");
else if( BtnClicked == IDRETRY )
MessageBox("You clicked the second button: RETRY");
else
MessageBox("You clicked Ignore or
you accepted the default button.");
}
|