ch03.htm
来自「A very good resource on Visual C++ 6.0 e」· HTM 代码 · 共 892 行 · 第 1/4 页
HTM
892 行
File, Save As--MFC classes catch it, put up the dialog box to obtain the new filename,
handle all the behind-the-scenes work, and finally call one of your functions, which
must be named Serialize(), to actually write out the document. (Chapter 7, "Persistence
and File I/O," explains the Serialize() function.) You need only to add message
map entries for behavior that is not common to all applications.</P>
<P>
<H2><A NAME="Heading7"></A>Learning How ClassWizard Helps You Catch Messages</H2>
<P>Message maps may not be simple to read, but they are simple to create if you use
ClassWizard. There are two ways to add an entry to a message map in Visual C++ 6.0:
with the main ClassWizard dialog box or with one of the new dialog boxes that add
message handlers or virtual functions. This section shows you these dialog boxes
for ShowString, rather than work you through creating a sample application.</P>
<P>
<H3><A NAME="Heading8"></A>The ClassWizard Tabbed Dialog Box</H3>
<P>The main ClassWizard dialog box is displayed by choosing View, ClassWizard or
by pressing Ctrl+W. ClassWizard is a tabbed dialog box, and Figure 3.1 shows the
Message Maps tab. At the top of the dialog box are two drop-down list boxes, one
that reminds you which project you are working on (ShowString in this case) and the
other that reminds you which class owns the message map you are editing. In this
case, it is the CShowStringApp class, whose message map you have already seen.</P>
<P><A HREF="javascript:popUp('03uvc01.gif')"><B>FIG. 3.1</B></A><B> </B><I>ClassWizard
makes catching messages simple.</I></P>
<P>Below those single-line boxes is a pair of multiline boxes. The one on the left
lists the class itself and all the commands that the user interface can generate.
Commands are discussed in the "Commands" section later in this chapter.
With the classname highlighted, the box on the right lists all the Windows messages
this class might catch. It also lists a number of virtual functions that catch common
messages.</P>
<P>To the right of those boxes are buttons where you can add a new class to the project,
add a function to the class to catch the highlighted message, remove a function that
was catching a message, or open the source code for the function that catches the
highlighted message. Typically, you select a class, select a message, and click Add
Function to catch the message. Here's what the Add Function button sets in motion:</P>
<P>
<UL>
<LI>Adds a skeleton function to the bottom of the source file for the application
<P>
<LI>Adds an entry to the message map in the source file
<P>
<LI>Adds an entry to the message map in the include file
<P>
<LI>Updates the list of messages and member functions in the dialog box
</UL>
<P>After you add a function, clicking Edit Code makes it simple to start filling
in the behavior of that function. If you prefer, double-click the function name in
the Member Functions list box.</P>
<P>Below the Object IDs and Messages boxes is a list of the member functions of this
class that are related to messages. This class has two such functions:</P>
<P>
<UL>
<LI>InitInstance()--Overrides a virtual function in CWinApp, the base class for CShowStringApp,
and is labeled with a V (for <I>virtual</I> function) in the list.
<P>
<LI>OnAppAbout()--Catches the ID_APP_ABOUT command and is labeled with a W (for Windows
message) in the list.
</UL>
<P>The InitInstance function is called whenever an application first starts. You
don't need to understand this function to see that ClassWizard reminds you the function
has been over-ridden.</P>
<P>Finally, under the Member Functions box is a reminder of the meaning of the highlighted
message. called to implement wait cursors is a description of the DoWaitCursor virtual
function.</P>
<P>
<H3><A NAME="Heading9"></A>The Add Windows Message Handler Dialog Box</H3>
<P>In release 5.0 of Visual C++, a new way of catching messages was added. Rather
than opening ClassWizard and then remembering to set the right classname in a drop-down
list box, you right-click on the classname in ClassView and then choose Add Windows
Message Handler from the shortcut menu that appears. Figure 3.2 shows the dialog
box that appears when you make this choice.</P>
<P><A HREF="javascript:popUp('03uvc02.gif')"><B>FIG. 3.2</B></A><B> </B><I>The New
Windows Message and Event Handlers dialog box is another way to catch messages.</I></P>
<P>This dialog box doesn't show any virtual functions that were listed in the main
ClassView dialog box. It is easy to see that this class catches the command ID_APP_ABOUT
but doesn't catch the command update. (Commands and command updating are discussed
in more detail later in this chapter.) To add a new virtual function, you right-click
on the class in ClassView and choose Add New Virtual Function from the shortcut menu.
Figure 3.3 shows this dialog box.</P>
<P><A HREF="javascript:popUp('03uvc03.gif')"><B>FIG. 3.3</B></A><B> </B><I>The New
Virtual Override dialog box simplifies implementing virtual functions.</I></P>
<P>You can see in Figure 3.3 that CShowStringApp already overrides the InitInstance()
virtual function, and you can see what other functions are available to be overridden.
As in the tabbed dialog box, a message area at the bottom of the dialog box reminds
you of the purpose of each function: In fact, the text--Called to implement wait
cursors--is identical to that in Figure 3.1.</P>
<P>
<H3><A NAME="Heading10"></A>Which Class Should Catch the Message?</H3>
<P>The only tricky part of message maps and message handling is deciding which class
should catch the message. That's a decision you can't make until you understand all
the different message and command targets that make up a typical application. The
choice is usually one of the following:</P>
<P>
<UL>
<LI>The active view
<P>
<LI>The document associated with the active view
<P>
<LI>The frame window that holds the active view
<P>
<LI>The application object
</UL>
<P>Views, documents, and frames are discussed in Chapter 4, "Documents and Views."</P>
<P>
<H2><A NAME="Heading11"></A>Recognizing Messages</H2>
<P>There are almost 900 Windows messages, so you won't find a list of them all in
this chapter. Usually, you arrange to catch messages with ClassWizard and are presented
with a much shorter list that is appropriate for the class you are catching messages
with. Not every kind of window can receive every kind of message. For example, only
classes that inherit from CListBox receive list box messages such as LB_SETSEL, which
directs the list box to move the highlight to a specific list item. The first component
of a message name indicates the kind of window this message is destined for, or coming
from. These window types are listed in Table 3.1.</P>
<P>
<H4>Table 3.1  Windows Message Prefixes and Window Types</H4>
<P>
<TABLE BORDER="1">
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT"><B>Prefix</B></TD>
<TD ALIGN="LEFT"><B>Window Type</B></TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">ABM, ABN </TD>
<TD ALIGN="LEFT">Appbar </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">ACM, ACN </TD>
<TD ALIGN="LEFT">Animation control </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">BM, BN </TD>
<TD ALIGN="LEFT">Button </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">CB, CBN </TD>
<TD ALIGN="LEFT">Combo box </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">CDM, CDN </TD>
<TD ALIGN="LEFT">Common dialog box </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">CPL </TD>
<TD ALIGN="LEFT">Control Panel application </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">DBT </TD>
<TD ALIGN="LEFT">Any application (device change message) </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">DL </TD>
<TD ALIGN="LEFT">Drag list box </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">DM </TD>
<TD ALIGN="LEFT">Dialog box </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">EM, EN </TD>
<TD ALIGN="LEFT">Edit box </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">FM, FMEVENT </TD>
<TD ALIGN="LEFT">File Manager </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">HDM, HDN </TD>
<TD ALIGN="LEFT">Header control </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">HKM </TD>
<TD ALIGN="LEFT">HotKey control </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">IMC, IMN </TD>
<TD ALIGN="LEFT">IME window </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">LB, LBN </TD>
<TD ALIGN="LEFT">List box </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">LVM, LVN </TD>
<TD ALIGN="LEFT">List view </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">NM </TD>
<TD ALIGN="LEFT">Any parent window (notification message) </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">PBM </TD>
<TD ALIGN="LEFT">Progress bar </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">PBT </TD>
<TD ALIGN="LEFT">Any application (battery power broadcast) </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">PSM, PSN </TD>
<TD ALIGN="LEFT">Property sheet </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">SB </TD>
<TD ALIGN="LEFT">Status bar </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">SBM </TD>
<TD ALIGN="LEFT">Scrollbar </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">STM, STN </TD>
<TD ALIGN="LEFT">Static control </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">TB, TBN </TD>
<TD ALIGN="LEFT">Toolbar </TD>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?