I'm writing a wxWidgets GUI application, but it also uses some console objects.
I need a way of displaying stdout and accessing stdin; the bes开发者_JAVA技巧t way to do this, would be displaying the console as well as the GUI. This can be done if a user runs the program from the command prompt/shell etc, but the command prompt does not automatically open to view stdout when the application is ran.
I know this has to be possible, because when you run a console application, the console runs automatically. I found one or two solutions that require the Windows API, but sadly my code needs to be cross platform (I'm developing this on Linux).
The solution is very simple: use wxStreamToTextRedirector. This allows console output to be redirected to a text control. You could create a separate window for this and color it to look like a console. The link above provides an example.
If using Code::Blocks, in project properties under Build Targets, there is an option to build the project as a console application. Choosing that will have the app run with the console attached.
WxWidgets has macro's (wxIMPLEMENT_APP_CONSOLE, wxIMPLEMENT_APP) for showing the console (or not). Seems to work fine, you can pick the right macro depending on your preprocessor definitions.
class MyApp: public wxApp
{
public:
virtual bool OnInit();
};
#ifdef _DEBUG
wxIMPLEMENT_APP_CONSOLE(MyApp);
#else
wxIMPLEMENT_APP(MyApp);
#endif
bool MyApp::OnInit()
{
MainWindow *frame = new MainWindow( TOOLNAME, wxPoint(50, 50), wxSize(600,400) );
frame->Maximize();
...
return true;
}
精彩评论