index.html
来自「《Big C++ 》Third Edition电子书和代码全集-Part1」· HTML 代码 · 共 620 行 · 第 1/2 页
HTML
620 行
<!-- -*-html-*-
$Source$
$Revision$
Big C++, chptr 27
editor: cols=80, tabstop=2
(c) 2005 John Wiley & Sons
Kurt Schmidt, kschmidt@cs.drexel.edu
NOTES
- 3 spaces are used for each indent in examples
REVISIONS:
- $Log$
-->
<html>
<head>
<title>Big C++: Chptr. 27 -- Graphical User Interfaces</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Copyright" content="2005 John Wiley & Sons">
<meta name="Author" content="Kurt Schmidt">
</head>
<body>
<hr><h2><font color='#009999' size='+3'>Chapter 27: Graphical User Interfaces
</font></h2>
<p align="center"><img src="images/ccc_4.png"></p>
<hr><h2><font color='#009999' size='+2'>Chapter Goals</font></h2>
<hr noshade size="4" color="#009999">
<ul>
<li><font size="+1">To learn about event driven programming</font></li>
<li><font size="+1">To learn how to use an application framework</font></li>
<li><font size="+1">To implement menus and buttons and their associated
actions</font></li>
<li><font size="+1">To understand the concept of layout management for
graphical
user interface components</font></li>
<li><font size="+1">To understand window repainting</font></li>
<li><font size="+1">To be able to implement simple applications with
graphical user interfaces</font></li>
</ul>
<hr><h2><font color='#009999'>27.1 The wxWindows Toolkit</font></h2>
<ul>
<li><font size="+1">Modern operating systems provide libraries for GUI
programming.</font></li>
<li><font size="+1">The most commonly used C++ GUI toolkit is the MFC
(Microsoft Foundation Classes), which is used to write Microsoft Windows
programs.</font></li>
<li><font size="+1">We will use a different toolkit, called wxWindows:</font>
<ul>
<li><font size="+1">wxWindows is freely available.</font></li>
<li><font size="+1">wxWindows runs on several platforms (including Linux
and Macintosh)</font></li>
<li><font size="+1">wxWindows works with a large number of
compilers</font></li>
<li><font size="+1">wxWindows is more transparent to the beginning
programmer.</font></li>
<li><font size="+1">wxWindows is structurally similar to MFC</font></li>
</ul>
</li>
<li><font size="+1">You can download the wxWindows software from
<tt>www.wxwindows.org</tt></font></li>
</ul>
<hr><h2><font color='#009999'>27.2 Frames</font></h2>
<ul>
<li><font size="+1">To get started with wxWindows, we will write a very simple
program that simply puts up a frame, a window with the typical decorations
that the windowing system provides.</font></li>
<li><font size="+1">The icons for minimizing or closing the window (etc.) vary
from system to system, but are not important to our discussion.</font></li>
<li><font size="+1">To use the wzWindows toolkit in your program, you need to
include the header file <tt>wx/w.h</tt>.</font></li>
<li><font size="+1">To the define a class that contains details about the workings
of your application.
<blockquote>
<pre>class BasicApp: public wxApp
{
public:
BasicApp();
virtual bool OnInit(); /* override base class implementation */
private:
wxFrame* frame; /* this application has a frame */
};</pre>
</blockquote></font></li>
</ul>
<hr><h2><font color='#009999'>27.2 Frames (cont.)</font></h2>
<ul>
<li><font size="+1">The frame is initialized in the constructor.
<blockquote>
<pre>BasicApp::BasicApp()
{
frame = new wxFrame(NULL, -1,
"My First GUI Program");
}</pre>
</blockquote></font></li>
<li><font size="+1">The parameters indicate the frame has no parent window,
a default window ID, and is called "My First GUI Program"</font></li>
<li><font size="+1">In the <tt>OnInit</tt> functions, you show the frame and
return true to indicate that the initialization was successful.
<blockquote>
<pre>bool BasicApp::OnInit()
{
frame->Show(true);
return true;
}</pre>
</blockquote></font></li>
</ul>
<hr><h2><font color='#009999'>27.2 Frames (cont.)</font></h2>
<ul>
<li><font size="+1">Note that the names in wxWindows (such as <tt>OnInit</tt>
and <tt>Show</tt>) do not follow the same style guidelines as the text.</font></li>
<li><font size="+1">Two preprocessor macros carry out some magic that the framework
needs to make an application out of the <tt>BasicApp</tt> class.
<blockquote>
<pre>DECLARE_APP(BasicAPP)
IMPLEMENT_APP(BasicAPP)</pre>
</blockquote>
</font></li>
<li><font size="+1">If you distribute the code into separate files </font>
<ul>
<li><font size="+1">place <tt>DECLARE_APP</tt> in the <tt>.h</tt> file</font></li>
<li><font size="+1">place <tt>IMPLEMENT_APP</tt> in the <tt>.cpp</tt> file</font></li>
</ul>
</li>
<li><font size="+1">The wxWindows toolkit supplies an <i>application</i> <i>framework</i>
- several base classes from which programmers derive classes to specify the
behavior of their application.</font></li>
<li><font size="+1">The application framework does complex work to interface
with the operating system, but the programmer is insulated from these technical
details. </font> </li>
<li><font size="+1">The instructions for compiling a wxWindows program differs
quite a bit depending on compiler and platform.</font></li>
</ul>
<hr><h2><font color='#009999'>27.2 Frames (<tt>basic.cpp</tt>)</font></h2>
<iframe src="code/basic.cpp.html" width="80%" height="80%">Your browser does
not support the <iframe> tag.</iframe>
<hr><h2><font color='#009999'>27.3 Adding a Text Control to the Frame</font></h2>
<ul>
<li><font size="+1">Again, we use inheritance to define our own, more interesting,
type of frame.</font></li>
<li><font size="+1">This frame contains a text area into which users can type
any text.</font><font size="+1">
<blockquote>
<pre>class TextFrame : public wxFrame
{
public:
TextFrame();
private:
wxTextCtrl* text;
};</pre>
</blockquote>
</font></li>
<li><font size="+1">The constructor initializes the base class and the text
control:
<blockquote>
<pre>TextFrame::TextFrame()
: wxFrame(NULL, -1, "TextFrame")
{
text = new wxTextCtrl(this, -1, "Type some text here!",
wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE);
}</pre>
</blockquote>
</font></li>
<li><font size="+1">Note that first parameter (<tt>this</tt>) indicates that
the parent of the text control is this frame.</font> </li>
<li><font size="+1">The next two parameters indicate a default window ID and
initial contents of the text control.</font></li>
<li><font size="+1">The next two parameters indicate default size and position.</font></li>
<li><font size="+1">The final parameter turns on the "multiline" style,
which allows the text control to hold multiple lines of text.</font></li>
</ul>
<hr><h2><font color='#009999'>27.3 Adding a Text Control to the Frame (cont.)</font></h2>
<ul>
<li><font size="+1">We have to make a slight change to the application class
to show a <tt>TextFrame</tt>, not a <tt>wxFrame</tt>.
<blockquote>
<pre>class TextApp : public wxApp
{
public:
TextApp();
virtual bool OnInit();
private:
<font color="#0000FF">TextFrame</font>* frame;
};
TextApp::TextApp()
{
frame = new <font color="#0000FF">TextFrame</font>();
}</pre>
</blockquote></font></li>
</ul>
<hr><h2><font color='#009999'>27.3 Adding a Text Control to the Frame (<tt>text.cpp</tt>)</font></h2>
<iframe src="code/text.cpp.html" width="80%" height="80%">Your browser does not
support the <iframe> tag.</iframe>
<hr><h2><font color='#009999'>27.4 Menus</font></h2>
<ul>
<li><font size="+1">In this step, we will add a menu to the sample application.</font></li>
<li><font size="+1">We will create an application with a <i>menu bar</i> that
contains the names of top-level menus.</font></li>
<li><font size="+1">There will be a single top-level menu named "Say"
that contains two <i>menu items</i>, "Hello" and "Goodbye".</font></li>
<blockquote><img src="images/menu.png"></blockquote>
</ul>
<hr><h2><font color='#009999'>27.4 Menus (cont.)</font></h2>
<ul>
<li><font size="+1">Each menu item must have an integer ID number, used to match
up menu items with their actions.
<blockquote>
<pre>const int ID_SAY_HELLO = 1000;
const int ID_SAY_GOODBYE = 1001;</pre>
</blockquote></font></li>
<li><font size="+1">Now construct a menu and append the two menu items to it.
<blockquote>
<pre>wxMenu* menu = new wxMenu();
menu->Append(ID_SAY_HELLO, "Hello");
menu->Append(ID_SAY_GOODBYE, "Goodbye");</pre>
</blockquote></font></li>
<li><font size="+1">Finally, constructor a menu bar object, set it as the menu
bar of the frame, and append the menu.
<blockquote>
<pre>wxMenuBar* menu_bar = new wxMenuBar();
SetMenuBar(menu_bar);
menu_bar->Append(menu, "Say);</pre>
</blockquote></font></li>
</ul>
<hr><h2><font color='#009999'>27.4 Menus (<tt>menu.cpp)</tt></font></h2>
<iframe src="code/menu.cpp.html" width="80%" height="80%">Your browser does not
support the <iframe> tag.</iframe>
<hr><h2><font color='#009999'>27.5 Event Handling</font></h2>
<ul>
<li><font size="+1">We will now attach actions to the menu items.</font></li>
<li><font size="+1">Define a function for each action.
<blockquote>
<pre>void EventFrame::OnSayHello(wxCommandEvent& event)
{
text->AppendText("Hello, World!\n");
} </pre>
</blockquote>
</font></li>
<li><font size="+1">We want this function to run whenever the program user selects
the menu option.</font></li>
<li><font size="+1">All event handler functions have a parameter that describes
the triggering event.</font></li>
<li><font size="+1">You must declare the function this way or get a bewildering
error message (note that the parameter is not used in the function).</font></li>
</ul>
<hr><h2><font color='#009999'>27.5 Event Handling (cont.)</font></h2>
<ul>
<li><font size="+1">We create an event table out of macros that automatically
produce correct code.</font></li>
<li><font size="+1">The generic form of the event table is:
<blockquote>
<pre>BEGIN_EVENT_TABLE(<i>ClassName</i>, <i>BaseClassName</i>)
EVT_TYPE(<i>parameters</i>, <i>function</i>)
. . .
END_EVENT_TABLE()</pre>
</blockquote>
</font></li>
<li><font size="+1">There are several different event types; here we use the
<tt>MENU</tt> event type that requires a menu ID as a parameter.
<blockquote>
<pre>BEGIN_EVENT_TABLE(EventFrame, wxFrame)
EVT_MENU(IS_SAY_HELLO, EVentFrame::OnSayHello)
END_EVENT_TABLE()</pre>
</blockquote>
</font></li>
<li><font size="+1">We need to insert another macro into the definition of each
class that has an event table.
<blockquote>
<pre>class EventFrame : public wxFrame
{
. . .
private:
wxTextCtrl* text;
DECLARE_EVENT_TABLE()
};</pre>
</blockquote></font>
</ul>
<hr><h2><font color='#009999'>27.5 Event Handling (<tt>event.cpp</tt>)</font></h2>
<iframe src="code/event.cpp.html" width="80%" height="80%">Your browser does not
support the <iframe> tag.</iframe>
<hr><h2><font color='#009999'>27.6 Layout Management</font></h2>
<ul>
<li><font size="+1">The next sample program uses buttons instead of menus.</font></li>
<li><font size="+1">Each button has an ID, and the event table matches the ID
with a function.
<blockquote>
<pre>BEGIN_EVENT_TABLE(ButtonFrame, wxFrame)
EVT_BUTTON(IS_SAY_HELLO, ButtonFrame::OnSayHello)
END_EVENT_TABLE()</pre>
</blockquote>
</font></li>
<li><font size="+1">When you construct a button, you specify the parent window,
the button ID, and the button label.
<blockquote>
<pre>wxButton* hello_button = new wxButton(this,
IS_SAY_HELLO, "Say Hello");</pre>
</blockquote>
</font> </li>
<li><font size="+1">The event handler has the same form as the menu event handler.
<blockquote>
<pre>void ButtonFrame::OnSayHello(wxCommandEvent& event)
{
text->AppendText("Hello, World!\n");
}</pre>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?