⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch08.htm

📁 24小时精通VC
💻 HTM
📖 第 1 页 / 共 4 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>

<HEAD>
	
	<TITLE>Teach Yourself Visual C++&#174; 5 in 24 Hours -- Hour 8 -- Messages and Event-Driven Programming</TITLE>
</HEAD>

<BODY TEXT="#000000" BGCOLOR="#FFFFFF">

<CENTER>
<H1><IMG SRC="sams.gif" tppabs="http://www.mcp.com/824169600/0-672/0-672-31242-5/button/sams.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM" BORDER="0"><BR>
<FONT COLOR="#000077">Teach Yourself Visual C++&#174; 5 in 24 Hours</FONT></H1>
</CENTER>
<CENTER>
<P><A HREF="ch07.htm" tppabs="http://www.mcp.com/824169600/0-672/0-672-31242-5/ch07/ch07.htm"><IMG SRC="previous.gif" tppabs="http://www.mcp.com/824169600/0-672/0-672-31242-5/button/previous.gif" WIDTH="128" HEIGHT="28"
ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="ch09.htm" tppabs="http://www.mcp.com/824169600/0-672/0-672-31242-5/ch09/ch09.htm"><IMG
SRC="next.gif" tppabs="http://www.mcp.com/824169600/0-672/0-672-31242-5/button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"
BORDER="0"></A><A HREF="index-1.htm" tppabs="http://www.mcp.com/824169600/0-672/0-672-31242-5/index.htm"><IMG SRC="contents.gif" tppabs="http://www.mcp.com/824169600/0-672/0-672-31242-5/button/contents.gif" WIDTH="128"
HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A> 
<HR>

</CENTER>
<CENTER>
<H1><FONT COLOR="#000077">- Hour 8 -<BR>
Messages and Event-Driven Programming</FONT></H1>
</CENTER>
<P>Messages are at the heart of every Windows program. Even the 50-line MFC program
in Hour 2, &quot;Writing Simple C++ Programs,&quot; had to handle the <TT>WM_PAINT</TT>
message. A good understanding of how the Windows operating system sends messages
will be a great help to you as you write your own programs.</P>
<P>In this hour, you will learn

<UL>
	<LI>How Windows applications use messages to communicate with the operating system
	and window objects in the application<BR>
	<BR>
	
	<LI>How messages are managed using the MFC framework<BR>
	<BR>
	
	<LI>Some basic information about the Document/View architecture and how AppWizard
	is used to create Document/View applications<BR>
	<BR>
	
	<LI>MFC base classes that are used in every MFC application
</UL>

<P>In this hour, you will also create a small sample program to learn how messages
are passed to applications by the Windows operating system.
<H2><FONT COLOR="#000077"><B>Understanding the Windows Programming Model</B></FONT></H2>
<P>Programs written for Windows differ from most console-mode programs. The console-mode
programs that you have seen in this book have consisted of short listings that created
small sequential programs that assumed complete control over a console-mode window.</P>
<P>Although sequential programs work well for explaining simple concepts like the
basics of the C++ language, they don't work well in a multitasking environment like
Microsoft Windows. In the Windows environment everything is shared: the screen, the
keyboard, the mouse--even the user. Programs written for Windows must cooperate with
Windows and with other programs that might be running at the same time.</P>
<P>In a cooperative environment like Windows, messages are sent to a program when
an event that affects the program occurs. Every message sent to a program has a specific
purpose. For example, messages are sent when a program should be initialized, when
menu selections are made, and when a window should be redrawn. Responding to event
messages is a key part of most Windows programs.</P>
<P>Another characteristic of Windows programs is that they must share resources.
Many resources must be requested from the operating system before they are used and,
after they are used, must be returned to the operating system so that they can be
used by other programs. This is one way Windows controls access to resources like
the screen and other physical devices.</P>
<P>In short, a program that runs in a window must be a good citizen. It cannot assume
that it has complete control over the computer on which it is running; it must ask
permission before taking control of any central resource, and it must be ready to
react to events that are sent to it.
<H2><FONT COLOR="#000077"><B>Using AppWizard with Document/View</B></FONT></H2>
<P>As you saw in the first few hours, the Developer Studio includes a tool, AppWizard
(also called MFC AppWizard), which is used to create a skeleton application. AppWizard
asks you a series of questions about your program; then it generates a project and
much of the source code for you, letting you concentrate on the code that makes your
program unique.</P>
<P>So far you have used AppWizard to create programs that use a dialog box as their
main window. However, the real strength of AppWizard is its capability to help you
create full Windows applications. Much of the code that is used as a starting point
for Windows programs is generic &quot;skeleton&quot; code; AppWizard will generate
this code for you.


<BLOCKQUOTE>
	<P>
<HR>
<B> </B><FONT COLOR="#000077"><B>Just a Minute:</B></FONT><B> </B>The MFC class library
	is built around the Document/View programming model. Using AppWizard to create the
	skeleton code for your program is the quickest way to get started writing Document/View
	programs. 
<HR>


</BLOCKQUOTE>

<H3><FONT COLOR="#000077"><B>A Quick Overview of the Document/View Architecture</B></FONT></H3>
<P>The AppWizard uses MFC classes to create applications that are based on the MFC
Document/View architecture. The basic idea behind Document/View is to separate the
data-handling classes from the classes that handle the user interface.</P>
<P>Separating the data from the user interface enables each class to concentrate
on performing one job, with a set of interfaces defined for interaction with the
other classes involved in the program. A view class is responsible for providing
a &quot;viewport&quot; through which you can see and manage the document. A document
class is responsible for controlling all the data, including storing it when necessary.
Figure 8.1 shows how the document and view classes interact with each other.</P>
<P><A NAME="01"></A><A HREF="01.htm" tppabs="http://www.mcp.com/824169600/0-672/0-672-31242-5/ch08/01.htm"><B>Figure 8.1.</B></A> <I><BR>
The Document/View architecture.</I></P>
<P>You will learn more about the Document/View architecture in Hour 9, &quot;The
Document/View Architecture.&quot; For now, just be aware that four main &quot;super
classes&quot; are used in a Document/View application:

<UL>
	<LI>The<I> document</I> class controls the data used by an application. The data
	does not have to be an actual page of text; for example, a spreadsheet or project
	plan can easily be represented as a document.<BR>
	<BR>
	
	<LI>The<I> view</I> class is used to display information about the document to the
	user and to handle any interaction that is required between the user and the document.<BR>
	<BR>
	
	<LI>The <I>frame</I> class is used to physically contain the view, menu, toolbar,
	and other physical elements of the program<I>.</I><BR>
	<BR>
	
	<LI>The <I>application</I> class controls the application-level interaction with
	Windows.
</UL>

<P>In addition to the four classes listed here, you will learn in Hour 9 some specialized
classes that are used in Document/View programs.
<H3><FONT COLOR="#000077"><B>Types of Applications Built by AppWizard</B></FONT></H3>
<P>AppWizard sets up the following types of generic programs for you:

<UL>
	<LI><I>Single Document</I>, or SDI: A program that controls a single document at
	a time<BR>
	<BR>
	
	<LI><I>Multiple Document</I>, or MDI: A program that can control several different
	documents at once<BR>
	<BR>
	
	<LI><I>Dialog based</I>: A program that has a dialog box as its main display window
</UL>

<P>After you select one of these application types, you are asked for more information
about the new program. The opening MFC AppWizard screen is shown in Figure 8.2.</P>
<P><A NAME="02"></A><A HREF="02.htm" tppabs="http://www.mcp.com/824169600/0-672/0-672-31242-5/ch08/02.htm"><B>Figure 8.2.</B> </A><I><BR>
The opening screen for MFC AppWizard.</I>
<H3><FONT COLOR="#000077"><B>Using AppWizard to Create an SDI Application</B></FONT></H3>
<P>To create a simple SDI program, select Single Document on the opening MFC AppWizard
screen. AppWizard displays six Wizard pages filled with default information for a
typical SDI program. You can move to the next page by pressing the button labeled
Next and to the previous page by pressing the button labeled Back. At any time you
can tell AppWizard to create the project for you by pressing the button labeled Finish.</P>
<P>AppWizard will create several classes and files for you and create a project that
you can use to manage the process of compiling the program. AppWizard creates these
classes for a program named Hello:

<UL>
	<LI><TT>CHelloApp</TT>: Derived from <TT>CWinApp</TT>, the application class for
	the program<BR>
	<BR>
	
	<LI><TT>CHelloDoc</TT>: The program's document class, derived from <TT>CDocument</TT><BR>
	<BR>
	
	<LI><TT>CHelloView</TT>: The program's view class derived from <TT>CView</TT><BR>
	<BR>
	
	<LI><TT>CMainFrame</TT>: The main frame class for the program
</UL>

<P>In addition, AppWizard creates several files that are not used for C++ classes.
Some of these files are

<UL>
	<LI><TT>hello.aps</TT>: A file that contains a precompiled version of the program's
	resources<BR>
	<BR>
	
	<LI><TT>hello.clw</TT>: A file that contains information used by ClassWizard<BR>
	<BR>
	
	<LI><TT>readme.txt</TT>: A file that has information about all the files created
	by AppWizard<BR>
	<BR>
	
	<LI><TT>hello.rc</TT>: A resource file that contains information about dialog boxes,
	menus, and other resources used by the program<BR>
	<BR>
	
	<LI><TT>resource.h</TT>: A header file containing declarations needed for the resources
	used by the program<BR>
	<BR>
	
	<LI><TT>hello.dsp</TT> and <TT>hello.dsw</TT>: The project and workspace files used
	by Developer Studio to build the program<BR>
	<BR>
	
	<LI><TT>stdafx.cpp</TT>: A file included in all AppWizard programs that includes
	all the standard <TT>include</TT> files<BR>
	<BR>
	
	<LI><TT>stdafx.h</TT>: A standard header file included in all AppWizard programs
	that is used to include other files that are included in the precompiled headers
</UL>

<P>Creating a Windows program using AppWizard is easy. In fact, you can compile and
run the program as it is now, although it doesn't really do anything.
<H2><FONT COLOR="#000077"><B>What Are Messages?</B></FONT></H2>
<P>Programs written for Microsoft Windows react to events that are sent to a program's
main window. Examples of events include moving the mouse pointer, clicking a button,
or pressing a key. These events are sent to the window in the form of messages. Each
message has a specific purpose: redraw the window, resize the window, close the window,
and so on.</P>
<P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>The <I>default window procedure</I>
is a special message-handling function supplied by Windows that handles the message
if no special processing is required.</P>
<P>For many messages, the application can just pass the message to the default window
procedure.</P>
<P>A Windows program can also send messages to other windows. Because every control
used in a Windows program is also a window, messages are also often used to communicate
with controls.</P>
<P>Two different types of messages are handled by a Windows program:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -