我們所了解的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 handlers
void 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 frame
void 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 handlers
void 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 frame
void ScenarioInput3::rootPage_OutputFrameLoaded(Object^ sender, Object^ e)
{
Page^ outputFrame = dynamic_cast<Page^>(rootPage->OutputFrame->Content);
}
#pragma endregion
#pragma region Click handlers
void 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 remove
void 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 frame
void ScenarioInput4::rootPage_OutputFrameLoaded(Object^ sender, Object^ e)
{
Page^ outputFrame = dynamic_cast<Page^>(rootPage->OutputFrame->Content);
}
#pragma endregion
#pragma region Click handlers
void 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](http://www.microsoft.com/click/services/Redirect2.ashx?CR_CC=200098144)
?
?
?
?
- 前言
- Visual Studio 11開發指南(1) Visual Studio 11簡介與新特性
- Visual Studio 11開發指南(2) Visual Studio 11放棄宏處理
- Visual Studio 11開發指南(3)Visual Studio 11開發SharePoint 2011程序
- Visual Studio 11開發指南(4)Visual Studio 11編程語言發展
- Visual Studio 11開發指南(5)Visual Studio 11 IDE增強
- Visual Studio 11開發指南(6)Visual Studio 11平臺改進
- Visual Studio 11開發指南(7)NET 4.5的改善
- Visual Studio 11開發指南(8)Visual C++ 11新特色
- Visual Studio 11開發指南(9)Visual C++ 新功能體驗
- Visual Studio 11開發指南(10)Visual C++11 IDE 新功能體驗
- Visual Studio 11開發指南(11)Visual Studio 11調試游戲
- Visual Studio 11開發指南(12)Visual Studio 11可視化多核多線程編程的行為
- Visual Studio 11開發指南(13)C++11語言新特性
- Visual Studio 11開發指南(14)C++11---C++/ CX設計
- Visual Studio 11開發指南(15)C++11單元測試
- Visual Studio 11開發指南(16)C++11更新-多線程和異步操作管理
- Visual Studio 11開發指南(17)C++11更新- Lambda表達式
- Visual Studio 11開發指南(18)C++11更新-自動矢量器使用
- Visual Studio 11開發指南(19)C++11更新-并行模式庫和代理庫
- 在 C++ 中使用 PPL 進行異步編程
- 基于VisualStudio11開發Windows8的Metro sample講解(1)MessageBox
- Visual C++ 11 中新的并發功能
- 基于Windows8與Visual Studio2012開發內核隱藏注冊表
- 基于VC++2012在Windows8上實現文件隱藏
- 實現諾基亞 lumia Windows phone 的手機通話記錄截取
- 最短代碼實現windows8下的下載器-下載安裝執行一體化
- 用Visual studio2012在Windows8上開發內核驅動監視線程創建
- 用Visual studio2012在Windows8上開發內核驅動監視進程創建
- 基于Windows8與Visual Studio2012實現殺毒通用模塊
- 用Visual studio2012在Windows8上開發內核中隱藏進程
- 用Visual studio11在Windows8上開發內核枚舉注冊表
- 用Visual studio11在Windows8上開發內核驅動隱藏注冊表
- 用Visual studio11在Windows8上開發驅動實現注冊表監控和過濾
- 用Visual studio11在Windows8上開發驅動實現內存填0殺進程
- 【CSDN2012年度博客之星】喜歡本博客的讀者,投票贈送《visual C++2010開發權威指南》電子稿--感謝支持 ~(截至到2012年12月30日)
- 今天在清華圖書館看到我的杰作,感慨萬千,而我要歸零一切 !
- use Visual studio2012 developing kernel driver monitor thread creation on Windows8
- To kernel driver monitoring process developed in Windows8 create using Visual studio2012
- Under Windows8 kernel mode development NDIS application-NDIS Filter explain
- use Visual studio2012 development kernel to hidden process on Windows8