I started to learn wxWidgets and read the cross-platform GUI programming.pdf ebook, which is very detailed. According to the above program, I have been typing for a long time and made several mistakes.
The Code is as follows.
/*************************************** ******************************
* Name: Main. cpp
* Purpose: Implements simple wxWidgets application with GUI.
* Author: hwh
* Created: 2011/11/24
* Copyright:
* License: wxWidgets license (www.wxwidgets.org)
*
* Notes:
**************************************** *****************************/
/*
# Include <wx/wx. h>
// Application class
Class wxminiapp: Public wxapp
{
Public:
// Function called at the application Initialization
Virtual bool oninit ();
// Event handler for button click
Void onclick (wxcommandevent & event) {gettopwindow ()-> close ();}
};
Implement_app (wxminiapp );
Bool wxminiapp: oninit ()
{
// Create a new frame and set it as the top most application window
Settopwindow (New wxframe (null,-1, wxt (""), wxdefaultposition, wxsize (100, 50 )));
// Create new button and assign it to the main frame
New wxbutton (gettopwindow (), wxid_exit, wxt ("Click! "));
// Connect button click event with event handler
Connect (wxid_exit, wxevt_command_button_clicked, wxcommandeventhandler (wxminiapp: onclick ));
// Show Main Frame
Gettopwindow ()-> show ();
// Enter the application's main loop
Return true;
}
*/
# Include "wx/wx. H"
Class MyApp: Public wxapp
{
Public:
Virtual bool oninit ();
};
Class myframe: Public wxframe
{
Public:
// Constructor of the main window class
Myframe (const wxstring & title );
// Event processing functions
Void onquit (wxcommandevent & event );
Void onabout (wxcommandevent & event );
PRIVATE:
// Declare the event table
Declare_event_table ()
};
// With this line, you can use MyApp & wxgetapp ().
Declare_app (MyApp)
// Tell the main application which class is used
Implement_app (MyApp)
Bool MyApp: oninit ()
{
// Create the Main Window
Myframe * frame = new myframe (wxt ("minimal wxWidgets app "));
// Display the Main Window
Frame-> show (true );
// Start the event processing cycle
Return true;
}
// Class event table myframe
Begin_event_table (myframe, wxframe)
Evt_menu (wxid_about, myframe: onabout)
Evt_menu (wxid_exit, myframe: onquit)
End_event_table ()
Void myframe: onabout (wxcommandevent & event)
{
Wxstring MSG;
MSG. printf (wxt ("hello and Welcom to % s"), wxversion_string );
Wxmessagebox (MSG, wxt ("about minimal"), wxok | wxicon_information, this );
}
Void myframe: onquit (wxcommandevent & event)
{
// Release the Main Window
Close ();
}
Myframe: myframe (const wxstring & title)
: Wxframe (null, wxid_any, title)
{
// Create a menu bar
Wxmenu * filemenu = new wxmenu;
Filemenu-> append (wxid_exit, wxt ("E & xit \ talt-X"), wxt ("Quiz this program "));
// Add the "about" menu
Wxmenu * helpmenu = new wxmenu;
Helpmenu-> append (wxid_about, wxt ("& about... \ TF1"), wxt ("show about dialog "));
// Add a menu item to the menu bar
Wxmenubar * menubar = new wxmenubar ();
Menubar-> append (filemenu, wxt ("& file "));
Menubar-> append (helpmenu, wxt ("& Help "));
// Then place the menu bar on the Main Window
Setmenubar (menubar );
// Create a status bar to make everything more interesting
Createstatusbar (2 );
Setstatustext (wxt ("Welcom to wxwidegets! "));
}