C++ 消息框检测到 YesNoCancel 按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12578642/
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
Messagebox detect YesNoCancel button
提问by user1690531
I am using a VCL Forms application in C++ Builder. can I please have some help in some code to display a messagebox with YesNoCancel buttons and then detect if the yes, the no or the cancel button is pressed.
我在 C++ Builder 中使用 VCL Forms 应用程序。我可以在某些代码中获得一些帮助以显示带有 YesNoCancel 按钮的消息框,然后检测是否按下了是、否或取消按钮。
Here is my code:
这是我的代码:
if(MessageBox(NULL, "Test message", "test title", MB_YESNOCANCEL) == IDYES)
{
}
I have included the following:
我已经包括以下内容:
#include <windows.h>
I am getting the following errors:
我收到以下错误:
E2034 Cannot convert 'char const[13]' to 'const wchar_t *'
E2034 无法将“char const[13]”转换为“const wchar_t *”
E2342 Type mismatch in parameter 'lpText' (wanted 'const wchar_t *', got 'const char *')
E2342 参数“lpText”中的类型不匹配(想要“const wchar_t *”,得到“const char *”)
Update
更新
Here is my code:
这是我的代码:
const int result = MessageBox(NULL, L"You have " + integerNumberOfImportantAppointments + " important appointments. Do you wish to view them?", L"test title", MB_YESNOCANCEL);
The value: integerNumberOfImportantAppointments is an integer. How can I display this in a messagebox?
值: integerNumberOfImportantAppointments 是一个整数。如何在消息框中显示它?
I am getting the following error: Invalid Pointer Addition.
我收到以下错误:无效的指针添加。
Also, can I choose the icon for the messagebox? A question in this case.
另外,我可以选择消息框的图标吗?在这种情况下的一个问题。
回答by Mark Ingram
Here you go. You need to use wide characters in the call to MessageBox
and you need to store the result in a variable, before working out what to do next.
干得好。您需要在调用中使用宽字符,MessageBox
并且需要将结果存储在变量中,然后再确定下一步做什么。
const int result = MessageBox(NULL, L"Test message", L"test title", MB_YESNOCANCEL);
switch (result)
{
case IDYES:
// Do something
break;
case IDNO:
// Do something
break;
case IDCANCEL:
// Do something
break;
}
Update, following question edit:
更新,以下问题编辑:
// Format the message with your appointment count.
CString message;
message.Format(L"You have %d important appointments. Do you wish to view them?", integerNumberOfImportantAppointments);
// Show the message box with a question mark icon
const int result = MessageBox(NULL, message, L"test title", MB_YESNOCANCEL | MB_ICONQUESTION);
You should read the documentation for MessageBox.
您应该阅读MessageBox的文档。
回答by Zdeslav Vojkovic
I have no experience with C++ Builder, but it seems that you are using ANSI strings where UNICODE (actually wide character, but let's ignore details for the moment) strings are required. Try this:
我没有使用 C++ Builder 的经验,但似乎您正在使用 ANSI 字符串,其中需要 UNICODE(实际上是宽字符,但让我们暂时忽略细节)字符串。尝试这个:
if(MessageBox(NULL, L"Test message", L"test title", MB_YESNOCANCEL) == IDYES)
Even better, to ensure that your strings are conforming to your app settings, you can use:
更好的是,为确保您的字符串符合您的应用程序设置,您可以使用:
if(MessageBox(NULL, _T("Test message"), _T("test title"), MB_YESNOCANCEL) == IDYES)
This will result in wide (wchar_t*) strings being used in UNICODE builds, and narrow (char*) strings in non-UNICODE builds (see '_TCHAR maps to' part in the Project Options)
这将导致在 UNICODE 构建中使用宽 (wchar_t*) 字符串,在非 UNICODE 构建中使用窄 (char*) 字符串(请参阅项目选项中的“_TCHAR 映射到”部分)
For more details, see here
有关更多详细信息,请参阅此处
回答by Lukeme9X
Im not sure about how to do this in C++ Building, but you need to enable I think something like multybit characters, but you need check against the documentation with your compiler.
我不确定如何在 C++ Building 中执行此操作,但是您需要启用我认为类似多位字符的内容,但是您需要使用编译器检查文档。
回答by Andrew Kachalin
All which is written above probably is obsolete for VS 2015. In my case
上面写的所有内容对于 VS 2015 来说可能已经过时了。就我而言
MessageBox(NULL, L"Test message", L"test title", MB_YESNOCANCEL);
doesn't work, because first argument excess. Error output was:
不起作用,因为第一个参数过多。错误输出是:
to many arguments in functional call.
to many arguments in functional call.
If I wrote:
如果我写:
const int result = MessageBox( L"Test message", L"test title", MB_YESNOCANCEL); //Without NULL
switch (result)
{
case IDYES:
// Do something
break;
case IDNO:
// Do something
break;
case IDCANCEL:
// Do something
break;
}
it will be work!
这将是工作!