Archived

This topic is now archived and is closed to further replies.

tuxx

Fancy MessageBox() text

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
That will not work if he wants the string that message points to. Lookup sprintf or ostrstream.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites