The wxApp class represents the application itself. It is used to:

You should use the macro IMPLEMENT_APP(appClass) in your application implementation file to tell wxWidgets how to create an instance of your application class.

Use DECLARE_APP(appClass) in a header file if you want the wxGetApp function (which returns a reference to your application object) to be visible to other files.

Derived from


Include files


See also

wxApp overview



Constructor. Called implicitly with a definition of a wxApp object.



Destructor. Will be called implicitly on program exit if the wxApp object is created on the stack.


int argc

Number of command line arguments (after environment-specific processing).


wxChar ** argv

Command line arguments (after environment-specific processing).


virtual wxLog* CreateLogTarget(void)

Creates a wxLog class for the application to use for logging errors. The default implementation returns a new wxLogGui class.

See also



virtual void Dispatch(void)

Dispatches the next event in the windowing system event queue.

This can be used for programming event loops, e.g.

  while (app.Pending())

See also



virtual void ExitMainLoop(void)

Call this to explicitly exit the main message (event) loop. You should normally exit the main loop (and the application) by deleting the top window.


int FilterEvent(wxEvent& event)

This function is called before processing any event and allows the application to preempt the processing of some events. If this method returns $-1$ the event is processed normally, otherwise either true or false should be returned and the event processing stops immediately considering that the event had been already processed (for the former return value) or that it is not going to be processed at all (for the latter one).


wxString GetAppName(void) const

Returns the application name.


wxWidgets sets this to a reasonable default before calling wxApp::OnInit, but the application can reset it at will.


wxString GetClassName(void) const

Gets the class name of the application. The class name may be used in a platform specific manner to refer to the application.

See also



bool GetExitOnFrameDelete(void) const

Returns true if the application will exit when the top-level window is deleted, false otherwise.

See also

wxApp shutdown overview


static wxAppConsole * GetInstance(void)

Returns the one and only global application object. Usually wxTheApp is usead instead.

See also



virtual wxWindow * GetTopWindow(void) const

Returns a pointer to the top window.


If the top window hasn't been set using wxApp::SetTopWindow, this function will find the first top-level window (frame or dialog) and return that.

See also



bool GetUseBestVisual(void) const

Returns true if the application will use the best visual on systems that support different visuals, false otherwise.

See also



wxString GetVendorName(void) const

Returns the application's vendor name.


bool IsActive(void) const

Returns TRUEif the application is active, i.e. if one of its windows is currently in the foreground. If this function returns FALSEand you need to attract users attention to the application, you may use wxTopLevelWindow::RequestUserAttention to do it.


static bool IsMainLoopRunning(void)

Returns TRUEif the main event loop is currently running, i.e. if the application is inside OnRun.

This can be useful to test whether the events can be dispatched. For example, if this function returns FALSE, non-blocking sockets cannot be used because the events from them would never be processed.


virtual int MainLoop(void)

Called by wxWidgets on creation of the application. Override this if you wish to provide your own (environment-dependent) main loop.

Return value

Returns 0 under X, and the wParam of the WM_QUIT message under Windows.


void OnAssert(const wxChar *file, int line, const wxChar *cond, const wxChar *msg)

This function is called when an assert failure occurs, i.e. the condition specified in wxASSERT macro evaluated to false. It is only called in debug mode (when __WXDEBUG__ is defined) as asserts are not left in the release code at all.

The base class version show the default assert failure dialog box proposing to the user to stop the program, continue or ignore all subsequent asserts.


the name of the source file where the assert occurred

the line number in this file where the assert occurred

the condition of the failed assert in string form

the message specified as argument to wxASSERT_MSG or wxFAIL_MSG, will be NULL if just wxASSERT or wxFAIL was used


bool OnCmdLineError(wxCmdLineParser& parser)

Called when command line parsing fails (i.e. an incorrect command line option was specified by the user). The default behaviour is to show the program usage text and abort the program.

Return true to continue normal execution or false to return false from OnInit thus terminating the program.

See also



bool OnCmdLineHelp(wxCmdLineParser& parser)

Called when the help option (-help) was specified on the command line. The default behaviour is to show the program usage text and abort the program.

Return true to continue normal execution or false to return false from OnInit thus terminating the program.

See also



bool OnCmdLineParsed(wxCmdLineParser& parser)

Called after the command line had been successfully parsed. You may override this method to test for the values of the various parameters which could be set from the command line.

Don't forget to call the base class version unless you want to suppress processing of the standard command line options.

Return true to continue normal execution or false to return false from OnInit thus terminating the program.

See also



virtual bool OnExceptionInMainLoop(void)

This function is called if an unhandled exception occurs inside the main application event loop. It can return TRUEto ignore the exception and to continue running the loop or FALSEto exit the loop and terminate the program. In the latter case it can also use C++ throw keyword to rethrow the current exception.

The default behaviour of this function is the latter in all ports except under Windows where a dialog is shown to the user which allows him to choose between the different options. You may override this function in your class to do something more appropriate.

Finally note that if the exception is rethrown from here, it can be caught in OnUnhandledException.


virtual int OnExit(void)

Override this member function for any processing which needs to be done as the application is about to exit. OnExit is called after destroying all application windows and controls, but before wxWidgets cleanup. Note that it is not called at all if OnInit failed.

The return value of this function is currently ignored, return the same value as returned by the base class method if you override it.


void OnFatalException(void)

This function may be called if something fatal happens: an unhandled exception under Win32 or a a fatal signal under Unix, for example. However, this will not happen by default: you have to explicitly call wxHandleFatalExceptions to enable this.

Generally speaking, this function should only show a message to the user and return. You may attempt to save unsaved data but this is not guaranteed to work and, in fact, probably won't.

See also



bool OnInit(void)

This must be provided by the application, and will usually create the application's main window, optionally calling wxApp::SetTopWindow. You may use OnExit to clean up anything initialized here, provided that the function returns TRUE.

Notice that if you want to to use the command line processing provided by wxWidgets you have to call the base class version in the derived class OnInit().

Return TRUEto continue processing, FALSEto exit the application immediately.


void OnInitCmdLine(wxCmdLineParser& parser)

Called from OnInit and may be used to initialize the parser with the command line options for this application. The base class versions adds support for a few standard options only.


virtual int OnRun(void)

This virtual function is where the execution of a program written in wxWidgets starts. The default implementation just enters the main loop and starts handling the events until it terminates, either because ExitMainLoop has been explicitly called or because the last frame has been deleted and GetExitOnFrameDelete flag is TRUE(this is the default).

The return value of this function becomes the exit code of the program, so it should return $0$ in case of successful termination.


virtual void OnUnhandledException(void)

This function is called when an unhandled C++ exception occurs inside OnRun() (the exceptions which occur during the program startup and shutdown might not be caught at all). Note that the exception type is lost by now, so if you want to really handle the exception you should override OnRun() and put a try/catch clause around the call to the base class version there.


bool ProcessMessage(WXMSG * msg)

Windows-only function for processing a message. This function is called from the main message loop, checking for windows that may wish to process it. The function returns true if the message was processed, false otherwise. If you use wxWidgets with another class library with its own message loop, you should make sure that this function is called to allow wxWidgets to receive messages. For example, to allow co-existence with the Microsoft Foundation Classes, override the PreTranslateMessage function:

// Provide wxWidgets message loop compatibility
BOOL CTheApp::PreTranslateMessage(MSG *msg)
  if (wxTheApp && wxTheApp->ProcessMessage((WXMSW *)msg))
    return true;
    return CWinApp::PreTranslateMessage(msg);


virtual bool Pending(void)

Returns true if unprocessed events are in the window system event queue.

See also



bool SendIdleEvents(wxWindow* win, wxIdleEvent& event)

Sends idle events to a window and its children.

Please note that this function is internal to wxWidgets and shouldn't be used by user code.


These functions poll the top-level windows, and their children, for idle event processing. If true is returned, more OnIdle processing is requested by one or more window.

See also



void SetAppName(const wxString& name)

Sets the name of the application. The name may be used in dialogs (for example by the document/view framework). A default name is set by wxWidgets.

See also



void SetClassName(const wxString& name)

Sets the class name of the application. This may be used in a platform specific manner to refer to the application.

See also



void SetExitOnFrameDelete(bool flag)

Allows the programmer to specify whether the application will exit when the top-level frame is deleted.


If true (the default), the application will exit when the top-level frame is deleted. If false, the application will continue to run.

See also

wxApp shutdown overview


static void SetInstance(wxAppConsole* app)

Allows external code to modify global wxTheApp, but you should really know what you're doing if you call it.


Replacement for the global application object.

See also



void SetTopWindow(wxWindow* window)

Sets the `top' window. You can call this from within wxApp::OnInit to let wxWidgets know which is the main window. You don't have to set the top window; it is only a convenience so that (for example) certain dialogs without parents can use a specific window as the top window. If no top window is specified by the application, wxWidgets just uses the first frame or dialog in its top-level window list, when it needs to use the top window.


The new top window.

See also

wxApp::GetTopWindow, wxApp::OnInit


void SetVendorName(const wxString& name)

Sets the name of application's vendor. The name will be used in registry access. A default name is set by wxWidgets.

See also



void SetUseBestVisual(bool flag)

Allows the programmer to specify whether the application will use the best visual on systems that support several visual on the same display. This is typically the case under Solaris and IRIX, where the default visual is only 8-bit whereas certain applications are supposed to run in TrueColour mode.

Note that this function has to be called in the constructor of the wxApp instance and won't have any effect when called later on.

This function currently only has effect under GTK.


If true, the app will use the best visual.


virtual void HandleEvent(wxEvtHandler *handler, wxEventFunction func, wxEvent& event) const

This function simply invokes the given method func of the specified event handler handler with the event as parameter. It exists solely to allow to catch the C++ exceptions which could be thrown by all event handlers in the application in one place: if you want to do this, override this function in your wxApp-derived class and add try/catch clause(s) to it.


bool Yield(bool onlyIfNeeded = false)

Yields control to pending messages in the windowing system. This can be useful, for example, when a time-consuming process writes to a text window. Without an occasional yield, the text window will not be updated properly, and on systems with cooperative multitasking, such as Windows 3.1 other processes will not respond.

Caution should be exercised, however, since yielding may allow the user to perform actions which are not compatible with the current task. Disabling menu items or whole menus during processing can avoid unwanted reentrance of code: see ::wxSafeYield for a better function.

Note that Yield() will not flush the message logs. This is intentional as calling Yield() is usually done to quickly update the screen and popping up a message box dialog may be undesirable. If you do wish to flush the log messages immediately (otherwise it will be done during the next idle loop iteration), call wxLog::FlushActive.

Calling Yield() recursively is normally an error and an assert failure is raised in debug build if such situation is detected. However if the onlyIfNeeded parameter is true, the method will just silently return false instead.

ymasuda 平成17年11月19日