Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display a Variable in MessageBox c++

How to display a Variable in 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):

"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):

id is: 3

how to do this with c++ ?

like image 544
a7md0 Avatar asked Feb 07 '14 06:02

a7md0


Video Answer


3 Answers

Answer to your question:

string name = 'stackoverflow';

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

do in same way for others.

like image 135
user3148898 Avatar answered Sep 20 '22 14:09

user3148898


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);
like image 22
Gilles Walther Avatar answered Sep 17 '22 14:09

Gilles Walther


It's not good to see people still messing with buffers. That was unnecessary in 1998, and definitely today.

std::string name = "stackoverflow";

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

If you're using Unicode (which makes sense in the 21st century)

std::wstring name = L"stackoverflow";

MessageBox(hWnd, (L"name is: "+name).c_str(), L"Msg title", MB_OK | MB_ICONQUESTION);
like image 44
MSalters Avatar answered Sep 16 '22 14:09

MSalters