这篇文章记录了 WPF 中的 MessageBox 的使用,给出了详细的代码例子。

消息框

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
namespace Dialogs
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

private void messageBoxHandle(object sender, RoutedEventArgs e)
{
const string title = "黑夜望月";
switch (textBox.Text)
{
case "Ok":
var result4 = MessageBox.Show("您购买的图灵电子书仅供您个人使用,未经授权,不得以任何方式复制\r\n和传播本书内容。", title, MessageBoxButton.OK);
boxResult.Content = result4;
break;
case "YesNo":
var result2 = MessageBox.Show("您购买的图灵电子书仅供您个人使用,未经授权,不得以任何方式复制\r\n和传播本书内容。", title, MessageBoxButton.YesNo);

switch (result2)
{
case MessageBoxResult.Yes:
boxResult.Content = "您选择了 Yes";
break;
case MessageBoxResult.No:
boxResult.Content = "您选择了 No";
break;
default:
break;
}
break;
case "OKCancel":
var result1 = MessageBox.Show("您购买的图灵电子书仅供您个人使用,未经授权,不得以任何方式复制\r\n和传播本书内容。", title, MessageBoxButton.OKCancel);
switch (result1)
{
case MessageBoxResult.OK:
boxResult.Content = "您选择了 OK";
break;
case MessageBoxResult.Cancel:
boxResult.Content = "您选择了 Cancel";
break;
default:
break;
}
break;
case "YesNoCancel":
var result3 = MessageBox.Show("您购买的图灵电子书仅供您个人使用,未经授权,不得以任何方式复制\r\n和传播本书内容。", title, MessageBoxButton.YesNoCancel);
switch (result3)
{
case MessageBoxResult.Yes:
boxResult.Content = "您选择了 OK";
break;
case MessageBoxResult.Cancel:
boxResult.Content = "您选择了 Cancel";
break;
case MessageBoxResult.No:
boxResult.Content = "您选择了 No";
break;
default:
break;
}
break;
}
}
}
}

Yes 消息框

Yes 消息框

YesNo 消息框

YesNo 消息框

YesNoCancel 消息框

YesNoCancel 消息框

OkCancel 消息框

OkCancel 消息框

消息框图标

我们可以给消息框提供一个图标选项,图标类如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
public enum MessageBoxImage
{
//消息框不包含符号。
None = 0,
// 显示错误图标
Error = 16,
//
// 摘要:
// The message box contains a symbol consisting of a white X in a circle with a
// red background.
Hand = 16,
//
// 摘要:
// The message box contains a symbol consisting of white X in a circle with a red
// background.
Stop = 16,
//
// 摘要:
// The message box contains a symbol consisting of a question mark in a circle.
// The question mark message icon is no longer recommended because it does not clearly
// represent a specific type of message and because the phrasing of a message as
// a question could apply to any message type. In addition, users can confuse the
// question mark symbol with a help information symbol. Therefore, do not use this
// question mark symbol in your message boxes. The system continues to support its
// inclusion only for backward compatibility.
Question = 32, // 问号
//
// 摘要:
// The message box contains a symbol consisting of an exclamation point in a triangle
// with a yellow background.
Exclamation = 48, // 感叹号
//
// 摘要:
// The message box contains a symbol consisting of an exclamation point in a triangle
// with a yellow background.
Warning = 48,
//
// 摘要:
// The message box contains a symbol consisting of a lowercase letter i in a circle.
Asterisk = 64, // 星号
//
// 摘要:
// The message box contains a symbol consisting of a lowercase letter i in a circle.
Information = 64
}

例子:

1
var result4 = MessageBox.Show("您购买的图灵电子书仅供您个人使用,未经授权,不得以任何方式复制\r\n和传播本书内容。", title, MessageBoxButton.OK, MessageBoxImage.Question);