Messagebox检测YesNoCancel按钮

问题描述 投票:2回答:4

我在C ++ Builder中使用VCL Forms应用程序。我可以在某些代码中帮助显示带有YesNoCancel按钮的消息框,然后检测是否按下了是,否或取消按钮。

这是我的代码:

if(MessageBox(NULL, "Test message", "test title",  MB_YESNOCANCEL) == IDYES)
{

}

我包括以下内容:

#include <windows.h>

我收到以下错误:

E2034无法将'char const [13]'转换为'const wchar_t *'

E2342参数'lpText'中的类型不匹配(想要'const wchar_t *',得到'const char *')

更新

这是我的代码:

const int result = MessageBox(NULL, L"You have " + integerNumberOfImportantAppointments + " important appointments. Do you wish to view them?", L"test title",  MB_YESNOCANCEL);

值:integerNumberOfImportantAppointments是一个整数。如何在消息框中显示?

我收到以下错误:无效的指针添加。

此外,我可以选择消息框的图标吗?这种情况下的一个问题。

c++ messagebox vcl
4个回答
9
投票

干得好。您需要在调用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的文档。


3
投票

我没有使用C ++ Builder的经验,但似乎你正在使用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构建中使用narrow(char *)字符串(请参阅'_TCHAR映射到'Project Options中的'部分)

有关更多详细信息,请参阅here


1
投票

我不确定如何在C ++ Building中执行此操作,但您需要启用我认为像multybit字符之类的东西,但您需要使用编译器检查文档。


0
投票

上面写的所有内容可能已经过时了VS 2015.在我的情况下

 MessageBox(NULL, L"Test message", L"test title",  MB_YESNOCANCEL);

不起作用,因为第一个论点过多。错误输出是:

to many arguments in functional call.

如果我写道:

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;
}

它会工作!

© www.soinside.com 2019 - 2024. All rights reserved.