]>
git.saurik.com Git - wxWidgets.git/blob - docs/doxygen/overviews/helloworld.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: topic overview
4 // Author: wxWidgets team
5 // Licence: wxWindows licence
6 /////////////////////////////////////////////////////////////////////////////
10 @page overview_helloworld Hello World Example
14 This page shows a very simple wxWidgets program that can be used as a skeleton
15 for your own code. While it does nothing very useful, it introduces a couple of
16 important concepts and explains how to write a working wxWidgets application.
18 First, you have to include wxWidgets' header files, of course. This can be done
19 on a file by file basis (such as @c wx/window.h) or using one global include
20 (@c wx/wx.h) which includes most of the commonly needed headers (although not
21 all of them as there are simply too many wxWidgets headers to pull in all of
22 them). For the platforms with support for precompiled headers, as indicated by
23 @c WX_PRECOMP, this global header is already included by @c wx/wxprec.h so we
24 only include it for the other ones:
27 // wxWidgets "Hello world" Program
29 // For compilers that support precompilation, includes "wx/wx.h".
30 #include <wx/wxprec.h>
37 Practically every app should define a new class derived from wxApp. By
38 overriding wxApp's OnInit() virtual method the program can be initialized, e.g.
39 by creating a new main window.
42 class MyApp: public wxApp
45 virtual bool OnInit();
49 The main window is created by deriving a class from wxFrame and giving it a
50 menu and a status bar in its constructor. Also, any class that wishes to
51 respond to any "event" (such as mouse clicks or messages from the menu or a
52 button) must declare an event table using the macro below.
54 Finally, the way to react to such events must be done in "handlers". In our
55 sample, we react to three menu items, one for our custom menu command and two
56 for the standard "Exit" and "About" commands (any program should normally
57 implement the latter two). Notice that these handlers don't need to be neither
61 class MyFrame: public wxFrame
64 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
67 void OnHello(wxCommandEvent& event);
68 void OnExit(wxCommandEvent& event);
69 void OnAbout(wxCommandEvent& event);
71 wxDECLARE_EVENT_TABLE();
75 In order to be able to react to a menu command, it must be given a unique
76 identifier which can be defined as a const variable or an enum element. The
77 latter is often used because typically many such constants will be needed:
86 Notice that you don't need to define identifiers for the "About" and "Exit". We
87 then proceed to actually implement an event table in which the events are
88 routed to their respective handler functions in the class MyFrame.
90 There are predefined macros for routing all common events, ranging from the
91 selection of a list box entry to a resize event when a user resizes a window on
92 the screen. If @c wxID_ANY is given as the ID, the given handler will be
93 invoked for any event of the specified type, so that you could add just one
94 entry in the event table for all menu commands or all button commands etc.
96 The origin of the event can still be distinguished in the event handler as the
97 (only) parameter in an event handler is a reference to a wxEvent object, which
98 holds various information about the event (such as the ID of and a pointer to
99 the class, which emitted the event).
102 wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
103 EVT_MENU(ID_Hello, MyFrame::OnHello)
104 EVT_MENU(wxID_EXIT, MyFrame::OnExit)
105 EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
109 As in all programs there must be a "main" function. Under wxWidgets main is
110 implemented using this macro, which creates an application instance and starts
114 wxIMPLEMENT_APP(MyApp)
117 As mentioned above, wxApp::OnInit() is called upon startup and should be used
118 to initialize the program, maybe showing a "splash screen" and creating the
119 main window (or several). The frame should get a title bar text ("Hello World")
120 and a position and start-up size. One frame can also be declared to be the top
121 window. Returning @true indicates a successful initialization.
126 MyFrame *frame = new MyFrame( "Hello World", wxPoint(50, 50), wxSize(450, 340) );
132 In the constructor of the main window (or later on) we create a menu with our
133 menu items as well as a status bar to be shown at the bottom of the main
134 window. Both have to be associated with the frame with respective calls.
137 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
138 : wxFrame(NULL, wxID_ANY, title, pos, size)
140 wxMenu *menuFile = new wxMenu;
141 menuFile->Append(ID_Hello, "&Hello...\tCtrl-H",
142 "Help string shown in status bar for this menu item");
143 menuFile->AppendSeparator();
144 menuFile->Append(wxID_EXIT);
146 wxMenu *menuHelp = new wxMenu;
147 menuHelp->Append(wxID_ABOUT);
149 wxMenuBar *menuBar = new wxMenuBar;
150 menuBar->Append( menuFile, "&File" );
151 menuBar->Append( menuHelp, "&Help" );
153 SetMenuBar( menuBar );
156 SetStatusText( "Welcome to wxWidgets!" );
160 Notice that we don't need to specify the labels for the standard menu items
161 @c wxID_ABOUT and @c wxID_EXIT, they will be given standard (even correctly
162 translated) labels and also standard accelerators correct for the current
163 platform making your program behaviour more native. For this reason you should
164 prefer reusing the standard ids (see @ref page_stockitems) if possible.
166 Here are the standard event handlers implementations. MyFrame::OnExit() closes
167 the main window by calling Close(). The parameter @true indicates that other
168 windows have no veto power such as after asking "Do you really want to close?".
169 If there is no other main window left, the application will quit.
172 void MyFrame::OnExit(wxCommandEvent& event)
178 MyFrame::OnAbout() will display a small window with some text in it. In this
179 case a typical "About" window with information about the program.
182 void MyFrame::OnAbout(wxCommandEvent& event)
184 wxMessageBox( "This is a wxWidgets' Hello world sample",
185 "About Hello World", wxOK | wxICON_INFORMATION );
189 The implementation of custom menu command handler may perform whatever task
190 your program needs to do, in this case we will simply show a message from it as
191 befits a hello world example:
194 void MyFrame::OnHello(wxCommandEvent& event)
196 wxLogMessage("Hello world from wxWidgets!");
200 Here is the entire program that can be copied and pasted:
203 // wxWidgets "Hello world" Program
205 // For compilers that support precompilation, includes "wx/wx.h".
206 #include <wx/wxprec.h>
212 class MyApp: public wxApp
215 virtual bool OnInit();
218 class MyFrame: public wxFrame
221 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
224 void OnHello(wxCommandEvent& event);
225 void OnExit(wxCommandEvent& event);
226 void OnAbout(wxCommandEvent& event);
228 wxDECLARE_EVENT_TABLE();
236 wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
237 EVT_MENU(ID_Hello, MyFrame::OnHello)
238 EVT_MENU(wxID_EXIT, MyFrame::OnExit)
239 EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
242 wxIMPLEMENT_APP(MyApp);
246 MyFrame *frame = new MyFrame( "Hello World", wxPoint(50, 50), wxSize(450, 340) );
251 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
252 : wxFrame(NULL, wxID_ANY, title, pos, size)
254 wxMenu *menuFile = new wxMenu;
255 menuFile->Append(ID_Hello, "&Hello...\tCtrl-H",
256 "Help string shown in status bar for this menu item");
257 menuFile->AppendSeparator();
258 menuFile->Append(wxID_EXIT);
260 wxMenu *menuHelp = new wxMenu;
261 menuHelp->Append(wxID_ABOUT);
263 wxMenuBar *menuBar = new wxMenuBar;
264 menuBar->Append( menuFile, "&File" );
265 menuBar->Append( menuHelp, "&Help" );
267 SetMenuBar( menuBar );
270 SetStatusText( "Welcome to wxWidgets!" );
273 void MyFrame::OnExit(wxCommandEvent& event)
278 void MyFrame::OnAbout(wxCommandEvent& event)
280 wxMessageBox( "This is a wxWidgets' Hello world sample",
281 "About Hello World", wxOK | wxICON_INFORMATION );
284 void MyFrame::OnHello(wxCommandEvent& event)
286 wxLogMessage("Hello world from wxWidgets!");