📄 ch03.htm
字号:
<BLOCKQUOTE>
<P>
<HR>
<B> </B><FONT COLOR="#000077"><B>Just a Minute:</B></FONT><B> </B>Like functions,
classes are an important part of the C++ programming language. In fact, one of the
earliest names for C++ was C with Classes.
<HR>
</BLOCKQUOTE>
<P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>An <I>instance</I> of a class,
sometimes called an <I>object</I>, is an occurrence of a class. An instance of one
of your classes can be used or manipulated inside your programs.</P>
<P>You normally use classes to model objects in your program. Member functions, described
in the next section, are used to control the state of an object, as well as to access
any data contained in it.</P>
<P>In programs written with MFC, classes are used to model different parts of the
application, such as the window frame, menus, buttons, and other controls. Member
functions are used to handle specific work that needs to be handled by the class.
<H3><FONT COLOR="#000077"><B>Classes Versus Instances</B></FONT></H3>
<P>Classes and instances of classes are not the same things--this can sometimes be
a confusing concept if you are new to C++ or object-oriented programming. Think of
a class as the description of an object; an instance of a class is a concrete occurrence
of that class.
<H3><FONT COLOR="#000077"><B>Constructors</B></FONT></H3>
<P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>A <I>constructor</I>, sometimes
called a "ctor," is a special member function that is created when an object
of the class is created.</P>
<P>A constructor always has the same name as the class and never has a return value,
not even <TT>void</TT>. The purpose of the constructor is to place a newly created
object into a known state. Typically, constructors can allocate system resources,
clear or set variables, or perform some other type of initialization.
<H3><FONT COLOR="#000077"><B>Destructors</B></FONT></H3>
<P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>A <I>destructor</I>, sometimes
called a "dtor," is a special member function that is called as an object
is destroyed. The destructor is declared as having no return type and is never declared
with a parameter list. The name of the destructor is the class name prefixed by a
tilde (~) character.</P>
<P>It is not necessary to define a destructor unless there are specific tasks that
must be performed to clean up after an object, such as releasing system resources
that might have been allocated.
<H2><FONT COLOR="#000077"><B>Using MFC for Windows Programming</B></FONT></H2>
<P>In the first hour, you created an MFC program using AppWizard. When you use AppWizard
to create a project, it might seem that you get a great deal of functionality for
free. However, a great deal of code is generated--even a simple program like HelloMFC
results in a large number of source files.</P>
<P>MFC doesn't need to be that complicated. In fact, you can write a very simple
MFC program that fits in a single source file and is about one page long.
<H3><FONT COLOR="#000077"><B>The HelloWin MFC Example</B></FONT></H3>
<P>Listing 3.3 is an example of a simple MFC program that displays a Hello World
message in the center of the client window, much like the HelloMFC program you created
in the first hour.
<H4><FONT COLOR="#000077">TYPE: Listing 3.3. A simple Windows program written using
C++ and MFC.</FONT></H4>
<PRE><FONT COLOR="#0066FF"><TT>#include <afxwin.h></TT>
<TT>// The CHelloApp class</TT>
<TT>class CHelloApp : public CWinApp</TT>
<TT>{</TT>
<TT> public:</TT>
<TT> BOOL InitInstance();</TT>
<TT>};</TT>
<TT>// The CHelloWnd class</TT>
<TT>class CHelloWnd : public CFrameWnd</TT>
<TT>{</TT>
<TT> public:</TT>
<TT> CHelloWnd();</TT>
<TT> protected:</TT>
<TT> afx_msg void OnPaint();</TT>
<TT> DECLARE_MESSAGE_MAP()</TT>
<TT>};</TT>
<TT>// InitInstance - Returns TRUE if initialization is successful.</TT>
<TT>BOOL CHelloApp::InitInstance()</TT>
<TT>{</TT>
<TT> m_pMainWnd = new CHelloWnd;</TT>
<TT> if( m_pMainWnd != 0 )</TT>
<TT> {</TT>
<TT> m_pMainWnd->ShowWindow( m_nCmdShow );</TT>
<TT> m_pMainWnd->UpdateWindow();</TT>
<TT> return TRUE;</TT>
<TT> }</TT>
<TT> else</TT>
<TT> return FALSE;</TT>
<TT>}</TT>
<TT>// Create a message map that handles one message - WM_PAINT</TT>
<TT>BEGIN_MESSAGE_MAP( CHelloWnd, CFrameWnd )</TT>
<TT> ON_WM_PAINT()</TT>
<TT>END_MESSAGE_MAP()</TT>
<TT>CHelloWnd::CHelloWnd()</TT>
<TT>{</TT>
<TT> Create( NULL, "Hello" );</TT>
<TT>}</TT>
<TT>// OnPaint - Handles the WM_PAINT message from Windows.</TT>
<TT>void CHelloWnd::OnPaint()</TT>
<TT>{</TT>
<TT> CPaintDC dc(this);</TT>
<TT> dc.TextOut(50, 50, "Hello World!", 12);</TT>
<TT>}</TT>
<TT>// Create a single instance of the application.</TT>
<TT>CHelloApp theApplication; </TT>
</FONT></PRE>
<P>The simple Windows program provided in Listing 3.3 might seem large, but it's
actually about half the size of a similar program written in C. Using the MFC class
library enables you to use a large amount of source code that has already been written
for you. There is a lot of strange-looking code in Listing 3.3, so don't try to understand
it all right now.
<H3><FONT COLOR="#000077"><B>Building the HelloWin Example</B></FONT></H3>
<P>To build the program, create an MFC Windows project named HelloWin. Begin by selecting
File | New from the Visual C++ main menu; select the Projects tab in the New dialog
box. Next, select Win32 Application as the project type. You must also specify a
name and location for your project, just as you did for the projects in the first
two hours.</P>
<P>After the project has been created, open a new C++ source file document and enter
the contents of Listing 3.3 exactly as they are shown. Save the file as <TT>HelloWin.cpp</TT>
and add it to the project. (If necessary, refer to Hour 1, "Introducing Visual
C++ 5," for specific instructions.)</P>
<P>Set the linking options for the project by selecting Project | Settings from the
main menu. On the tab marked General is an item labeled Microsoft Foundation Classes.
It will have the value <TT>Not Using MFC</TT>. Change the selection to <TT>Use MFC
in a Shared Dll</TT>. You can do this by clicking on the down arrow beside the Not
Using MFC selection. This opens a box where you can then make the appropriate selection.</P>
<P>Compile the HelloWin project by selecting Build | Build HelloWin.exe from the
main menu (or Press F7).</P>
<P>To start the HelloWin program, select Build | Start Debug | Go from the main menu
(or Press F5). Figure 3.3 shows an example of HelloWin running.</P>
<P><A NAME="03"></A><A HREF="03.htm"><B>Figure 3.3.</B></A> <I><BR>
The HelloWin program displaying its message in a window.</I>
<H2><FONT COLOR="#000077"><B>The Common Elements of a Windows Program</B></FONT></H2>
<P>Two elements are found in almost every Windows program; each of these elements
can be found in the HelloWin program that you just compiled and ran:
<UL>
<LI>Windows are used for visible parts of an application<BR>
<BR>
<LI>Messages are used to control the interaction between an application and the Windows
operating system
</UL>
<H3><FONT COLOR="#000077"><B>Windows Are Everywhere</B></FONT></H3>
<P>One of the fundamental concepts in Windows programming is that everything you
see is a window. Some examples of windows are
<UL>
<LI>Controls such as pushbuttons, list boxes, and text edit controls<BR>
<BR>
<LI>Dialog boxes and property pages<BR>
<BR>
<LI>Toolbars and menu bars<BR>
<BR>
<LI>The Windows 95 taskbar<BR>
<BR>
<LI>The DOS command box that is used for console-mode applications
</UL>
<P>All windows have a common set of operations that can be applied to them. They
are all re-sized, moved, enabled, disabled, hidden, and displayed in the same way.
<H3><FONT COLOR="#000077"><B>The Client and Non-Client Areas</B></FONT></H3>
<P>A window is divided into two main areas, as shown in Figure 3.4:
<UL>
<LI>The non-client area, which contains the border, menus, and caption area for the
window<BR>
<BR>
<LI>The client area, which is the area that is left over, also known as the "main"
part of the window
</UL>
<P><A NAME="04"></A><A HREF="04.htm"><B>Figure 3.4.</B></A> <I><BR>
Client and non-client areas of a window.</I></P>
<P>The non-client area of a window is normally maintained by Windows; your applications
will normally be concerned only with the client area.
<H3><FONT COLOR="#000077"><B>Messages and Functions</B></FONT></H3>
<P>When Windows needs to communicate with an application, it sends it a message.
A message is similar to a function call--in fact, the MFC library will route most
messages as function calls into your application. For example, in an AppWizard application,
the MFC library calls the <TT>OnDraw</TT> function whenever Windows sends a <TT>WM_PAINT</TT>
message.</P>
<P>When your application communicates with a window, it will usually send it a message.
To enable or disable a control, you must send the control a <TT>WM_ENABLE</TT> message.
When using C, this process is very tedious and error prone. MFC simplifies things
by providing functions that you can call and then handling the message sending for
you.
<H2><FONT COLOR="#000077"><B>What Are Statements and Expressions?</B></FONT></H2>
<P>Statements and expressions are the elements defined by the C++ language that are
converted into machine code by the compiler to build your C++ programs. Seems like
a textbook-type definition, doesn't it? In reality, though, it is very hard to define
exactly what they are. When talking about a building, we can say that it is made
of bricks, boards, and other things; we can define the brick or board very easily.
In the case of the C++ programming language, it is much more difficult. Here we are
dealing with abstract concepts. The difference between a statement and expression
is very subtle, as you will soon see. Although it appears to be confusing at first,
the language will become understandable with practice. Eventually the C++ language
will become as natural to you as your native language.</P>
<P>Just like the simple Hello programs, all C++ programs are made up of statements
and expressions. Expressions and statements range from the simple statements that
were shown in the Hello programs to very complex expressions that stretch across
several lines.
<H3><FONT COLOR="#000077"><B>Statements</B></FONT></H3>
<P>All statements end with semicolons. In fact, the simplest statement is called
the null statement, and it consists of only a single semicolon, as follows:</P>
<PRE><FONT COLOR="#0066FF"><TT>;</TT>
</FONT></PRE>
<P>The null statement isn't used often; it's used only in situations in which the
C++ syntax requires a statement, but no real work needs to be done.</P>
<P>You use a statement to tell the compiler to perform some type of specific action.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -