我們所瞭解的MessageBox。
函數原型:essageBox.Show(Text,Title,MessageBoxButtons,MessageBoxIcon ,MessageBoxDefaultButtons)
參數說明:
(1)Text:必選項,訊息框的本文。
(2)Title:可選項,訊息框的標題。
(3)MessageBoxButtons:可選項,訊息框的按鈕設定,預設只顯示【確定】按鈕。
OK――確定 OKCancel――確定和取消 AbortRetryIgnore――終止、重試和忽略 YesNoCancel――是、否和取消 YesNo――是和否 RetryCancel――重試和取消 (4)MessageBoxIcon:對話方塊中顯示的表徵圖樣式,預設不顯示任何表徵圖。 Question――問號 Information、Asterisk――i號 Error、Stop、Hand――錯誤號碼 Warning、Exclamation――!號 None――不顯示任何表徵圖
(5)MessageBoxDefaultButtons:可選項,對話方塊中預設選中的按鈕設定。 DefaultButton1――第1個button是預設按鈕 DefaultButton2――第2個button是預設按鈕 DefaultButton3――第3個button是預設按鈕
備忘:函數原型中藍色字型部分的參數,可以通過點來擷取其後面跟隨的參數值。
MessageBox(NULL,"text","title",BUTTON);
參數title:string類型,指定訊息對話方塊的標題。text:指定訊息對話方塊中顯示的訊息,該參數可以是數值資料類型、字串或boolean值。icon:Icon枚舉類型,可選項,指定要在該對話方塊左側顯示的表徵圖。button:Button枚舉類型,可選項,指定顯示在該對話方塊底部的按鈕。default:數值型,可選項,指定作為預設按鈕的按鈕編號,按鈕編號自左向右依次計數,預設值為1,如果該參數指定的編號超過了顯示的按鈕個數,那麼MessageBox()函數將使用預設值傳回值Integer。函數執行成功時返回使用者選擇的按鈕編號(例如1、2、3等),發生錯誤時返回-1。如果任何參數的值為NULL,MessageBox()函數返回NULL。
比如說,想彈出的視窗,命令為: MessageBox(NULL,"MessageBoxText(內容)","Title(標題)",MB_OK); 如果想置頂 int iRet; iRet=MessageBox(hwnd,"PLC警示!","對話方塊",MB_YESNO|MB_ICONQUESTION|MB_SYSTEMMODAL); 或 iRet=MessageBox(NULL,"PLC警示!","對話方塊", MB_YESNO|MB_ICONQUESTION|MB_DEFBUTTON1|MB_SYSTEMMODAL); 增加一個焦點在第一個按鈕上更有效。
參數如下
hWnd:標識將被建立的訊息框的擁有視窗。如果此參數為NULL,則訊息框沒有擁有視窗。
lpText:指向一個以NULL結尾的、含有將被顯示的訊息的字串的指標。
lpCaption:指向一個以NULL結尾的、用於對話方塊標題的字串的指標。
uType:指定一個決定對話方塊的內容和行為的位標誌集。此參數可以為下列標誌組中標誌的組合。
指定下列標誌中的一個來顯示訊息框中的按鈕,標誌的含義如下。
MB_ABORTRETRYIGNORE:訊息框含有三個按鈕:Abort,Retry和Ignore。
MB_OK:訊息框含有一個按鈕:OK。這是預設值。
MB_OKCANCEL:訊息框含有兩個按鈕:OK和Cancel。
MB_RETRYCANCEL:訊息框含有兩個按鈕:Retry和Cancel。
MB_YESNO:訊息框含有兩個按鈕:Yes和No。
MB_YESNOCANCEL:訊息框含有三個按鈕:Yes,No和Cancel。
指定下列標誌中的一個來顯示訊息框中的表徵圖:標誌的含義如下。
MB_ICONEXCLAMATION:
MB_ICONWARNING:一個驚歎號出現在訊息框。
MB_ICONINFORMATION:
MB_ICONASTERISK:一個圓圈中小寫字母i組成的表徵圖出現在訊息框。
MB_ICONQUESTION:一個問題標記表徵圖出現在訊息框。
MB_ICONSTOP:
MB_ICONERROR:
MB_ICONHAND:一個停止訊息表徵圖出現在訊息框。
指定下列標誌中的一個來顯不預設的按鈕:標誌的含義如下。
MB_DEFBUTTON1:第一個按鈕為預設按鈕。如果MB_DEFBUTTON2,MB_DEFBUTTON3,MB_DEFBUTTON4沒有被指定,則MB_DEFBUTTON1為預設值。
MB_DEFBUTTON2;第二個按鈕為預設按鈕。
MB_DEFBUTTON3:第三個按鈕為預設按鈕。
MB_DEFBUTTON4:第四個按鈕為預設按鈕。
指定下列標誌中的一個來顯示對話方塊的形態:標誌的含義如蔔。
MB_APPLMODAL:在hwnd參數標識的視窗中繼續工作以前,使用者一定響應訊息框。但是,使用者可以移動到其他線程的視窗且在這些視窗中工作。根據應用程式中視窗的層次機構,使用者則以移動到線程內的其他視窗。所有母訊息框的子視窗自動地失效,但是快顯視窗不是這樣。如果既沒有指定MB_SYSTEMMODAL也沒有指定MB_TASKMOOAL,則MB_APPLMODAL為預設的。
MB_SYSTEMMODAL:除了訊息框有WB_EX_TOPMOST類型,MB_APPLMODAL和WS_EX_TOPMOST一樣。用系統模態訊息框來改變各種各樣的使用者,主要的損壞錯誤需要立即注意(例如,記憶體溢出)。如果不是那些與hwnd聯絡的視窗,此標誌對使用者對視窗的相互聯絡沒有影響。
MB_TASKMODAL:如果參數hwnd為NULL,除了所有屬於當前線程高層次的視窗足失效的,MB_TASKMODALL和MB_ApPLMODAL一樣。當調用應用程式或庫沒有一個可以得到的視窗控制代碼時,使用此標誌。但仍需要阻止到調用應用程式甲其他視窗的輸入而不是擱置其他線程。
另外,可以指定下列標誌。
MB_DEFAULT_DESKTOP_ONLy:接收輸入的當前案頭一定是一個預設案頭。否則,函數調用失敗。預設案頭是一個在使用者已經紀錄且以後應用程式在此上面啟動並執行案頭。
MB_HELP:把一個Help按鈕增加到訊息框。選擇Help按鈕或按F1產生一個Help事件。
MB_RIGHT:文本為右調整。
MB_RTLREADING:用在Hebrew和Arabic系統中從右至左的順序顯示訊息和大寫文本。
MB_SETFOREGROUND:訊息框變為前景視窗。在內部系統為訊息個調用SetForegroundWindow函數。
MB_TOPMOSI:訊息框用WS_EX_TOPMOST視窗類別型來建立MB_SERVICE_NOTIFICATION。
Windows NT:調用程式是一個通知事件的使用者的服務程式。函數在當前活動案頭上顯示一個訊息框,即使沒有使用者登記到電腦。
如果設定了此參數,則hwnd參數一定為NULL。所以訊息框可以出現在一個案頭上而不是案頭響應參數hwnd。
對於Windows NT 4.0,MB_SERVICE_NOTIFICATION的值已經改變。對於舊的和新的值,請參見WINUSER。
Windows NT 4.O通過把舊值對應到MessageBox和MessageBoxEx執行中的新值,為先存在的服務程式提供逆相容。此映射只為有了版本數目的可執行程式而做。
為了建立一個用MB_SERVICE_NOTIFICATION的伺服器,且可以在Windows NT 3.X和Window NT 4.0上執行,可有兩種選擇。在連線時間,指定一個版本數目小於4.0的版本,或在連線時間,指定一個4.0版本。在已耗用時間,用函數GetVersionEx來檢測系統版本,然後在Windows NT 3.X上用MB_SERVICE_NOTIFICATION_NT 3.x來運行和在Windows NT 4.0上用MB_SERVICE_NOTIFICAION來運行。MB_SERVCE_NOTIFICATION_NT3.x(WindowNT)此值響應於為WindowNT3.51的MB_SERVICE_NOTIFICAION
定義的值。
傳回值如下
如果沒有足夠的記憶體來建立訊息框,則傳回值為零。如果函數調用成功,則傳回值為下列對話方塊返回的功能表項目值中的一個:
IDABORT:Abort 按鈕被選中。IDCANCEL:Cancel按鈕被選中。IDIGNORE:Ignore按鈕被選中。
IDNO:NO按鈕被選中。IDOK:OK按鈕被選中。IDRETRY:RETRY按鈕被選中。
IDYES:YES按鈕被選中。
如果一個訊息框有一個Cancel按鈕,且如果Esc鍵被按下或Cancel鍵被選擇,則函數返回IDCANCEL值。如果訊息框沒有Cancel按鈕,則按Esc鍵沒有作用。
MessageBox的傳回值預設定義 MessageBox的傳回值其實都是整型的數,以下是預設的定義
#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
下面我們來親自實踐Windows8中的MessageBox
看一下程式初始化代碼
#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);}
對話方塊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
對話方塊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
對話方塊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
對話方塊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
按下F5看運行效果
趕緊下載VS11體驗吧
http://www.microsoft.com/click/services/Redirect2.ashx?CR_CC=200098144