Attaching the console with the GUI in wxWidgets

匆匆过客 提交于 2020-01-02 05:47:05

问题


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 best 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).


回答1:


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.




回答2:


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.




回答3:


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;
}


来源:https://stackoverflow.com/questions/2457065/attaching-the-console-with-the-gui-in-wxwidgets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!