FlexibleMessageBox – .NET MessageBox 的灵活替代方案
使用 FlexibleMessageBox.Show 可以无缝替换您对 MessageBox.Show 的使用,并在单个类中获得更多功能,您可以轻松地将其添加到您的项目中。
介绍
在使用经典的 Windows Forms 设计用户界面时,您通常使用 .NET 的 MessageBox
来提供简短的信息反馈或让用户做出选择。在我最新的项目中,我也需要简单的对话框来显示一些服务器操作处理后的状态消息。这些消息通常很简短,例如“一切都已成功处理”。所以我想:“为什么还要创建自己的对话框呢?”,并决定使用标准的 MessageBox
来完成这项工作。
问题
一切进展顺利,我的程序也因此不断增长。我添加了一些更大的批量操作,这些操作包含许多服务器操作。每个操作都有其(通常很简短的)消息,但也可能产生一个简短的错误报告。所有这些消息加在一起可能就构成相当大的“状态报告”。
当我第一次看到结果时,它们看起来像这样
是的,MessageBox
确实会调整大小以适应我的内容,但我看不到底部的任何按钮,因为它在底部被裁剪了。但这还不够:并非所有内容都可以访问,因为缺少垂直滚动条。
这对我来说是不可接受的,所以我开始考虑创建一个自定义对话框来显示我的各种消息。我越想越不想设计一个专门为该应用程序量身定制的对话框,尤其是我不想更改我代码中所有对 MessageBox.Show(…)
的调用。
解决方案:一个灵活的消息框
我在网上搜索了标准 MessageBox
的替代品。但尽管我找到了非常有用的东西,但没有一个解决方案让我真正满意。我首先想要一个可调整大小的 MessageBox
替代品,而不是一大堆对我来说过度的代码。于是我最终编写了一个适合我自身需求的替换方案。
特点
这是 FlexibleMessageBox
提供功能的摘要
- 由于所有重要的
static
Show
函数都得到支持,因此可以像使用MessageBox
一样简单地使用它。 - 它很小,只有一个源文件,可以轻松添加到任何解决方案中。
- 它可以调整大小,内容会自动换行。
- 它会尝试自动调整宽度以显示最长的文本行。
- 它永远不会超过当前的桌面工作区域。
- 它会在需要时显示垂直滚动条。
- 它支持文本中的超链接。
外观
使用 FlexibleMessageBox
,上面提到的结果现在看起来是这样的:
但是 FlexibleMessageBox
的外观也可以是这样的:
或者使用另一种字体,如下所示:
使用 FlexibleMessageBox
集成到您的项目中
要在代码中使用 FlexibleMessageBox
,建议执行以下简单步骤:
- 下载 FlexibleMessageBox.cs 源文件(或演示项目)。
- 将 FlexibleMessageBox.cs 源文件添加到您的解决方案中。
- 在您的源文件中添加
using JR.Utils.GUI.Forms
。
就是这样。现在您可以像(几乎)所有其他 MessageBox
一样使用 FlexibleMessageBox
了。
使用示例
FlexibleMessageBox.Show("Just a text");
FlexibleMessageBox.Show("A text", "A caption");
FlexibleMessageBox.Show("Some text with a link: www.google.com",
"Some caption",
MessageBoxButtons.AbortRetryIgnore,
MessageBoxIcon.Information,
MessageBoxDefaultButton.Button2);
var result = FlexibleMessageBox.Show("Know the answer to life the universe and everything?",
"One short question",
MessageBoxButtons.YesNo); //By the way: The answer is 42 :-)
使用演示应用程序
这是一个简单的 Windows Forms 应用程序,用于演示 FlexibleMessageBox
的功能。
最左边的两个按钮显示了两个不同参数的 FlexibleMessageBox
变体。中间的两个按钮显示了许多行文本的示例,第一次使用 .NET MessageBox
,然后对比使用 FlexibleMessageBox
。您可以尝试调整 FlexibleMessageBox
的大小,并观察自动换行和垂直滚动条。在右侧,您可以更改一些(下面描述的)static
参数,以查看所有 FlexibleMessageBoxe
s 的外观如何变化。点击复选框使用其他字体。使用滑块修改最大宽度和高度。
代码内部
决定:以单个文件形式提供代码
我知道我应该将代码拆分成多个源文件以使其更易读和模块化。但我也知道,就我而言,当我想要使用一个外部组件却不得不包含许多文件时,我常常感到恼火。所以我决定将这个组件放在一个单独的文件中,可以轻松地添加到其他项目中。
干净的界面
您可能已经注意到 FlexibleMessageBox
的 public
接口非常干净。
这是因为所有相关的辅助函数都位于内部类 FlexibleMessageBoxForm
中。有关此类的简要说明,请参见下文。
静态 Show 函数
以下是可用的 Show
函数,它们是原始 .NET MessageBox
Show
函数的子集。
#region Public show functions
/// <summary>
/// Shows the specified message box.
/// </summary>
/// <param name="text">The text.</param>
/// <returns>The dialog result.</returns>
public static DialogResult Show(string text)
/// <summary>
/// Shows the specified message box.
/// </summary>
/// <param name="owner">The owner.</param>
/// <param name="text">The text.</param>
/// <returns>The dialog result.</returns>
public static DialogResult Show(IWin32Window owner, string text)
/// <summary>
/// Shows the specified message box.
/// </summary>
/// <param name="text">The text.</param>
/// <param name="caption">The caption.</param>
/// <returns>The dialog result.</returns>
public static DialogResult Show(string text, string caption)
/// <summary>
/// Shows the specified message box.
/// </summary>
/// <param name="owner">The owner.</param>
/// <param name="text">The text.</param>
/// <param name="caption">The caption.</param>
/// <returns>The dialog result.</returns>
public static DialogResult Show(IWin32Window owner, string text, string caption)
/// <summary>
/// Shows the specified message box.
/// </summary>
/// <param name="text">The text.</param>
/// <param name="caption">The caption.</param>
/// <param name="buttons">The buttons.</param>
/// <returns>The dialog result.</returns>
public static DialogResult Show(string text, string caption, MessageBoxButtons buttons)
/// <summary>
/// Shows the specified message box.
/// </summary>
/// <param name="owner">The owner.</param>
/// <param name="text">The text.</param>
/// <param name="caption">The caption.</param>
/// <param name="buttons">The buttons.</param>
/// <returns>The dialog result.</returns>
public static DialogResult Show(IWin32Window owner,
string text, string caption, MessageBoxButtons buttons)
/// <summary>
/// Shows the specified message box.
/// </summary>
/// <param name="text">The text.</param>
/// <param name="caption">The caption.</param>
/// <param name="buttons">The buttons.</param>
/// <param name="icon">The icon.</param>
/// <returns></returns>
public static DialogResult Show(string text, string caption,
MessageBoxButtons buttons, MessageBoxIcon icon)
/// <summary>
/// Shows the specified message box.
/// </summary>
/// <param name="owner">The owner.</param>
/// <param name="text">The text.</param>
/// <param name="caption">The caption.</param>
/// <param name="buttons">The buttons.</param>
/// <param name="icon">The icon.</param>
/// <returns>The dialog result.</returns>
public static DialogResult Show(IWin32Window owner, string text,
string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
/// <summary>
/// Shows the specified message box.
/// </summary>
/// <param name="text">The text.</param>
/// <param name="caption">The caption.</param>
/// <param name="buttons">The buttons.</param>
/// <param name="icon">The icon.</param>
/// <param name="defaultButton">The default button.</param>
/// <returns>The dialog result.</returns>
public static DialogResult Show(string text, string caption,
MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
/// <summary>
/// Shows the specified message box.
/// </summary>
/// <param name="owner">The owner.</param>
/// <param name="text">The text.</param>
/// <param name="caption">The caption.</param>
/// <param name="buttons">The buttons.</param>
/// <param name="icon">The icon.</param>
/// <param name="defaultButton">The default button.</param>
/// <returns>The dialog result.</returns>
public static DialogResult Show(IWin32Window owner, string text, string caption,
MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
#endregion
静态参数
有几个 static
变量包含用于所有 FlexibleMessageBox
实例的设置。
MAX_WIDTH_FACTOR
定义所有 FlexibleMessageBox
实例相对于工作区域的宽度百分比。
允许值从 0.2
到 1.0
不等。
0.2
表示:FlexibleMessageBox
的最大宽度是工作区域的一半。1.0
表示:FlexibleMessageBox
的宽度可以等于工作区域的宽度。
默认值为 0.7
,即工作区域宽度的 70%。
MAX_HEIGHT_FACTOR
定义所有 FlexibleMessageBox
实例相对于工作区域的高度百分比。
允许值从 0.2
到 1.0
不等。
0.2
表示:FlexibleMessageBox
的最大高度是工作区域的一半。1.0
表示:FlexibleMessageBox
的高度可以等于工作区域的高度。
默认值为 0.9
,即工作区域高度的 90%。
字体
定义用于所有 FlexibleMessageBox
实例的字体。默认值为 SystemFonts.MessageBoxFont
。
内部窗体类 FlexibleMessageBoxForm
隐藏的内部类 FlexibleMessageBoxForm
派生自 Form
,并负责安排所需的 GUI 元素。文本以 RichTextBox
的形式提供。此外,还有三个按钮和一个用于图标的 PictureBox
——没有魔法。
Show 函数
<code>FlexibleMessageBoxForm
的 static
Show
函数由 FlexibleMessageBox
的所有 Show
函数调用。它处理所有不同的给定参数并执行以下操作:
- 创建
FlexibleMessageBox
窗体的新实例。 - 绑定标题和消息文本。
- 设置按钮的可见性和文本。
- 设置默认按钮。
- 设置对话框的图标。如果没有使用图标:则会调整 Rich Text Box 的位置和宽度。
- 设置所有控件的字体,使用静态
FONT
。如果未设置字体,则使用标准的SystemFonts.MessageBoxFont
。 - 通过尝试自动调整宽度以显示最长的文本行来计算对话框的初始大小。
- 使用
MAX_WIDTH_FACTOR
和MAX_HEIGHT_FACTOR
的静态变量设置对话框的最大尺寸。 - 设置对话框的初始位置(如果已提供)。否则,将对话框居中显示在当前屏幕上。
- 最后但同样重要的是:显示对话框。
辅助函数和其他内容
有几个小的辅助函数。请自行探索。我尝试对其进行良好记录,并使用代码区域来提高可读性。
历史
版本 1.3 - 2014 年 12 月 19 日 (Bug修复)
- 添加了重构函数
GetButtonText()
。 - 使用了
CurrentUICulture
而不是InstalledUICulture
。 - 添加了更多按钮本地化。现在支持的语言是:英语、德语、西班牙语、意大利语。
- 添加了标准的
MessageBox
处理,用于“复制到剪贴板”(使用 **<Ctrl> + <C>** 和 **<Ctrl> + <Insert>**)。 - Tab 键处理已更正(仅通过可见按钮进行 Tab 切换)。
- 添加了标准的
MessageBox
处理,用于 ALT 键盘快捷键。
版本 1.2 - 2013 年 8 月 10 日
- 不再
ShowInTaskbar
(原始MessageBox
也隐藏在taskbar
中)。 - 添加了 Escape 按钮的处理。
- 将右上角的关闭按钮(红色 X)调整为行为类似于
MessageBox
(但隐藏而不是禁用)。
版本 1.1 - 2013 年 6 月 14 日
- 一些重构。
- 添加了内部窗体类。
- 添加了缺失的代码注释等。
版本 1.0 - 2013 年 4 月 15 日
- 初始版本