Jump to content
  • Advertisement
Sign in to follow this  
counterrabbit

printing out variables in MessageBox

This topic is 4750 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hey there, im building a win32 app and i need to trace a variable to the screen as the game is running, i want to print out the value of int a; for example like so MessageBox(NULL, a, "MY MESSAGE", MB_OK); but obviouslt i cant dio that as the parameter at a is of type text string . . . so how do i make it print out my variable?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster



Easy.





{
char strx[128]; // or long enuf for whatever you are printing
sprintf(strx, " MSG HERE IntVar= %d FloatVar= %f StringVar= '%s'",I,F,S);
MessageBox(NULL, a, strx, MB_OK);
}


If you reuse the same pattern alot (msgstr + vartype) make a macro out of it.






Share this post


Link to post
Share on other sites
Exactly the same as printf. But, it prints to the cerr, or stderr stream.

Why don't I use fprintf you ask? That is a fairly good question.


void print(char* format, ...)
{
int str_len; // total length of string with args
va_list m; // args list
char str[512]; // string to print args into

// print into string
va_start(m, format);
str_len = vsprintf(str, format, m);
va_end(m);

std::cerr << str;

if( str[str_len-1] != '\n' )
std::cerr << std::endl;

std::cerr.flush();
}

Share this post


Link to post
Share on other sites
MessageBox(NULL, boost::lexical_cast< std::string >(a).c_str(), "MY MESSAGE", MB_OK);

Which can be rewritten as a more generic templated function:
#include <windows.h>
#include <boost/lexical_cast.hpp>

#undef MessageBox

template < typename TYPE >
int MessageBox(HWND windowHandle, TYPE const & value, char const * title, unsigned int type)
{

#if defined(__UNICODE)

return MessageBoxW(windowHandle, boost::lexical_cast< std::wstring >(value).c_str(), title, type);

#else

return MessageBoxA(windowHandle, boost::lexical_cast< std::string >(value).c_str(), title, type);

#endif

}

int main()
{
MessageBox(0, 32, "Test", MB_OK);
}

Enigma

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!