在 MessageBox C++ 中显示变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21620752/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:44:12  来源:igfitidea点击:

Display a Variable in MessageBox c++

c++cwindowsmessagebox

提问by a7md0

How to display a Variable in MessageBox c++ ?

如何在 MessageBox c++ 中显示变量?

string name = "stackoverflow";

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

I want to show it in the following way (#1):

我想以下列方式展示它(#1):

"name is: stackoverflow"


and this?

和这个?

int id = '3';

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

and I want to show it in the following way (#2):

我想以下列方式展示它(#2):

id is: 3

how to do this with c++ ?

如何用 C++ 做到这一点?

回答by Gibby

Create a temporary buffer to store your string in and use sprintf, change the formatting depending on your variable type. For your first example the following should work:

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

 char buff[100];
 string name = "stackoverflow";
 sprintf_s(buff, "name is:%s", name.c_str());
 cout << buff;

Then call message box with buff as the string argument

然后以 buff 为字符串参数调用消息框

MessageBox(hWnd, buff, "Msg title", MB_OK | MB_ICONQUESTION);

for an int change to:

对于 int 更改为:

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

回答by cup

This can be done with a macro

这可以用宏来完成

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

To use

使用

string x = "fred";
int d = 3;
MSGBOX("In its simplest form");
MSGBOX("String x is " << x);
MSGBOX("Number value is " << d);

Alternatively, you can use varargs (the old fashioned way: not the C++11 way which I haven't got the hang of yet)

或者,您可以使用可变参数(老式方法:不是我尚未掌握的 C++11 方法)

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

回答by Gilles Walther

This is the only one that worked for me:

这是唯一对我有用的:

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

回答by user3148898

Answer to your question:

回答你的问题:

string name = 'stackoverflow';

字符串名称 = 'stackoverflow';

MessageBox("name is: "+name , "Msg title", MB_OK | MB_ICONQUESTION);

MessageBox("name is: "+name , "Msg title", MB_OK | MB_ICONQUESTION);

do in same way for others.

对其他人也一样。