MessageBox

Started by
2 comments, last by _goat 17 years, 10 months ago
1)If I have a variable and it's of type Dword and I want to print it out in a messagebox like DWORD myone; I can't do MessageBox(NULL, myone, "name", MB_OK); because it requires a type LPCTSTR, so how do i convert it to LPCTSTR?
Advertisement
Try something like so:
char buffer[MAX_PATH] = "";DWORD myone;sprintf(buffer, "%d", myone);MessageBox(NULL, buffer, "title", MB_OK);

Basically what sprintf does is it writes variables (like integers, doubles, floats, characters, etc.) into a string buffer, which can then be passed into the MessageBox function. %d in the sprintf function stands for an integer value, while %f would be float, %s is a string, and then there are a few others I forget. Lookup sprintf in the MSDN library; should find all the info you need there.

There's also the itoa() function, but with sprintf you can format the output to be something more appealing.
IF you're using C++, you can use strings and this simple useful template function I've found somewhere(can't remember where):

template <class T> string ToString(T val)
{
std::stringstream ss;
ss<<val;
return ss.str();
}

Now you can just do:

MessageBox(NULL,ToString(myone).c_str(),"name",MB_OK);

Of course, you can use the ToString function with any type that can be converted into a string.
A more robust method than mikeman's would be to use Boost's lexical_cast function, which will also let you convert strings back into primitives. A link to Boost can be found in my signature.
[ search: google ][ programming: msdn | boost | opengl ][ languages: nihongo ]

This topic is closed to new replies.

Advertisement