Steps to add menu:
Here is the complete source:
#include <wx/wx.h>
#include <iostream>
using namespace std;
enum
{
ID_QUIT,
ID_ABOUT,
ID_POPUP
};
// Create our Application class derived from wxApp
class MyApp: public wxApp
{
public:
bool OnInit(); // have to override onInit
};
class MainWindow: public wxFrame
{
public:
MainWindow(const wxString& title , const wxPoint& pos , const wxSize& size );
void MenuClickQuit( wxCommandEvent& event);
void MenuClickAbout( wxCommandEvent& event);
void MenuClickPopup( wxCommandEvent& event);
DECLARE_EVENT_TABLE()
};
class FormData : public wxFrame
{
public:
FormData( wxWindow* Parent,const wxString& Title , const wxPoint& Pos , const wxSize& size);
};
MainWindow::MainWindow(const wxString& title , const wxPoint& pos , const wxSize& size )
: wxFrame( NULL, -1, title, pos, size)
{
this->Show( TRUE);
wxMenu *MenuFile = new wxMenu;
MenuFile->Append( ID_ABOUT , _T("&About"));
MenuFile->Append( ID_QUIT , _T("&Quit"));
wxMenu *MenuData = new wxMenu;
MenuData->Append( ID_POPUP , _T( "Another &Form"));
wxMenuBar *MenuBar = new wxMenuBar;
MenuBar->Append( MenuFile , _T("&File") );
MenuBar->Append( MenuData , _T("&Data") );
this->SetMenuBar( MenuBar);
// status bar
this->CreateStatusBar();
this->SetStatusText( _T("Bambang is learning wxWidgets") );
};
bool MyApp::OnInit()
{
cout << "start\n";
MainWindow *mainFrame = new MainWindow( _T("Hello empty world"),wxPoint(-1,-1),wxSize(-1,-1));
this->SetTopWindow( mainFrame);
//mainFrame->Show( TRUE);
return TRUE;
};
void MainWindow::MenuClickQuit( wxCommandEvent& event)
{
cout << "Quit\n";
this->Close( TRUE );
}
void MainWindow::MenuClickAbout( wxCommandEvent& event)
{
wxMessageBox( _T("Learning wxWidgets") , _T( "About" ), wxOK | wxCANCEL | wxICON_INFORMATION , this );
}
void MainWindow::MenuClickPopup( wxCommandEvent& event)
{
FormData *f = new FormData( this , _T("FormData"), wxPoint(-1,-1) , wxSize(-1,-1) );
f->Show( TRUE);
}
FormData::FormData( wxWindow* Parent,const wxString& Title , const wxPoint& Pos , const wxSize& Size)
: wxFrame( Parent , -1, Title , Pos, Size)
{
};
IMPLEMENT_APP(MyApp);
BEGIN_EVENT_TABLE( MainWindow , wxFrame)
EVT_MENU( ID_QUIT , MainWindow::MenuClickQuit)
EVT_MENU( ID_ABOUT , MainWindow::MenuClickAbout )
EVT_MENU( ID_POPUP , MainWindow::MenuClickPopup )
END_EVENT_TABLE()
- Define IDs for our menu items
enum
{
ID_QUIT,
ID_ABOUT,
ID_POPUP
}; - Create instances of wxMenu inside the constructor of the main windows
wxMenu *MenuFile = new wxMenu;
MenuFile->Append( ID_ABOUT , _T("&About"));
MenuFile->Append( ID_QUIT , _T("&Quit"));
wxMenu *MenuData = new wxMenu;
MenuData->Append( ID_POPUP , _T( "Another &Form")); - Create an instance of wxMenuBar
wxMenuBar *MenuBar = new wxMenuBar; - Add our instances of wxMenus to the menubar
MenuBar->Append( MenuFile , _T("&File") );
MenuBar->Append( MenuData , _T("&Data") ); - Assign the menubar to the main windows
this->SetMenuBar( MenuBar); - Declare event function in our main windows class declaration
class MainWindow: public wxFrame
{
public:
MainWindow(const wxString& title , const wxPoint& pos , const wxSize& size );
void MenuClickQuit( wxCommandEvent& event);
void MenuClickAbout( wxCommandEvent& event);
void MenuClickPopup( wxCommandEvent& event);
DECLARE_EVENT_TABLE()
};
Don't forget to put the magic macro: DECLARE_EVENT_TABLE() - Write implementation of the event functions.
void MainWindow::MenuClickQuit( wxCommandEvent& event)
{
cout << "Quit\n"; this->Close( TRUE );
}
void MainWindow::MenuClickAbout( wxCommandEvent& event)
{
wxMessageBox( _T("Learning wxWidgets") , _T( "About" ), wxOK | wxCANCEL | wxICON_INFORMATION , this );
}
void MainWindow::MenuClickPopup( wxCommandEvent& event)
{
FormData *f = new FormData( this , _T("FormData"), wxPoint(-1,-1) , wxSize(-1,-1) );
f->Show( TRUE);
}
...and again a magic macro which glues our event to the system:
BEGIN_EVENT_TABLE( MainWindow , wxFrame)
EVT_MENU( ID_QUIT , MainWindow::MenuClickQuit)
EVT_MENU( ID_ABOUT , MainWindow::MenuClickAbout )
EVT_MENU( ID_POPUP , MainWindow::MenuClickPopup )
END_EVENT_TABLE()
Here is the complete source:
#include <wx/wx.h>
#include <iostream>
using namespace std;
enum
{
ID_QUIT,
ID_ABOUT,
ID_POPUP
};
// Create our Application class derived from wxApp
class MyApp: public wxApp
{
public:
bool OnInit(); // have to override onInit
};
class MainWindow: public wxFrame
{
public:
MainWindow(const wxString& title , const wxPoint& pos , const wxSize& size );
void MenuClickQuit( wxCommandEvent& event);
void MenuClickAbout( wxCommandEvent& event);
void MenuClickPopup( wxCommandEvent& event);
DECLARE_EVENT_TABLE()
};
class FormData : public wxFrame
{
public:
FormData( wxWindow* Parent,const wxString& Title , const wxPoint& Pos , const wxSize& size);
};
MainWindow::MainWindow(const wxString& title , const wxPoint& pos , const wxSize& size )
: wxFrame( NULL, -1, title, pos, size)
{
this->Show( TRUE);
wxMenu *MenuFile = new wxMenu;
MenuFile->Append( ID_ABOUT , _T("&About"));
MenuFile->Append( ID_QUIT , _T("&Quit"));
wxMenu *MenuData = new wxMenu;
MenuData->Append( ID_POPUP , _T( "Another &Form"));
wxMenuBar *MenuBar = new wxMenuBar;
MenuBar->Append( MenuFile , _T("&File") );
MenuBar->Append( MenuData , _T("&Data") );
this->SetMenuBar( MenuBar);
// status bar
this->CreateStatusBar();
this->SetStatusText( _T("Bambang is learning wxWidgets") );
};
bool MyApp::OnInit()
{
cout << "start\n";
MainWindow *mainFrame = new MainWindow( _T("Hello empty world"),wxPoint(-1,-1),wxSize(-1,-1));
this->SetTopWindow( mainFrame);
//mainFrame->Show( TRUE);
return TRUE;
};
void MainWindow::MenuClickQuit( wxCommandEvent& event)
{
cout << "Quit\n";
this->Close( TRUE );
}
void MainWindow::MenuClickAbout( wxCommandEvent& event)
{
wxMessageBox( _T("Learning wxWidgets") , _T( "About" ), wxOK | wxCANCEL | wxICON_INFORMATION , this );
}
void MainWindow::MenuClickPopup( wxCommandEvent& event)
{
FormData *f = new FormData( this , _T("FormData"), wxPoint(-1,-1) , wxSize(-1,-1) );
f->Show( TRUE);
}
FormData::FormData( wxWindow* Parent,const wxString& Title , const wxPoint& Pos , const wxSize& Size)
: wxFrame( Parent , -1, Title , Pos, Size)
{
};
IMPLEMENT_APP(MyApp);
BEGIN_EVENT_TABLE( MainWindow , wxFrame)
EVT_MENU( ID_QUIT , MainWindow::MenuClickQuit)
EVT_MENU( ID_ABOUT , MainWindow::MenuClickAbout )
EVT_MENU( ID_POPUP , MainWindow::MenuClickPopup )
END_EVENT_TABLE()
Previewnya om... biar ada semangat ikut mencobaaaa
ReplyDeleteMaksudnya, screenshotnya? ini masih windows kosong dengan menu bar.... masih belum menarik
ReplyDelete