在MessageBox C 中显示一个变量
Display a Variable in MessageBox c++
如何在MessageBox C ?
中显示变量string name = "stackoverflow";
MessageBox(hWnd, "name is: <string name here?>", "Msg title", MB_OK | MB_ICONQUESTION);
我想以以下方式显示(#1):
"name is: stackoverflow"
和这个?
int id = '3';
MessageBox(hWnd, "id is: <int id here?>", "Msg title", MB_OK | MB_ICONQUESTION);
我想以以下方式显示(#2):
id is: 3
如何使用C ?
创建一个临时缓冲区以存储您的字符串并使用sprintf
,请根据变量类型更改格式。对于您的第一个示例,以下应有效:
char buff[100];
string name = "stackoverflow";
sprintf_s(buff, "name is:%s", name.c_str());
cout << buff;
然后用buff呼叫消息框,为字符串参数
MessageBox(hWnd, buff, "Msg title", MB_OK | MB_ICONQUESTION);
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);
}
使用
string x = "fred";
int d = 3;
MSGBOX("In its simplest form");
MSGBOX("String x is " << x);
MSGBOX("Number value is " << d);
另外,您可以使用varargs(老式的方式:不是我尚未掌握的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);
这是唯一对我有用的人:
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::wstring name = L"stackoverflow";
MessageBox(hWnd, (L"name is: "+name).c_str(), L"Msg title", MB_OK | MB_ICONQUESTION);
回答您的问题:
字符串名称='stackoverflow';
MessageBox("名称为:" 名称," msg title",mb_ok | mb_iconquestion);
对他人以相同的方式做。
相关文章:
- 用C++中的一个变量定义一个常量
- 一个变量的输入值也会保存到另一个变量中
- 将双精度变量设置为另一个变量的值
- 对具有相同方法的不同类使用一个变量
- 为什么一个变量获得与另一个值相同的值
- 尝试在 C++ 中为 ifstream 提供一个变量
- 类中的一个变量显示,但另一个不显示
- 声明一个变量,该变量在 c++ 或 c 中具有值,当程序终止时不会被销毁
- c++问题:给一个变量赋值后,另一个变量发生了变化
- 如何将一个变量用于父类和派生类
- 如何在循环中使用scanf,将值存储到一个变量中,然后打印出来?
- 我如何将一个变量与另一个变量进行比较,例如我想如果(var1 > var2 x 1),然后执行此 c++
- 如何在一个函数中定义一个变量,并在另一个函数中访问和更改它?(C++)
- 如果一个变量在它之前释放了另一个(相同的数据类型)变量,如何将其分配给内存?
- 迭代器或反向器的一个变量
- fstream库,试图创建一个变量名为(c++)的文件
- 如何在 c++ 中将两个不同类型的变量分配给一个变量
- 如何将一些变量放在一个变量中?
- 将 int 转换为字符串,然后连接另一个变量以创建完整扩展名,然后将其转换为 const_char*
- 如何用索引命名一个变量来存储输入 mxArray?