适用于 .NET 的高级消息框






3.63/5 (20投票s)
作为 .NET MessageBox 类的替代品,它提供了附加功能和改进,例如显示任意数量按钮的可能性、“默认应用我的答案”复选框、智能布局等。
引言
有些应用程序需要比 .NET 提供的 MessageBox
类更高级的消息框。本文介绍了一个名为 MessageForm
的 .NET 库,它可以替代 .NET MessageBox
,并提供更多功能和改进。
MessageForm 与 MessageBox 的通用功能比较
特点 |
MessageForm |
MessageBox |
可包含任意数量的按钮 |
+ |
- |
按钮的文本和颜色通过参数指定 |
+ |
- |
可以显示“默认应用我的答案”复选框,以便用户将来可以默认应用他们的答案 |
+ |
- |
允许选择和复制消息;当您想从消息框获取路径或错误字符串时,这非常方便 | + | - |
窗口的内容可以独立于其大小查看 |
+ |
- |
允许在窗口标题栏中设置图标 |
+ |
- |
线程安全 |
+ |
+ |
将消息框置于桌面窗口的最顶层;这对于警报很重要 | + | +/- (如果显示消息框的应用程序是无窗口的,消息框可能会从一开始就“丢失”在其他桌面窗口下方) |
在设置消息旁的图标时,除了 |
+ |
- |
可以进一步调整/增强 |
+ |
- |
描述
MessageForm
库包含 2 个类
MessageForm
,实际上是高级消息框的实现;Message
,MessageForm
的包装器
MessageForm 类
MessageForm
类基于 System.Windows.Forms.Form 实现消息框窗口。通常您不想使用它,而是使用 Message
类。另一方面,MessageForm
提供了比 Message
更灵活的设置。
报文类别
Message
的目标是让您在代码中使用高级消息框尽可能简单。它提供了最常用 MessageForm
实例的集合。这意味着您通常只需要使用 Message
类。如果其预定义方法不能满足您的需求,您可以直接使用 MessageForm
,或者通过自己的方法增强 Message
。
Message
是一个线程安全的类,因此您可以从多个线程中使用它而不会发生冲突。Message.ShowMessagesInTurn
参数定义了消息框如何在多线程环境中显示。如果为 true
,则消息框会逐个显示。如果为 false
,则 Message
会同时显示来自不同线程的消息框(这就像 .NET MessageBox
做的那样)。
Message
类具有以下默认设置
- 消息窗口标题栏中显示的图标是托管应用程序的图标;
- 标题栏中的文本是托管应用程序的名称;
- 按钮以不同的颜色着色。这通常很方便,因为它有助于用户更快地选择所需的答案。只有 OK 按钮的消息框没有颜色按钮;
- 消息窗口是置顶的、顶级的,因为这通常是警报消息所需要的;
- 来自不同线程的消息框会逐个显示;
这些设置分别是 Message
的属性,因此可以从使用 MessageForm
的代码中更改,甚至直接在 MessageForm
项目中更改。例如,可以通过以下代码关闭按钮的着色
//toggle off coloring buttons
Cliver.Message.ButtonColors = null;
MessageForm 的用法
以下是使用 Message
和 MessageForm
类的几个示例。(您可以在 MessageForm
解决方案中的 Test
项目中找到更多示例。)相应的代码紧随图像之后。
使用 Message 类
带 OK 按钮的消息框(XP 和 Vista 样式)
Cliver.Message.Inform("Message.Ok test");
带 2 个自定义按钮的消息框(XP 和 Vista 样式)
//returns clicked button index
int a = Cliver.Message.Show(SystemIcons.Error, "Connection lost", 0,
"Try to reconnect", "Exit");
带 3 个自定义按钮和复选框的消息框
//Set message box caption once and forever
Cliver.Message.Caption = "Backup Application";
//returned silent box state
bool r;
//returned clicked button index
int a;
//show message box with 3 buttons
a = Cliver.Message.Show(SystemIcons.Information, out r,
@"Local file c:\test.txt differs from the backup copy", 0,
"Backup the newest file",
"Download the backup copy",
"Do nothing for now"
);
如果消息框需要显示不可预测的长文本,例如错误堆栈怎么办?有时消息可能太长,以至于 .NET MessageBox
会超出屏幕,导致消息的末尾不可见。以下是 MessageForm
如何处理此类“不安全”情况的示例

直接使用 MessageForm
带 6 个按钮、复选框和自定义消息图标的消息框。请注意,MessageForm
可以接受 Image
类型而不是 Icon
类型。
在低分辨率的经典样式屏幕上的外观

Cliver.MessageForm mf = new MessageForm(
"Using MessageForm directly",//caption
//message
"Copying files from: 'c:\\test' to: 'c:\\test2'\nFile 'test.txt' already exists.",
new Color[6] {
Color.LightCoral,
Color.LightYellow,
Color.Empty,
Color.LightGreen,
Color.LightBlue,
Color.Empty,
},//button colors
new string[6] {
"Overwrite",
"Overwrite if older",
"Skip",
"Rename",
"Append",
"Cancel"
},//array of answers
1,//default button
"Apply to all",//silent box text
new Bitmap("../../copying.jpg")//message icon, can be set from image
);
//set icon in the capture
mf.Icon = new Icon("../../computers308.ico");
//show message form
mf.GetAnswer();
//get state of silent checkbox
bool silent = mf.Silent;
修改 MessageForm
MessageForm 库是用纯 C# 编写的。它构建自己的窗口,而不是 MessageBox
类的包装器。它继承自 System.Windows.Forms.Form
,因此您可以通过更改其代码轻松灵活地按需修改它。
另外请注意,在 MessageForm
中自动排列控件时,以下值不会从其初始值更改
- 窗口的最小尺寸(只能增大);
- 消息图标的位置;
- 消息标签的 Y 值;
- 窗口左边缘与第一个按钮之间的间距;
牢记这些,您可以在 Visual Studio 窗口设计模式下调整 MessageForm
的外观。
此外,请注意 MessageFrom
类中的几个属性,它们也定义了其布局。
Using the Code
在附件代码中,您可以找到
- 库项目
MessageForm
,其中包含MessageForm
和Message
类。这些类可以编译为 DLL 或添加到您的代码中。 Test
项目,包含用法示例。
MessageForm
的最新版本可以在 SourceForge 上找到
祝您开心!