简体   繁体   English

带有自己的按钮文本的消息框

[英]Message boxes with own button texts

A lot of applications on Windows show plain message boxes just with "Yes"/"No" or "OK"/"Cancel" buttons. Windows上的许多应用程序仅通过“是” /“否”或“确定” /“取消”按钮显示简单的消息框。 Often it gets worse on non-English Windows' that the text is in English, but the buttons are labeled in the Windows' language. 在非英语Windows上,文本通常是英文,但按钮以Windows的语言标记常常会变得更糟。

Is there no simple API to show a message box with own button labels, eg "Delete" or "Keep"? 是否没有简单的API显示带有自己的按钮标签的消息框,例如“删除”或“保留”?

There is indeed such a Win32 function. 确实有这样的Win32函数。 It is TaskDialogIndirect . 它是TaskDialogIndirect Note that it was introduced in Vista and so if you are still attempting to support XP, then you would need to provide a fall back option for that platform. 请注意,它是在Vista中引入的,因此,如果您仍在尝试支持XP,则需要为该平台提供一个后备选项。

Like DavidHeffernan said, you should use TaskDialogIndirect() , as it natively supports custom buttons. 就像DavidHeffernan所说的那样,您应该使用TaskDialogIndirect() ,因为它本身支持自定义按钮。 It is only available on Vista and later, though. 不过,它仅在Vista和更高版本上可用。

On XP and earlier, you can customize the button captions on a standard MessageBox() (or really, any customization you want) by using SetWindowsHookEx() to install a thread-local WH_CBT hook, or use SetWinEventHook() , to obtain the dialog's HWND when it is created, then you can manipulate the dialog however you want. 在XP和更早版本上,可以使用SetWindowsHookEx()安装本地线程WH_CBT钩子,或使用SetWinEventHook()来获得对话框的内容,从而自定义标准MessageBox()上的按钮标题(或实际上,您需要进行任何自定义)。创建HWND时,您可以根据需要操作该对话框。 You can use GetDlgItem() to get the HWND of the individual buttons (and can thus use standard API functions/messages with them), and/or use GetDlgItemText() to change their captions. 您可以使用GetDlgItem()来获取各个按钮的HWND (从而可以与它们一起使用标准API函数/消息),和/或使用GetDlgItemText()来更改其标题。 The CtrlID of each button is the same value that MessageBox() returns when that button is clicked. 每个按钮的CtrlID与单击该按钮时MessageBox()返回的值相同。

This Projct on Codeproject provides a pretty good drop in replacement for MessageBox , that allows to replace buttons labels among other features. Codeproject上的项目提供了一个相当不错的替代MessageBox ,可以替换按钮标签以及其他功能。

It is basically a rewritten MessageBox function that is a superset of the standard Windows MessageBox function. 它基本上是重写的MessageBox函数,它是标准Windows MessageBox函数的超集。 Full source code is provided so you can adapt is easily to your needs if needed. 提供了完整的源代码,因此您可以根据需要轻松地适应您的需求。

And it's pure Win32, no MFC is involved here. 它是纯Win32,此处不涉及MFC。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM