2013-01-08 118 views
-1

我想创建一个Assert函数,在不打开控制台的情况下在新窗口中显示消息。该函数需要独立于O.S,如果可能的话不使用C++外部库。MessageBox操作系统。独立

#include <string> 
#include <sstream> 
#ifdef WIN32 
#include <windows.h> // include windows header, for Windows Based Sistems. 
#else 
// ... 
#endif 

void Assert (bool cond,const char* file,int line,const char* desc) 
{ 
     if (cond) return; // No Assertion. 
#ifdef WIN32 
     // Use MessageBox function to display the information. 
     // For Example ... 
     std::stringstream st; 
     st << "There Was An Error At Runtime ! \n"; 
     st << "File: " << file << "\n"; 
     st << "Line: " << line << "\n"; 
     st << "Description: " << desc << "\n"; 
     st << "Do You Want To Continue Running the Application?\n"; 
     if (MessageBox (NULL,"Unexpected Error", str.str().c_str(), MB_YESNO) == IDNO) 
      exit (-1); 
#else 
     // Do Something, but in Unix Base Systems. 
#endif 
} 
#define assert(condition,description) \ 
      __assert__ (condition,__FILE__,__LINE__,description) 

极品C++代码以输出在其他O.S

+0

定义其他操作系统 - 有这么多 – Zaffy

+0

linux,macintosh –

+0

为什么不在#ifdef WIN32的#else之后编写代码?其余的会变得更简单,我们不必猜测。 –

回答

1

C++标准一个MessageBox不包括GUI的操作。你需要利用外部库提供所需平台的GUI服务。

所以你要求的是不可能的。抱歉。

+0

我应该使用哪些库? GTK,wkWidgets?最简单的库只是打印一个MessageBox? –

+0

@VictorRuiz对话框没有问题,问题在于它的阻止行为 – Zaffy

+0

@VictorRuiz由于您只需要一个消息框,请在每个系统上使用默认值。调出一个消息框不应该太困难。请注意,在Mac OS X上,您需要在Objective C中编写GUI代码,而不是C++。但是,你的用例看起来很奇怪。如果您的应用程序不是GUI,那么为什么要打开消息框?没有任何意义。 –

相关问题