windows Win32 自定义消息框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7847620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Win32 custom message box
提问by Benjamin
I want to make a custom message box. What I want to customize is the button's text.
我想做一个自定义消息框。我想要自定义的是按钮的文本。
MessageBoxW(
NULL,
L"Target folder already exists. Do you want to overwrite the folder?",
L"No title",
MB_YESNOCANCEL | MB_ICONQUESTION
);
I'd like to just change the buttons text to Overwrite
, Skip
, Cancel
.
What's the most simple way?
我只想将按钮文本更改为Overwrite
, Skip
, Cancel
。
最简单的方法是什么?
I have to make this as having same look and feel with Windows default messagebox.
我必须使它与 Windows 默认消息框具有相同的外观和感觉。
采纳答案by Roman R.
As said by others, a typical way is to create a dialog resource and have a completely independent dialog, which GUI you need to design in the way that it looks like standard dialog (to meet your request for feel and look). If you want to accept text messages, you might probably need to add code which resizes the window appropriately.
正如其他人所说,一种典型的方法是创建一个对话框资源并拥有一个完全独立的对话框,您需要以它看起来像标准对话框的方式设计该 GUI(以满足您对感觉和外观的要求)。如果您想接受文本消息,您可能需要添加适当调整窗口大小的代码。
Still, there is another option for those who feel like diving into advanced things. While MessageBox
API does not offer much for fint tuning, you still have SetWindowsHookEx
in your hands. Having registgered the hook, you can intercept standard MessageBox
window procedure and subclass it in the way you like.
尽管如此,对于那些想要深入研究高级事物的人来说,还有另一种选择。虽然MessageBox
API 没有提供太多的 fint 调优,但您仍然可以SetWindowsHookEx
掌握。注册钩子后,您可以拦截标准MessageBox
窗口过程并以您喜欢的方式对其进行子类化。
Typical things include:
典型的事情包括:
- changing button text
- adding more controls
- adding timed automatic close
- 更改按钮文本
- 添加更多控件
- 添加定时自动关闭
Hooking standard window can do all of those.
挂钩标准窗口可以完成所有这些。
UPD. Hey, I realized I have some code with SetWindowsHookEx
to share: http://alax.info/blog/127
更新。嘿,我意识到我有一些代码SetWindowsHookEx
可以分享:http: //alax.info/blog/127
回答by Norbert Willhelm
You could create an own dialog. Or you could use a window hook as described in this article.
您可以创建一个自己的对话框。或所描述的,你可以使用一个窗口钩子在这篇文章中。
回答by David Heffernan
The task dialogfunctionality introduced in Vista does exactly what you want and follows the prevailing system theme. However, if you have to support XP, then this will be of little comfort to you.
Vista 中引入的任务对话框功能完全符合您的要求,并遵循流行的系统主题。但是,如果您必须支持 XP,那么这对您来说就没什么安慰了。
回答by Cat Plus Plus
Make a dialog resource (with a GUI editor, or by hand) and call DialogBox
on it. There's no way to alter MessageBox
behaviour, other than what's supported by its arguments.
制作对话资源(使用 GUI 编辑器或手动)并调用DialogBox
它。除了MessageBox
它的论点所支持的之外,没有办法改变行为。
That said, your message box can very well use stock Yes/No options.
也就是说,您的消息框可以很好地使用股票是/否选项。
回答by Michael Haephrati
Hereis a small open source library that allows you to customize Message Boxes. Developed by Hans Ditrich.
I have successfully used it in another POCthat allows embedding a custom icon in such MessageBoxthat can be called even from a Console application.
这是一个小型开源库,可让您自定义消息框。由汉斯·迪特里希( Hans Ditrich)开发。
我已经在另一个POC中成功地使用了它,它允许在这样的 MessageBox中嵌入自定义图标,甚至可以从控制台应用程序调用它。
I should also point to the Task Dialog. Here is an example of using it:
我还应该指向Task Dialog。下面是一个使用它的例子:
int nButtonPressed = 0;
TaskDialog(NULL, hInst,
MAKEINTRESOURCE(IDS_APPLICATION_TITLE),
MAKEINTRESOURCE(IDS_DOSOMETHING),
MAKEINTRESOURCE(IDS_SOMECONTENT),
TDCBF_OK_BUTTON | TDCBF_CANCEL_BUTTON,
TD_WARNING_ICON,
&nButtonPressed);
if (IDOK == nButtonPressed)
{
// OK button pressed
}
else if (IDCANCEL == nButtonPressed)
{
// Cancel pressed
}