带有“验证”和“取消”按钮的消息框
如何拥有一个带有两个按钮(验证和取消)的消息框?
How can I have a msgbox with two buttons, Validate and Cancel?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何拥有一个带有两个按钮(验证和取消)的消息框?
How can I have a msgbox with two buttons, Validate and Cancel?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(1)
不幸的是,标准 Win32
MessageBox
函数 确实不支持带有自定义标签的按钮。由于 VB.NETMsgBox
函数是该本机函数的薄包装器,因此它也不支持它们。您获得的只是预定义的值,这意味着您最多可以做的就是“确定”和“取消”之类的操作,并有文本解释“确定”意味着“继续验证”。值得注意的是,多年来,这是 Windows 样式指南认可的推荐做法。事实上,它看起来仍然是它仍然是。具体来说,请注意以下一般规则的例外情况:
Windows Vista 引入了一个新的 API,旨在取代老化且无法配置的
MessageBox
,它称为TaskDialog
。如果您运行的是 Windows Vista 或 7,您无疑已经在整个 Windows shell 中看到过此对话框。事实上,它确实允许您为每个按钮指定自定义名称,并提供一系列其他自定义选项。但这种控制并不是免费的。TaskDialog
函数有两个问题:.NET Framework 不包含开箱即用的包装器。您需要编写自己的简单包装器,或者下载 Windows API 代码包,其中包括这样的包装纸。但这给您的代码增加了额外的依赖性,您必须决定这是否值得。
由于该API仅在Vista中引入,这意味着程序只能在Vista或更高版本上运行时才能使用它。如果您仍然需要以 Windows XP(或 2000)为目标,那么您就不走运了。显示
TaskDialog
的代码将失败,您需要包含一个后备例程来显示标准MessageBox
。当然,这会让您回到开始的地方,而无法在这些旧操作系统上自定义按钮标签。同样,只有您可以决定这是否是您的应用程序和/或部署方案的一个重要问题。一代又一代 VB 程序员使用的另一个选项是创建您自己的
MessageBox
风格的小表单。布置一个简单的表单并放置一个图标/图像、一些标题文本和所有您想要的按钮并不难。由于您已经创建了整个表单,因此您可以根据自己的意愿自由地从代码中对其进行自定义。如果您在 Vista 之前的 Windows 版本中绝对需要此功能,那么创建您自己的消息框表单是您唯一的选择。否则,我强烈建议您利用新的
TaskDialog
API。在应用程序中这样做可以提高与用户可能已安装在其计算机上的其他应用程序的一致性,甚至与 Windows 本身的一致性。很难确保您自己的自定义消息框表单得到正确的小细节,例如根据标签文本的长度和用户的屏幕尺寸自动换行。并在标题栏中显示/隐藏“X”关闭按钮,具体取决于您的对话框是否包含“取消”按钮。标准 WindowsMessageBox
/TaskDialog
还可以免费为您完成大量其他事情,而您无需费力。这只是对一般原则的重申:当你没有绝对必要时,永远不要重新发明轮子。一个好的折衷方案可能是使用类似 this
TaskDialog
包装器/模拟器。在 Vista 及更高版本中,本地TaskDialog
API 可用,它会自动调用该函数。否则,它使用标准形式并尝试模拟本机TaskDialog
API 的行为。我已经编写了一个类似的自定义类供自己使用,但我从未抽出时间将其发布到网上。Unfortunately, the standard Win32
MessageBox
function does not support buttons with custom labels. And since the VB.NETMsgBox
function is a thin wrapper over that native function, it doesn't support them either. The pre-defined values are all you get, meaning that the best you can do is something like "OK" and "Cancel", with text explaining that "OK" means to "proceed with validation".It's worth noting that for years, this was the recommended practice endorsed by the Windows style guides. In fact, it looks like it still is. Specifically, note the exceptions to the following general rule:
Windows Vista introduced a new API intended to replace the aging and impossible-to-configure
MessageBox
—it's called theTaskDialog
. If you're running either Windows Vista or 7, you've undoubtedly seen this dialog used throughout the Windows shell. It does, in fact, allow you to specify custom names for each of the buttons, and provides an array of other customization options as well. But this kind of control doesn't come for free. There are two problems with theTaskDialog
function:The .NET Framework does not include a wrapper for it out of the box. You'll need to either write your own simple wrapper, or download the Windows API Code Pack that includes such a wrapper. But this adds an extra dependency to your code—you'll have to decide whether that's worth it or not.
Because the API was only introduced in Vista, that means programs can only use it when running on Vista or later. If you still need to target Windows XP (or 2000), you're out of luck. The code to show a
TaskDialog
will fail, and you'll need to include a fallback routine to show a standardMessageBox
. Which of course puts you right back where you started, without the ability to customize the button labels, on those legacy operating systems. Again, only you can decide if this is a significant concern for your application and/or deployment scenario.The other option, used by generations of VB programmers, is to bang up your own little
MessageBox
-style form. It's not that hard to lay out a simple form with a place for an icon/image, some caption text, and all the buttons you want. Since you've created the entire form, you'll be free to customize it from your code however you wish.If you absolutely need this functionality in versions of Windows prior to Vista, then creating your own message box form is about your only option. Otherwise, I highly recommend that you take advantage of the new
TaskDialog
API. Doing so within your application promotes consistency with other applications the user is likely to have installed on their computer, and even with Windows itself. It's difficult to ensure that your own custom message box form gets the little touches right, like automatically wrapping the label text depending on its length and the user's screen size. And showing/hiding the "X" close button in the title bar depending on whether your dialog includes a "Cancel" button. And an unbelievable number of other things that the standard WindowsMessageBox
/TaskDialog
does for you for free without your having to lift a finger. This is just a restatement of the general principle: never re-invent the wheel when you don't absolutely have to do so.A good compromise might be to use something like this
TaskDialog
wrapper/emulator. On Vista and later, where the nativeTaskDialog
API is available, it automatically calls that function. Otherwise, it uses a standard form and attempts to simulate the behavior of the nativeTaskDialog
API. I've written a similar custom class for my own use, but I never got around to publishing it online.