Development of Windows 8 Metro sample based on visualstudio11 (1) MessageBox

Source: Internet
Author: User

MessageBox.

 

Function prototype: essagebox. Show (text, title, messageboxbuttons, messageboxicon, messageboxdefaultbuttons)

Parameter description:

(1) Text: required. The text of the message box.

(2) Title: optional, the title of the message box.

(3) messageboxbuttons: Optional. Set the message box button. By default, only the "OK" button is displayed.

OK-confirm okcancel-confirm and cancel abortretryignore-terminate, retry, and ignore yesnocancel-yes, no, and cancel yesno-yes and no retrycancel-retry and cancel (4) messageboxicon: the icon style displayed in the dialog box. No icon is displayed by default. Question-question mark information, asterisk-I error, stop, hand-error no. Warning, exclamation ――! No. None-No icon is displayed

(5) messageboxdefaultbuttons: Optional. The selected button is selected by default in the dialog box. Defaultbutton1-1st buttons are default buttons defaultbutton2-2nd buttons are default buttons defaultbutton3-3rd buttons are default buttons

Note: The parameters in the blue font section of the function prototype can be obtained through vertices.
MessageBox (null, "text", "title", button );

Parameter title: string type, which specifies the title of the message dialog box. Text: Specifies the message displayed in the message dialog box. This parameter can be a numerical data type, string, or Boolean value. Icon: icon Enumeration type. Optional. It specifies the icon to be displayed on the left of the dialog box. Button: Specifies the button that is displayed at the bottom of the dialog box. Default: numeric type, optional. It indicates the number of buttons used as the default button. The number of buttons is counted from left to right. The default value is 1. If the number specified by this parameter exceeds the number of buttons displayed, the MessageBox () function returns an integer using the default value. If the function is successfully executed, the user-selected button numbers (such as 1, 2, and 3) are returned, and-1 is returned in case of an error. If the value of any parameter is null, The MessageBox () function returns NULL.
For example, if you want to pop up a window, the command is: MessageBox (null, "messageboxtext (content)", "title (title)", mb_ OK); if you want to pin int iret; iret = MessageBox (hwnd, "PLC alarm! "," Dialog box ", mb_yesno | mb_iconquestion | mb_systemmodal); or iret = MessageBox (null," PLC alarm! "," Dialog box ", mb_yesno | mb_iconquestion | mb_defbutton1 | mb_systemmodal); add a focus to make the first button more effective.

The parameters are as follows:

Hwnd: identifies the owned window of the message box to be created. If this parameter is null, the message box does not have a window.

Lptext: a pointer to a string ending with null that contains messages to be displayed.

Lpcaption: pointer to a string ending with NULL for the dialog box title.

Utype: Specifies a bit flag set that determines the content and behavior of the dialog box. This parameter can be a combination of the following flag groups.

Specify one of the following logos to display the buttons in the message box. The meanings of the logos are as follows.

Mb_abortretryignore: the message box contains three buttons: abort, retry, and ignore.

Mb_ OK: the message box contains a button: OK. This is the default value.

Mb_okcancel: the message box contains two buttons: OK and cancel.

Mb_retrycancel: the message box contains two buttons: retry and cancel.

Mb_yesno: the message box contains two buttons: yes and no.

Mb_yesnocancel: the message box contains three buttons: yes, no, and cancel.

Specify one of the following icons to display the icons in the message box.

Mb_iconexclamation:

Mb_iconwarning: an exclamation point appears in the message box.

Mb_iconinformation:

Mb_iconasterisk: An icon consisting of letters I in a circle appears in the message box.

Mb_iconquestion: a question mark icon appears in the message box.

Mb_iconstop:

Mb_iconerror:

Mb_iconhand: A stop message icon appears in the message box.

Specify one of the following icons to display non-default buttons: The meanings of the icon are as follows.

Mb_defbutton1: The first button is the default button. If mb_defbutton2, mb_defbutton3, and mb_defbutton4 are not specified, mb_defbutton1 is the default value.

Mb_defbutton2; the second button is the default button.

Mb_defbutton3: the third button is the default button.

Mb_defbutton4: The fourth button is the default button.

Specify one of the following logos to display the dialog box format: the meanings of the logos are as follows.

Mb_applmodal: Before you continue to work in the window marked by the hwnd parameter, you must respond to the message box. However, you can move to windows of other threads and work in these windows. Based on the window hierarchy in the application, the user moves to another window in the thread. The subwindows of all parent message boxes automatically expire, but the pop-up window does not. If neither mb_systemmodal nor mb_taskmooal is specified, mb_applmodal is the default value.

Mb_systemmodal: Besides the wb_ex_topmost type in the message box, the mb_applmodal is the same as ws_ex_topmost. Use the system modal message box to change various users. Pay attention to major damage errors (for example, memory overflow ). If it is not for Windows that are connected with hwnd, this flag does not affect the user's interaction with windows.

Mb_taskmodal: If the hwnd parameter is null, mb_taskmodall is the same as mb_applmodal except for all windows that belong to the current thread's high level are invalid. This flag is used when no window handle is available for calling an application or library. However, you still need to stop the input in other windows of calling application a, rather than shelving other threads.

In addition, you can specify the following flag.

Mb_default_shorttop_only: The current desktop that receives input must be a default desktop. Otherwise, the function call fails. The default desktop is a desktop on which applications run after the user logs.

Mb_help: Add a Help button to the message box. Select the Help button or Press F1 to generate a help event.

Mb_right: adjust the text to the right.

Mb_rtlreading: displays messages and uppercase text from right to left in Hebrew and Arabic systems.

Mb_setforeground: the message box is changed to the foreground window. In the internal system, call the setforegroundwindow function for messages.

Mb_topmosi: the message box uses the ws_ex_topmost window type to create mb_service_notification.

Windows NT: The calling program is a service program of the user who notifies the event. The function displays a message box on the current active desktop, even if no user is registered with the computer.

If this parameter is set, the hwnd parameter must be null. Therefore, the message box can appear on a desktop instead of the desktop Response Parameter hwnd.

For Windows NT 4.0, the value of mb_service_notification has changed. For old and new values, see winuser.

Windows NT 4.o maps old values to new values in MessageBox and messageboxex execution to provide inverse compatibility for existing services. This ing is only performed for executable programs with version numbers.

To create a server with mb_service_notification, you can run the command on Windows NT 3.x and Windows NT 4.0. Specify a version earlier than 4.0 at the connection time, or specify a version later than 4.0 at the connection time. During running time, use the getversionex function to detect the system version, run it on Windows NT 3.x using mb_service_notification_nt 3.x, and run it on Windows NT 4.0 using mb_service_notificaion. Mb_servce_icationication_nt3.x (windob7-) this value responds to the mb_service_notificaion for windownt3.51

Defined value.

 

 

The returned values are as follows:

 

If there is not enough memory to create a message box, the return value is zero. If the function is successfully called, the return value is one of the menu item values returned by the following dialog box:

Idabort: the abort button is selected. Idcancel: The cancel button is selected. Idignore: The ignore button is selected.

IDNO: The no button is selected. Idok: The OK button is selected. Idretry: the Retry button is selected.

Idyes: The Yes button is selected.

If a message box has a Cancel button and the ESC key is pressed or the cancel key is selected, the function returns the idcancel value. If the message box does not have the cancel button, pressing ESC does not work.

The return value of MessageBox is defined by default. The return value of MessageBox is actually an integer. The following is the default definition.

# Define idok 1

# Define idcancel 2

# Define idabort 3

# Define idretry 4

# Define idignore 5

# Define idyes 6

# Define IDNO 7

# If (winver> = 0x0400)

# Define idclose 8

# Define idhelp 9

Next, let's practice MessageBox in Windows 8.

 

Check the program initialization code.

#include "pch.h"#include "MainPage.xaml.h"#include "App.xaml.h"using namespace Windows::UI::Xaml;using namespace Windows::UI::Xaml::Controls;using namespace Windows::UI::Xaml::Data;using namespace Windows::System;using namespace Windows::Foundation;using namespace Platform;using namespace MessageDialog;using namespace CppSamplesUtils;using namespace Windows::UI::Xaml::Navigation;using namespace Windows::UI::Xaml::Interop;using namespace Windows::UI::ViewManagement;using namespace Windows::Graphics::Display;MainPage::MainPage(){    InitializeComponent();    SetFeatureName("MessageDialog");    _scenariosFrame = ScenarioList;    _inputFrame = ScenarioInput;    _outputFrame = ScenarioOutput;    _layoutHandlerToken = ApplicationView::GetForCurrentView()->ViewStateChanged +=    ref new TypedEventHandler<ApplicationView^, ApplicationViewStateChangedEventArgs^>(this, &MainPage::Page_ViewStateChanged);    _pageLoadedHandlerToken = Loaded += ref new RoutedEventHandler(this, &MainPage::Page_Loaded);    _logicalDpiChangedToken = DisplayProperties::LogicalDpiChanged += ref new DisplayPropertiesEventHandler(this, &MainPage::DisplayProperties_LogicalDpiChanged);}MainPage::~MainPage(){}void MainPage::Page_Loaded(Object^ sender, RoutedEventArgs^ e){        TypeName pageType = { "MessageDialog.ScenarioList", TypeKind::Custom };    ScenarioList->Navigate(pageType, this);    CheckResolutionAndViewState();}void MainPage::SetFeatureName(String^ strFeature){    FeatureName->Text = strFeature;}void MainPage::Page_ViewStateChanged(ApplicationView^ sender, ApplicationViewStateChangedEventArgs^ e){    CheckResolutionAndViewState();}void MainPage::DisplayProperties_LogicalDpiChanged(Object^ sender){    CheckResolutionAndViewState();}void MainPage::CheckResolutionAndViewState(){    ::ApplicationViewState state = ApplicationView::Value;    String^ stateString = ConvertViewState(state);    ::ResolutionScale scale = DisplayProperties::ResolutionScale;    String^ scaleString = ConvertResolution(scale);    VisualStateManager::GoToState(this, stateString + scaleString, false);}String^ MainPage::ConvertViewState(::ApplicationViewState state){    switch (state)    {    case ::ApplicationViewState::Filled:        return "Filled";    case ::ApplicationViewState::FullScreenLandscape:        return "FullScreenLandscape";    case ::ApplicationViewState::FullScreenPortrait:        return "FullScreenPortrait";    case ::ApplicationViewState::Snapped:        return "Snapped";    }    return "";}String^ MainPage::ConvertResolution(::ResolutionScale scale){    switch (scale)    {    case ::ResolutionScale::Scale100Percent:        return "Scale100Percent";    case ::ResolutionScale::Scale140Percent:        return "Scale140Percent";    case ::ResolutionScale::Scale180Percent:        return "Scale180Percent";    }    return "";}void MainPage::DoNavigation(TypeName pageType, ::Frame^ frame){    frame->Navigate(pageType, this);    std::wstring PageName(pageType.Name->Data());    std::basic_string <wchar_t>::size_type indexSubstring;    indexSubstring = PageName.find(L"Output");    if (indexSubstring != std::wstring::npos)    {               if (OutputFrameLoaded != nullptr)        {            OutputFrameLoaded(this, nullptr);        }    }    else    {        if (InputFrameLoaded != nullptr)        {            InputFrameLoaded(this, nullptr);        }    }}void MainPage::NotifyUser(String^ strMessage, NotifyType type){    switch (type)    {    case NotifyType::StatusMessage:        StatusBlock->Style = dynamic_cast<::Style^>(App::Current->Resources->Lookup("StatusStyle"));        break;    case NotifyType::ErrorMessage:        StatusBlock->Style = dynamic_cast<::Style^>(App::Current->Resources->Lookup("ErrorStyle"));        break;    default:        break;    }    StatusBlock->Text = strMessage;}void MainPage::Footer_Click(Object^ sender, RoutedEventArgs^ e){    auto _hyperlinkButton = (HyperlinkButton^)sender;    auto uri = ref new ::Uri((String^)_hyperlinkButton->Tag);    ::Launcher::LaunchUriAsync(uri);}

Dialog Box 1

#pragma region Click handlersvoid ScenarioInput1::Scenario1Launch_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e){     Windows::UI::Popups::MessageDialog^ msg = ref new Windows::UI::Popups::MessageDialog("You've exceeded your trial period.");    IAsyncOperation<IUICommand^>^ operation = msg->ShowAsync();     operation->Completed = ref new AsyncOperationCompletedHandler<IUICommand^>([this](IAsyncOperation<IUICommand^>^ op, AsyncStatus status)    {        if (status == AsyncStatus::Completed)        {            this->NotifyUserHandler();            op->Close();        }        else        {            rootPage->NotifyUser("Message dialog returned an error", NotifyType::StatusMessage);        }    });    }void ScenarioInput1::NotifyUserHandler(){    InvokedHandler^ callback = ref new InvokedHandler([this](Object^ sender, IInvokedHandlerArgs^ eInvoke)    {        rootPage->NotifyUser("The dialog has now been closed", NotifyType::StatusMessage);    });    Dispatcher->InvokeAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, callback, this, nullptr);}#pragma endregion

Dialog Box 2

using namespace MessageDialog;using namespace Windows::UI::Core;using namespace Windows::UI::Popups;using namespace Windows::UI::Xaml::Navigation;using namespace Windows::Foundation;using namespace Platform;ScenarioInput2::ScenarioInput2(){    InitializeComponent();}ScenarioInput2::~ScenarioInput2(){}void ScenarioInput2::OnNavigatedTo(NavigationEventArgs^ e){       rootPage = dynamic_cast<MainPage^>(e->Parameter);    _frameLoadedToken = rootPage->OutputFrameLoaded += ref new Windows::Foundation::EventHandler<Platform::Object^>(this, &ScenarioInput2::rootPage_OutputFrameLoaded);}void ScenarioInput2::OnNavigatedFrom(NavigationEventArgs^ e){    rootPage->OutputFrameLoaded -= _frameLoadedToken;}#pragma endregion#pragma region Output framevoid ScenarioInput2::rootPage_OutputFrameLoaded(Object^ sender, Object^ e){    // Get a pointer to the content within the OutputFrame    Page^ outputFrame = dynamic_cast<Page^>(rootPage->OutputFrame->Content);}#pragma endregion#pragma region Click handlersvoid ScenarioInput2::Scenario2Launch_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e){       Windows::UI::Popups::MessageDialog^ msg = ref new Windows::UI::Popups::MessageDialog("You have exceeded your trial period. Would you like to continue your trial or upgrade to the full version?");      UICommand^ continueCommand = ref new UICommand("Continue trial", ref new UICommandInvokedHandler(this, &ScenarioInput2::CommandInvokedHandler, CallbackContext::Same));    UICommand^ upgradeCommand = ref new UICommand("Upgrade", ref new UICommandInvokedHandler(this, &ScenarioInput2::CommandInvokedHandler, CallbackContext::Same));    msg->Commands->Append(continueCommand);    msg->Commands->Append(upgradeCommand);    msg->ShowAsync();}void ScenarioInput2::CommandInvokedHandler(Windows::UI::Popups::IUICommand^ command){    String ^buttonLabel = command->Label;    rootPage->NotifyUser("The '" + buttonLabel + "' button has been selected.", NotifyType::StatusMessage);}#pragma endregion

 

Dialog Box 3

using namespace MessageDialog;using namespace Windows::UI::Popups;using namespace Windows::UI::Xaml::Navigation;using namespace Platform;ScenarioInput3::ScenarioInput3(){    InitializeComponent();}ScenarioInput3::~ScenarioInput3(){}void ScenarioInput3::OnNavigatedTo(NavigationEventArgs^ e){     rootPage = dynamic_cast<MainPage^>(e->Parameter);    _frameLoadedToken = rootPage->OutputFrameLoaded += ref new Windows::Foundation::EventHandler<Platform::Object^>(this, &ScenarioInput3::rootPage_OutputFrameLoaded);}void ScenarioInput3::OnNavigatedFrom(NavigationEventArgs^ e){    rootPage->OutputFrameLoaded -= _frameLoadedToken;}#pragma endregion#pragma region Output framevoid ScenarioInput3::rootPage_OutputFrameLoaded(Object^ sender, Object^ e){    Page^ outputFrame = dynamic_cast<Page^>(rootPage->OutputFrame->Content);}#pragma endregion#pragma region Click handlersvoid ScenarioInput3::Scenario3Launch_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)    Windows::UI::Popups::MessageDialog^ msg = ref new Windows::UI::Popups::MessageDialog("Would you like to install the new updates?", "Updates available");    UICommand^ noInstallCommand = ref new UICommand("Don't install", ref new UICommandInvokedHandler(this, &ScenarioInput3::CommandInvokedHandler, CallbackContext::Same));    UICommand^ installCommand = ref new UICommand("Install updates", ref new UICommandInvokedHandler(this, &ScenarioInput3::CommandInvokedHandler, CallbackContext::Same));    msg->Commands->Append(noInstallCommand);    msg->Commands->Append(installCommand);    msg->DefaultCommandIndex = 1;    msg->ShowAsync();}void ScenarioInput3::CommandInvokedHandler(Windows::UI::Popups::IUICommand^ command){    String ^buttonLabel = command->Label;    rootPage->NotifyUser("The '" + buttonLabel + "' button has been selected.", NotifyType::StatusMessage);}#pragma endregion

 

Dialog Box 4

using namespace MessageDialog;using namespace Windows::UI::Popups;using namespace Windows::UI::Xaml::Navigation;using namespace Platform;ScenarioInput4::ScenarioInput4(){    InitializeComponent();}ScenarioInput4::~ScenarioInput4(){}#pragma region Template-Related Code - Do not removevoid ScenarioInput4::OnNavigatedTo(NavigationEventArgs^ e){       rootPage = dynamic_cast<MainPage^>(e->Parameter);      _frameLoadedToken = rootPage->OutputFrameLoaded += ref new Windows::Foundation::EventHandler<Platform::Object^>(this, &ScenarioInput4::rootPage_OutputFrameLoaded);}void ScenarioInput4::OnNavigatedFrom(NavigationEventArgs^ e){    rootPage->OutputFrameLoaded -= _frameLoadedToken;}#pragma endregion#pragma region Output framevoid ScenarioInput4::rootPage_OutputFrameLoaded(Object^ sender, Object^ e){    Page^ outputFrame = dynamic_cast<Page^>(rootPage->OutputFrame->Content);}#pragma endregion#pragma region Click handlersvoid ScenarioInput4::Scenario4Launch_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e){    Windows::UI::Popups::MessageDialog^ msg = ref new Windows::UI::Popups::MessageDialog("You have been disconnected from the internet.");    UICommand^ closeCommand = ref new UICommand("Retry", ref new UICommandInvokedHandler(this, &ScenarioInput4::CommandInvokedHandler, CallbackContext::Same));    UICommand^ retryCommand = ref new UICommand("Close", ref new UICommandInvokedHandler(this, &ScenarioInput4::CommandInvokedHandler, CallbackContext::Same));       msg->Commands->Append(closeCommand);    msg->Commands->Append(retryCommand);    msg->CancelCommandIndex = 1;    msg->ShowAsync();}void ScenarioInput4::CommandInvokedHandler(Windows::UI::Popups::IUICommand^ command){    String ^buttonLabel = command->Label;    rootPage->NotifyUser("The '" + buttonLabel + "' button has been selected.", NotifyType::StatusMessage);}#pragma endregion

Press F5 to see the Running Effect

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Download vs11 now

Http://www.microsoft.com/click/services/Redirect2.ashx? Cr_cc = 200098144

 

 

 

 

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.