Fancy MessageBox() text

Started by
8 comments, last by tuxx 21 years, 9 months ago
Hey all. I''m making an assert macro, and in the message box, I want to put in a message that explains there was an assertion error, and also insert the line and file of the error with it (pretty much your standard assert macro). I''ve run into a problem, though. In the text parameter of MessageBox(), you can only have something like ''message'' or ''"hello world."'' Is there anyway I can do something like: ''"Hello World." message''? Anyway, heres some sample code to help your understanding of my question:
  
char *message = "This is a variable message.";

MessageBox(NULL, "Hello world. " message " See ya later world.", "Sample", MB_OK);
  
I would like that MessageBox() to say something like ___________________________________________________ | Sample | --------------------------------------------------- | Hello world. This is a variable message. See ya | | later world. | | | | OK | |-------------------------------------------------| And I can''t do something like "Hello world. " >> message >> " See ya later world." Thanks.
[email=dumass@poppet.com]dumass@poppet.com[/email]
Advertisement
You forgot to escape your " characters in your string literal.

Change it to

"Hello world. \" message \" See ya later world."

and you''re good to go.
That will not work if he wants the string that message points to. Lookup sprintf or ostrstream.
If you want variable text in a message box, do this:

  int myNumber = 99;char strMessage[128];sprintf(strMessage, "Hello, World! # = %i", myNumber);MessageBox(hwnd, strMessage, "My Message", MB_OK);  
Thanks!
[email=dumass@poppet.com]dumass@poppet.com[/email]
Or:

  #define ErrorBox(msg) MessageBox(NULL,"An Error Occured.\nThe Error Was:\n" msg "\nThat was the message up there.","Error",MB_OK|MB_ICONERROR)...void Func(int n){   if(!IsEven(n))      ErrorBox("n must be even!");}  


2p
Steve

Steve
DirectX Programmer
Soon to be the new Bill Gates
Member of the Unban Mindwipe Society (UMWS)
sprintf works well, like the previous poster stated.

Another option that is more "C++" is to use ostrstream.

#include "strstrea.h"

ostrstream str;
str << "Hello World: " << message << endl;
MessageBox(NULL, str.str(), title, MB_OK);

Its a little more awkward in this curcumstance, so i wouldn''t really recommend it. It does let you use the << operator to format, which i prefer.
Ambassador: Mr. Bush are you stoned or just really, REALLY dumb?Pres. Bush - I assure you I am not stoned.
Hmm.. I don''t know whether this will work or not:

  // assume that the share_ptr behave like boost::share_ptr// and have the following overloaded operator (i haven use boost yet)// operator char* ()             { return _pData; }// operator const char *() const { return _pData; }share_ptr<char> operator + (const char *str1, const char *str2){  char *p = new char[strlen(str1) + strlen(str2) + 1];  share_ptr<char> newstr(p);  strcpy(p, str1);  strcat(p, str2);  return newstr;}//...char *message = "This is a variable message.";MessageBox(NULL, "Hello world. " + message + " See ya later world.", "Sample", MB_OK);    
"after many years of singularity, i'm still searching on the event horizon"
Good grief, I completely missed what he was trying to do. I must have skipped over it. I was wondering why he would want quotation marks around an error message.

The other folks are right, I... I am a fool sometimes.
Thanks guys!
[email=dumass@poppet.com]dumass@poppet.com[/email]

This topic is closed to new replies.

Advertisement