简介:
显示可包含文本、按钮和符号(通知并指示用户)的消息框。MFC的MessageBox的MB_TOPMOST属性可以直接设置。Winform的MessageBox没有直接设置的参数。
介绍
- 显示具有指定文本、标题、按钮、图标、默认按钮和选项的消息框。
MessageBox.Show 方法 (String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions)
public static DialogResult Show(
string text,
string caption,
MessageBoxButtons buttons,
MessageBoxIcon icon,
MessageBoxDefaultButton defaultButton,
MessageBoxOptions options
)
参数 | 含义 |
---|
text | 类型: System. String 要在消息框中显示的文本。 |
caption | 类型: System. String 要在消息框的标题栏中显示的文本。 |
buttons | 类型: System.Windows.Forms. MessageBoxButtons MessageBoxButtons 值之一,可指定在消息框中显示哪些按钮。
成员名称 | 说明 |
---|
OK | 消息框包含“确定”按钮。 | OKCancel | 消息框包含“确定”和“取消”按钮。 | AbortRetryIgnore | 消息框包含“中止”、“重试”和“忽略”按钮。 | YesNoCancel | 消息框包含“是”、“否”和“取消”按钮。 | YesNo | 消息框包含“是”和“否”按钮。 | RetryCancel | 消息框包含“重试”和“取消”按钮。 |
|
icon | 类型: System.Windows.Forms. MessageBoxIcon MessageBoxIcon 值之一,它指定在消息框中显示哪个图标。
成员名称 | 说明 |
---|
None | 消息框未包含符号。 | Hand | 该消息框包含一个符号,该符号是由一个红色背景的圆圈及其中的白色 X 组成的。 | Question | 该消息框包含一个符号,该符号是由一个圆圈和其中的一个问号组成的。 不再建议使用问号消息图标,原因是该图标无法清楚地表示特定类型的消息,并且问号形式的消息表述可应用于任何消息类型。 此外,用户还可能将问号消息符号与帮助信息混淆。 因此,请不要在消息框中使用此问号消息符号。 系统继续支持此符号只是为了向后兼容。 | Exclamation | 该消息框包含一个符号,该符号是由一个黄色背景的三角形及其中的一个感叹号组成的。 | Asterisk | 该消息框包含一个符号,该符号是由一个圆圈及其中的小写字母 i 组成的。 | Stop | 该消息框包含一个符号,该符号是由一个红色背景的圆圈及其中的白色 X 组成的。 | Error | 该消息框包含一个符号,该符号是由一个红色背景的圆圈及其中的白色 X 组成的。 | Warning | 该消息框包含一个符号,该符号是由一个黄色背景的三角形及其中的一个感叹号组成的。 | Information | 该消息框包含一个符号,该符号是由一个圆圈及其中的小写字母 i 组成的。 |
|
defaultButton | 类型: System.Windows.Forms. MessageBoxDefaultButton MessageBoxDefaultButton 值之一,可指定消息框中的默认按钮。
成员名称 | 说明 |
---|
Button1 | 消息框上的第一个按钮是默认按钮。 | Button2 | 消息框上的第二个按钮是默认按钮。 | Button3 | 消息框上的第三个按钮是默认按钮。 |
|
options | 类型: System.Windows.Forms. MessageBoxOptions MessageBoxOptions 值之一,可指定将对消息框使用哪些显示和关联选项。 若要使用默认值,请传入 0。
成员名称 | 说明 |
---|
ServiceNotification | 消息框显示在活动桌面上。 调用方是一种服务,用于将事件通知用户。 即使没有用户登录到计算机,该功能也会在当前活动桌面上显示一个消息框。 | DefaultDesktopOnly | 消息框显示在活动桌面上。 此常数与 ServiceNotification 相同,只是系统仅在交互窗口站的默认桌面上显示消息框。 DefaultDesktopOnly 将使引发 MessageBox 的应用程序失去焦点。 显示的 MessageBox 将不使用视觉样式。 有关更多信息,请参见 使用视觉样式呈现控件。 | RightAlign | 消息框文本右对齐。 | RtlReading | 指定消息框文本按从右到左的阅读顺序显示。 |
|
返回值 | 返回值 类型: System.Windows.Forms. DialogResult DialogResult 值之一。
成员名称 | 说明 |
---|
None | 从对话框返回了 Nothing。 这表明有模式对话框继续运行。 | OK | 对话框的返回值是 OK(通常从标签为“确定”的按钮发送)。 | Cancel | 对话框的返回值是 Cancel(通常从标签为“取消”的按钮发送)。 | Abort | 对话框的返回值是 Abort(通常从标签为“中止”的按钮发送)。 | Retry | 对话框的返回值是 Retry(通常从标签为“重试”的按钮发送)。 | Ignore | 对话框的返回值是 Ignore(通常从标签为“忽略”的按钮发送)。 | Yes | 对话框的返回值是 Yes(通常从标签为“是”的按钮发送)。 | No | 对话框的返回值是 No(通常从标签为“否”的按钮发送)。 |
|
使用
- 使用带有MessageBoxOptions形参的MessageBox.Show方法。
- 前五个变量根据需要自行更改。置顶时,第六个变量为MessageBoxOptions.ServiceNotification或MessageBoxOptions.DefaultDesktopOnly。
MessageBox.Show("Text", "Caption", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification);
MessageBox.Show("Text", "Caption", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);