]>
git.saurik.com Git - wxWidgets.git/blob - docs/doxygen/overviews/helloworld.h
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: topic overview
4 // Author: wxWidgets team
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
11 @page overview_helloworld Hello World Example
15 This page shows a very simple wxWidgets program that can be used as a skeleton
16 for your own code. While it does nothing very useful, it introduces a couple of
17 important concepts and explains how to write a working wxWidgets application.
19 First, you have to include wxWidgets' header files, of course. This can be done
20 on a file by file basis (such as @c wx/window.h) or using one global include
21 (@c wx/wx.h) which includes most of the commonly needed headers (although not
22 all of them as there are simply too many wxWidgets headers to pull in all of
23 them). For the platforms with support for precompiled headers, as indicated by
24 @c WX_PRECOMP, this global header is already included by @c wx/wxprec.h so we
25 only include it for the other ones:
28 // wxWidgets "Hello world" Program
30 // For compilers that support precompilation, includes "wx/wx.h".
31 #include <wx/wxprec.h>
38 Practically every app should define a new class derived from wxApp. By
39 overriding wxApp's OnInit() virtual method the program can be initialized, e.g.
40 by creating a new main window.
43 class MyApp: public wxApp
46 virtual bool OnInit();
50 The main window is created by deriving a class from wxFrame and giving it a
51 menu and a status bar in its constructor. Also, any class that wishes to
52 respond to any "event" (such as mouse clicks or messages from the menu or a
53 button) must declare an event table using the macro below.
55 Finally, the way to react to such events must be done in "handlers". In our
56 sample, we react to three menu items, one for our custom menu command and two
57 for the standard "Exit" and "About" commands (any program should normally
58 implement the latter two). Notice that these handlers don't need to be neither
62 class MyFrame: public wxFrame
65 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
68 void OnHello(wxCommandEvent& event);
69 void OnExit(wxCommandEvent& event);
70 void OnAbout(wxCommandEvent& event);
72 wxDECLARE_EVENT_TABLE();
76 In order to be able to react to a menu command, it must be given a unique
77 identifier which can be defined as a const variable or an enum element. The
78 latter is often used because typically many such constants will be needed:
87 Notice that you don't need to define identifiers for the "About" and "Exit". We
88 then proceed to actually implement an event table in which the events are
89 routed to their respective handler functions in the class MyFrame.
91 There are predefined macros for routing all common events, ranging from the
92 selection of a list box entry to a resize event when a user resizes a window on
93 the screen. If @c wxID_ANY is given as the ID, the given handler will be
94 invoked for any event of the specified type, so that you could add just one
95 entry in the event table for all menu commands or all button commands etc.
97 The origin of the event can still be distinguished in the event handler as the
98 (only) parameter in an event handler is a reference to a wxEvent object, which
99 holds various information about the event (such as the ID of and a pointer to
100 the class, which emitted the event).
103 wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
104 EVT_MENU(ID_Hello, MyFrame::OnHello)
105 EVT_MENU(wxID_EXIT, MyFrame::OnExit)
106 EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
110 As in all programs there must be a "main" function. Under wxWidgets main is
111 implemented using this macro, which creates an application instance and starts
115 wxIMPLEMENT_APP(MyApp)
118 As mentioned above, wxApp::OnInit() is called upon startup and should be used
119 to initialize the program, maybe showing a "splash screen" and creating the
120 main window (or several). The frame should get a title bar text ("Hello World")
121 and a position and start-up size. One frame can also be declared to be the top
122 window. Returning @true indicates a successful initialization.
127 MyFrame *frame = new MyFrame( "Hello World", wxPoint(50, 50), wxSize(450, 340) );
133 In the constructor of the main window (or later on) we create a menu with our
134 menu items as well as a status bar to be shown at the bottom of the main
135 window. Both have to be associated with the frame with respective calls.
138 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
139 : wxFrame(NULL, wxID_ANY, title, pos, size)
141 wxMenu *menuFile = new wxMenu;
142 menuFile->Append(ID_Hello, "&Hello...\tCtrl-H",
143 "Help string shown in status bar for this menu item");
144 menuFile->AppendSeparator();
145 menuFile->Append(wxID_EXIT);
147 wxMenu *menuHelp = new wxMenu;
148 menuHelp->Append(wxID_ABOUT);
150 wxMenuBar *menuBar = new wxMenuBar;
151 menuBar->Append( menuFile, "&File" );
152 menuBar->Append( menuHelp, "&Help" );
154 SetMenuBar( menuBar );
157 SetStatusText( "Welcome to wxWidgets!" );
161 Notice that we don't need to specify the labels for the standard menu items
162 @c wxID_ABOUT and @c wxID_EXIT, they will be given standard (even correctly
163 translated) labels and also standard accelerators correct for the current
164 platform making your program behaviour more native. For this reason you should
165 prefer reusing the standard ids (see @ref page_stockitems) if possible.
167 Here are the standard event handlers implementations. MyFrame::OnExit() closes
168 the main window by calling Close(). The parameter @true indicates that other
169 windows have no veto power such as after asking "Do you really want to close?".
170 If there is no other main window left, the application will quit.
173 void MyFrame::OnExit(wxCommandEvent& event)
179 MyFrame::OnAbout() will display a small window with some text in it. In this
180 case a typical "About" window with information about the program.
183 void MyFrame::OnAbout(wxCommandEvent& event)
185 wxMessageBox( "This is a wxWidgets' Hello world sample",
186 "About Hello World", wxOK | wxICON_INFORMATION );
190 The implementation of custom menu command handler may perform whatever task
191 your program needs to do, in this case we will simply show a message from it as
192 befits a hello world example:
195 void MyFrame::OnHello(wxCommandEvent& event)
197 wxLogMessage("Hello world from wxWidgets!");
201 Here is the entire program that can be copied and pasted:
204 // wxWidgets "Hello world" Program
206 // For compilers that support precompilation, includes "wx/wx.h".
207 #include <wx/wxprec.h>
213 class MyApp: public wxApp
216 virtual bool OnInit();
219 class MyFrame: public wxFrame
222 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
225 void OnHello(wxCommandEvent& event);
226 void OnExit(wxCommandEvent& event);
227 void OnAbout(wxCommandEvent& event);
229 wxDECLARE_EVENT_TABLE();
237 wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
238 EVT_MENU(ID_Hello, MyFrame::OnHello)
239 EVT_MENU(wxID_EXIT, MyFrame::OnExit)
240 EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
243 wxIMPLEMENT_APP(MyApp);
247 MyFrame *frame = new MyFrame( "Hello World", wxPoint(50, 50), wxSize(450, 340) );
252 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
253 : wxFrame(NULL, wxID_ANY, title, pos, size)
255 wxMenu *menuFile = new wxMenu;
256 menuFile->Append(ID_Hello, "&Hello...\tCtrl-H",
257 "Help string shown in status bar for this menu item");
258 menuFile->AppendSeparator();
259 menuFile->Append(wxID_EXIT);
261 wxMenu *menuHelp = new wxMenu;
262 menuHelp->Append(wxID_ABOUT);
264 wxMenuBar *menuBar = new wxMenuBar;
265 menuBar->Append( menuFile, "&File" );
266 menuBar->Append( menuHelp, "&Help" );
268 SetMenuBar( menuBar );
271 SetStatusText( "Welcome to wxWidgets!" );
274 void MyFrame::OnExit(wxCommandEvent& event)
279 void MyFrame::OnAbout(wxCommandEvent& event)
281 wxMessageBox( "This is a wxWidgets' Hello world sample",
282 "About Hello World", wxOK | wxICON_INFORMATION );
285 void MyFrame::OnHello(wxCommandEvent& event)
287 wxLogMessage("Hello world from wxWidgets!");