wxApp overview

Classes: wxApp

A wxWidgets application does not have a main procedure; the equivalent is the OnInit member defined for a class derived from wxApp. OnInit will usually create a top window as a bare minimum.

Unlike in earlier versions of wxWidgets, OnInit does not return a frame. Instead it returns a boolean value which indicates whether processing should continue (true) or not (false). You call wxApp::SetTopWindow to let wxWidgets know about the top window.

Note that the program's command line arguments, represented by argc and argv, are available from within wxApp member functions.

An application closes by destroying all windows. Because all frames must be destroyed for the application to exit, it is advisable to use parent frames wherever possible when creating new frames, so that deleting the top level frame will automatically delete child frames. The alternative is to explicitly delete child frames in the top-level frame's wxCloseEvent handler.

In emergencies the wxExit function can be called to kill the application however normally the application shuts down automatically, see below.

An example of defining an application follows:

class DerivedApp : public wxApp
  virtual bool OnInit();


bool DerivedApp::OnInit()
  wxFrame *the_frame = new wxFrame(NULL, ID_MYFRAME, argv[0]);

  return true;

Note the use of IMPLEMENT_APP(appClass), which allows wxWidgets to dynamically create an instance of the application object at the appropriate point in wxWidgets initialization. Previous versions of wxWidgets used to rely on the creation of a global application object, but this is no longer recommended, because required global initialization may not have been performed at application object construction time.

You can also use DECLARE_APP(appClass) in a header file to declare the wxGetApp function which returns a reference to the application object. Otherwise you can only use the global wxTheApp pointer which is of type wxApp *.

ymasuda 平成17年11月19日