2013-01-08 60 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,那麼爲什麼要打開消息框?沒有任何意義。 –

相關問題