Win32自定义消息框

发布于 2024-12-11 07:44:25 字数 332 浏览 0 评论 0原文

我想制作一个自定义消息框。我想要自定义的是按钮的文本。

MessageBoxW(
  NULL,
  L"Target folder already exists. Do you want to overwrite the folder?",
  L"No title",
  MB_YESNOCANCEL | MB_ICONQUESTION
  );

我只想将按钮文本更改为覆盖跳过取消
最简单的方法是什么?

我必须使其与 Windows 默认消息框具有相同的外观和感觉。

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?

I have to make this as having same look and feel with Windows default messagebox.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

天邊彩虹 2024-12-18 07:44:25

正如其他人所说,典型的方法是创建一个对话框资源并拥有一个完全独立的对话框,您需要以看起来像标准对话框的方式设计该GUI(以满足您对感觉和外观的要求)。如果您想接受短信,您可能需要添加适当调整窗口大小的代码。

不过,对于那些想要深入研究高级事物的人来说,还有另一种选择。虽然 MessageBox API 没有提供太多的 fint 调整功能,但您仍然可以使用 SetWindowsHookEx。注册钩子后,您可以拦截标准 MessageBox 窗口过程并按照您喜欢的方式对其进行子类化。

典型的事情包括:

  • 更改按钮文本
  • 添加更多控件
  • 添加定时自动关闭挂钩

标准窗口可以执行所有这些操作。

UPD。嘿,我意识到我有一些 SetWindowsHookEx 代码可以分享:http://alax.info/blog/127< /a>

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.

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.

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

赤濁 2024-12-18 07:44:25

您可以创建一个自己的对话框。或者,您可以使用本文中所述的窗口挂钩。

该文章的存档版本可以在 web.archive.com

You could create an own dialog. Or you could use a window hook as described in this article.

An archived version of the article can be found on web.archive.com.

霞映澄塘 2024-12-18 07:44:25

创建一个对话框资源(使用 GUI 编辑器或手动)并调用 DialogBox 就可以了。除了其参数支持的行为之外,无法改变 MessageBox 的行为。

也就是说,您的消息框可以很好地使用库存是/否选项。

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.

That said, your message box can very well use stock Yes/No options.

站稳脚跟 2024-12-18 07:44:25

Vista 中引入的任务对话框功能完全按照您的要求进行操作并遵循流行的系统主题。但是,如果您必须支持 XP,那么这对您来说就不太舒服了。

The task dialog functionality 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.

月下凄凉 2024-12-18 07:44:25

我知道这个问题很老了,但我现在才偶然发现它。
我想扩展有关使用 TaskDialog 而不是 MessageBox 的其他答案。下面是一个使用 TaskDialog 来精确执行所要求的操作的简洁示例; 更改按钮的文本

const TASKDIALOG_BUTTON buttons[] = { {IDYES, L"Overwrite"}, {IDNO, L"Skip"}, {IDCANCEL, L"Cancel"} };
TASKDIALOGCONFIG taskDialogConfig = {
    .cbSize = sizeof(TASKDIALOGCONFIG),
    .pszMainIcon = TD_WARNING_ICON, // TaskDialog does not support a question icon; see below
    .pButtons = buttons,
    .cButtons = ARRAYSIZE(buttons),
    .pszWindowTitle = L"No title",
    .pszContent = L"Target folder already exists. Do you want to overwrite the folder?"
};
TaskDialogIndirect(&taskDialogConfig, NULL, NULL, NULL);

一些值得注意的事情:

  • 当不指定父窗口时,您需要使用 TaskDialogIndirect,而不是基本的 TaskDialog 函数
  • ,图标pszMainIcon 中指定的内容也会显示在标题栏中。
  • 没有与 MessageBox 的 MB_ICONQUESTION 等效的内容,引用来自 此论坛帖子:< code>不要使用问号图标来提问。再次强调,仅对帮助入口点使用问号图标。无论如何,都不需要使用问号图标来提问 - 将主指令作为问题就足够了。
  • 检查选择了哪个按钮必须通过将指向 int 的指针作为第二个参数传递来完成TaskDialogIndirect 并在返回时检查其值(文档应该很清楚)

I know this question is old, but I just stumbled upon it now.
I would like to expand the other answers in regards to using a TaskDialog instead of a MessageBox. Here's a concise example of using a TaskDialog to do precisely what was asked; change the button's texts:

const TASKDIALOG_BUTTON buttons[] = { {IDYES, L"Overwrite"}, {IDNO, L"Skip"}, {IDCANCEL, L"Cancel"} };
TASKDIALOGCONFIG taskDialogConfig = {
    .cbSize = sizeof(TASKDIALOGCONFIG),
    .pszMainIcon = TD_WARNING_ICON, // TaskDialog does not support a question icon; see below
    .pButtons = buttons,
    .cButtons = ARRAYSIZE(buttons),
    .pszWindowTitle = L"No title",
    .pszContent = L"Target folder already exists. Do you want to overwrite the folder?"
};
TaskDialogIndirect(&taskDialogConfig, NULL, NULL, NULL);

Some noteworthy things:

  • You need to use TaskDialogIndirect, not the basic TaskDialog function
  • when not specifying a parent window, the icon specified in pszMainIcon is displayed in the title bar as well
  • There is no equivalent to the MessageBox's MB_ICONQUESTION, quoting a quote from this forumpost: Don't use the question mark icon to ask questions. Again, use the question mark icon only for Help entry points. There is no need to ask questions using the question mark icon anyway—it's sufficient to present a main instruction as a question.
  • checking which button was selected would have to be done by passing a pointer to an int as the second argument of TaskDialogIndirect and checking its value on return (the documentation should be pretty clear)
断桥再见 2024-12-18 07:44:25

这里是一个小型开源库,可以让您自定义消息框。由 Hans Ditrich 开发。
我已在另一个允许嵌入此类 MessageBox 中的自定义图标甚至可以从 控制台应用程序

我还应该指出任务对话框。这是使用它的示例:

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
}

Here is a small open source library that allows you to customize Message Boxes. Developed by Hans Ditrich.
I have successfully used it in another POC that allows embedding a custom icon in such MessageBox that can be called even from a Console application.

I should also point to the Task Dialog. Here is an example of using it:

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文