我正在 C++ Builder 中使用 VCL Forms 应用程序。
如何显示带有是/否/取消按钮的消息框,然后检测是否按下了是、否或取消按钮。
这是我的代码:
if(MessageBox(NULL, "Test message", "test title", MB_YESNOCANCEL) == IDYES) {}
我已包含以下内容:
#include <windows.h>
我收到以下错误:
E2034 Cannot convert 'char const[13]' to 'const wchar_t *'
E2342 Type mismatch in parameter 'lpText' (wanted 'const wchar_t *', got 'const char *')
给你。您需要在调用
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;
}
更新,编辑以下问题:
// 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);
您应该阅读 MessageBox 的文档。
您似乎正在使用 ANSI 字符串,其中需要 UNICODE(实际上是宽字符,但让我们暂时忽略细节)字符串。试试这个:
if(MessageBox(NULL, L"Test message", L"test title", MB_YESNOCANCEL) == IDYES)
更好的是,为了确保您的字符串符合您的应用程序设置,您可以使用:
if(MessageBox(NULL, _T("Test message"), _T("test title"), MB_YESNOCANCEL) == IDYES)
这将导致在 UNICODE 构建中使用宽 (
wchar_t*
) 字符串,在非 UNICODE 构建中使用窄 (char*
) 字符串(请参阅项目选项中的“_TCHAR 映射到”部分)。
有关更多详细信息,请参阅Embarcadero 文档维基。
我认为,您需要启用诸如多位字符之类的功能,但您需要使用编译器检查文档。
以前的所有答案对于 VS 2015 来说都已过时。就我而言:
MessageBox(NULL, L"Test message", L"test title", MB_YESNOCANCEL);
不起作用,因为第一个参数过多。错误输出为:
函数调用中的许多参数。
如果我写:
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;
}
它会起作用的!