I want to create a hybrid app that includes console and GUI together.Below is my homework which I don't quite sure whether it is right.The wrong part is the last line of code (MyApp::OnInit(),right before return true) that execute console output's not executed in the right order,it is before the GUI windows being showing.What's more,if SetExitOnFrameDelete(false), after the GUI windows deletion,the app did not quit,and have no way to quit.
Thanks for your help in advance.
Code: Select all
//This application demonstrates how to show a dialog in a console app.
#include <wx/wx.h>
#include <stdio.h>
enum
{
ID_Quit,
ID_CLICK_ME,
ID_DISABLE_ENABLE,
};
class MyApp : public wxApp
{
public:
bool OnInit ();
};
class MyFrame:public wxFrame
{
public:
MyFrame(const wxString& title,const wxPoint& pos,const wxSize& size);
void OnQuit(wxCommandEvent& event);
void OnClickMe(wxCommandEvent& event);
void OnDisableEnable(wxCommandEvent& event);
void OnClose(wxCloseEvent& event);
private:
bool m_enabled;
wxButton* m_clickButton;
wxButton* m_enableButton;
DECLARE_EVENT_TABLE()
};
BEGIN_EVENT_TABLE(MyFrame,wxFrame)
EVT_MENU(ID_Quit,MyFrame::OnQuit)
EVT_BUTTON(ID_CLICK_ME,MyFrame::OnClickMe)
EVT_BUTTON(ID_DISABLE_ENABLE,MyFrame::OnDisableEnable)
EVT_CLOSE(MyFrame::OnClose)
END_EVENT_TABLE()
IMPLEMENT_APP(MyApp)
MyFrame::MyFrame(const wxString& title,const wxPoint& pos,const wxSize& size):
wxFrame((wxFrame*)NULL,-1,title,pos,size),m_enabled(true)
{
wxMenu* menu=new wxMenu;
menu->Append(ID_Quit,"E&xit");
wxMenuBar* menuBar=new wxMenuBar;
menuBar->Append(menu,"&File");
SetMenuBar(menuBar);
m_clickButton=new wxButton(this,ID_CLICK_ME,"&Click Me");
wxBoxSizer* topsizer=new wxBoxSizer(wxHORIZONTAL);
topsizer->Add(m_clickButton,0,wxTOP|wxALL,5);
m_enableButton=new wxButton(this,ID_DISABLE_ENABLE,"&Disable");
topsizer->Add(m_enableButton,0,wxTOP|wxALL,5);
wxBoxSizer* vertsizer=new wxBoxSizer(wxVERTICAL);
wxStaticText* text=new wxStaticText(this,-1,wxT("Test"),wxDefaultPosition,wxDefaultSize,wxALIGN_CENTRE_HORIZONTAL|wxEXPAND);
topsizer->Add(vertsizer,1,wxALIGN_CENTER|wxALL,10);
vertsizer->Add(text,1,wxALIGN_CENTER|wxALL,10);
CreateStatusBar();
SetSizer(topsizer);
//Connect(wxEVT_CLOSE_WINDOW, wxCloseEventHandler(MyFrame::OnClose));
}
void MyFrame::OnQuit(wxCommandEvent& event)
{
Close(true);
}
void MyFrame::OnClickMe(wxCommandEvent& WXUNUSED(event))
{
wxMessageBox(wxT("I've been clicked!"));
}
void MyFrame::OnDisableEnable(wxCommandEvent& WXUNUSED(event))
{
m_enabled=(m_enabled==true?false:true);
m_clickButton->Enable(m_enabled);
m_enableButton->SetLabel((m_enabled?"&Disable":"&Enable"));
}
void MyFrame::OnClose(wxCloseEvent& event)
{
wxMessageDialog *dial = new wxMessageDialog(NULL,
wxT("Are you sure to quit?"), wxT("Question"),
wxYES_NO | wxNO_DEFAULT | wxICON_QUESTION);
int ret = dial->ShowModal();
dial->Destroy();
if (ret == wxID_YES) {
event.Skip();//Destroy();
}
}
bool MyApp::OnInit()
{
//wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE, "program");
printf("This is a line of text from console.\n");
wxPrintf("Press any key to show a dialog..\n");
system("pause");
wxMessageBox(wxT("This is a window."));
wxPrintf("Press any key to continue...\n");
system("pause");
wxPrintf("This is a line of text from console using wxPrintf\n");
system("pause");
MyFrame* frame=new MyFrame(wxT("Test"),wxPoint(50,50),wxSize(600,340));
//SetTopWindow(frame);
//****And below matters a lot!!!
//SetExitOnFrameDelete(false);
frame->Show(true);
wxPrintf("Now application will quit.\n");
system("pause");
return true;
}