在MessageBox c+中显示一个变量+; 如何在MessageBox C++中实现变量的显示? string name = "stackoverflow"; MessageBox(hWnd, "name is: <string name here?>", "Msg title", MB_OK | MB_ICONQUESTION);

在MessageBox c+中显示一个变量+; 如何在MessageBox C++中实现变量的显示? string name = "stackoverflow"; MessageBox(hWnd, "name is: <string name here?>", "Msg title", MB_OK | MB_ICONQUESTION);,c++,windows,messagebox,C++,Windows,Messagebox,这个呢 int id = '3'; MessageBox(hWnd, "id is: <int id here?>", "Msg title", MB_OK | MB_ICONQUESTION); 如何用C++来做?< /强> < p>回答你的问题: 字符串名称='stackoverflow' MessageBox(“名称为:”+名称,“消息标题”,MB|u OK | MB|u i查询) 对其他人也这样做。创建一个临时缓冲区来存储字符串,并使用sprintf,根据变量类型更改

这个呢

int id = '3';

MessageBox(hWnd, "id is: <int id here?>", "Msg title", MB_OK | MB_ICONQUESTION);

<强>如何用C++来做?< /强>

< p>回答你的问题:

字符串名称='stackoverflow'

MessageBox(“名称为:”+名称,“消息标题”,MB|u OK | MB|u i查询)


对其他人也这样做。

创建一个临时缓冲区来存储字符串,并使用
sprintf
,根据变量类型更改格式。对于您的第一个示例,以下内容应适用:

 char buff[100];
 string name = "stackoverflow";
 sprintf_s(buff, "name is:%s", name.c_str());
 cout << buff;
要将int更改为:

int d = 3;
sprintf_s(buff, "name is:%d",d);

这可以通过宏来完成

#define MSGBOX(x) \
{ \
   std::ostringstream oss; \
   oss << x; \
   MessageBox(oss.str().c_str(), "Msg Title", MB_OK | MB_ICONQUESTION); \
}

这是唯一一个对我有效的方法:

std::string myString = "x = ";
int width = 1024;
myString += std::to_string(width);

LPWSTR ws = new wchar_t[myString.size() + 1];
copy(myString.begin(), myString.end(), ws);
ws[myString.size()] = 0; // zero at the end

MessageBox(NULL, ws, L"Windows Tutorial", MB_ICONEXCLAMATION | MB_OK);

看到人们还在摆弄缓冲区是不好的。这在1998年是不必要的,在今天也是如此

std::string name = "stackoverflow";

MessageBox(hWnd, ("name is: "+name).c_str(), "Msg title", MB_OK | MB_ICONQUESTION);
如果您使用的是Unicode(这在21世纪很有意义)


我尝试了这个
std::string mynm=“stackoverflow”;消息框(hWnd,L“名称为:+mynm,L“消息标题”,MB|U OK | MB|U IConquest)但错误为1 IntelliSense:没有运算符“+”与这些操作数匹配操作数类型为:const wchar_t[10]+std::stringremove L from语句这完全是错误的。+运算符不连接C++中的字符串文字;它执行指针运算。我已将sprintf更改为sprintf_s,代码工作正常非常感谢您表示感谢单击数字下方的绿色复选框接受答案。@Gaspa79 Erm您不应该直接包含winuser.h。根据文件。
#define MSGBOX(x) \
{ \
   std::ostringstream oss; \
   oss << x; \
   MessageBox(oss.str().c_str(), "Msg Title", MB_OK | MB_ICONQUESTION); \
}
string x = "fred";
int d = 3;
MSGBOX("In its simplest form");
MSGBOX("String x is " << x);
MSGBOX("Number value is " << d);
void MsgBox(const char* str, ...)
{
    va_list vl;
    va_start(vl, str);
    char buff[1024];  // May need to be bigger
    vsprintf(buff, str, vl);
    MessageBox(buff, "MsgTitle", MB_OK | MB_ICONQUESTION);
}
string x = "fred";
int d = 3;
MsgBox("In its simplest form");
MsgBox("String x is %s", x.c_str());
MsgBox("Number value is %d", d);
std::string myString = "x = ";
int width = 1024;
myString += std::to_string(width);

LPWSTR ws = new wchar_t[myString.size() + 1];
copy(myString.begin(), myString.end(), ws);
ws[myString.size()] = 0; // zero at the end

MessageBox(NULL, ws, L"Windows Tutorial", MB_ICONEXCLAMATION | MB_OK);
std::string name = "stackoverflow";

MessageBox(hWnd, ("name is: "+name).c_str(), "Msg title", MB_OK | MB_ICONQUESTION);
std::wstring name = L"stackoverflow";

MessageBox(hWnd, (L"name is: "+name).c_str(), L"Msg title", MB_OK | MB_ICONQUESTION);