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

📄 ch08.htm

📁 24小时精通VC
💻 HTM
📖 第 1 页 / 共 4 页
字号:
<UL>
	<LI>Messages sent from the operating system
	<LI>Messages sent to and from controls that deal with user input
</UL>

<P>Examples of messages sent from the operating system include messages used to tell
the program that it should start or close or to tell a window that it is being resized
or moved. Messages sent to controls can be used to change the font used by a window
or its title. Messages received from a control include notifications that a button
has been pressed or that a character has been entered in an edit control.</P>
<P>There are two reasons why messages are used so heavily in Windows programs:

<UL>
	<LI>Unlike a function call, a message is a physical chunk of data, so it can be easily
	queued and prioritized.<BR>
	<BR>
	
	<LI>A message is not dependent on a particular language or processor type, so a message-based
	program can easily be ported to other CPUs, as is often done with Windows NT.
</UL>

<P>Queues work well for event-driven programming. When an event occurs, a message
can be created and quickly queued to the appropriate window or program. Each message
that is queued can then be handled in an orderly manner.</P>
<P>The fact that messages are language independent has enabled Windows to grow over
the years. Today, you can write a Windows program using diverse languages such as
Visual Basic, Delphi, Visual C++, or PowerBuilder. Because messages are language
independent, messages can easily be sent between these programs. The message interface
enables you to add new features to the programs you write and also enables Windows
to grow in the future.


<BLOCKQUOTE>
	<P>
<HR>
<B> </B><FONT COLOR="#000077"><B>Just a Minute:</B></FONT><B> </B>Since it was first
	introduced, every release of Microsoft Windows has added new messages and new functionality.
	However, most of the core messages used in the initial version of Windows still are
	available, even on multiprocessor machines that are running Windows NT. 
<HR>
</P>
	<P>
<HR>
<B> </B><FONT COLOR="#000077"><B>CAUTION:</B></FONT><B> </B>When using an event-driven
	programming model such as Microsoft Windows, you cannot always be certain about message
	order. A subtle difference in the way different users use a program can cause messages
	to be received in a different sequence. This means that every time you handle an
	event, you should handle only that particular event and not assume that any other
	activity has taken place. 
<HR>


</BLOCKQUOTE>

<H3><FONT COLOR="#000077"><B>A Program to Test for Mouse Clicks</B></FONT></H3>
<P>As an example, you're about to create a program that actually shows how messages
are used to notify your application about events. This program, MouseTst, will be
an SDI application that displays a message whenever the mouse is clicked inside the
client area. The first step in creating MouseTst is to use AppWizard to create an
SDI application. Feel free to select or remove any options offered by AppWizard,
because none of the options have any bearing on the demonstration. Name the application
MouseTst.
<H3><FONT COLOR="#000077"><B>What Are Message Queues?</B></FONT></H3>
<P>Messages are delivered to all windows that must receive events. For example, the
simple act of moving the mouse cursor across the main window of a Windows program
generates a large number of messages. Messages sent to a window are placed in a queue,
and a program must examine each message in turn. Typically, a program examines messages
that are sent to it and responds only to messages that are of interest, as shown
in Figure 8.3.</P>

<P><A NAME="03"></A><A HREF="03.htm" tppabs="http://www.mcp.com/824169600/0-672/0-672-31242-5/ch08/03.htm"><B>Figure 8.3.</B> </A><I><BR>
Messages queued and handled in order by an application.</I></P>

<P>As shown in Figure 8.3, messages sent to a program are handled by a window procedure
that is defined for the program.
<H3><FONT COLOR="#000077"><B>How Are Messages Handled?</B></FONT></H3>
<P>When a user moves the mouse over a program's main window, two messages are sent
to the program's window procedure.

<UL>
	<LI><TT>WM_NCMOUSEMOVE</TT> is sent when the mouse is moved over the menu or caption
	bar.
	<LI><TT>WM_MOUSEMOVE</TT> is sent when the mouse is over the window's client area.
</UL>

<P>Another type of mouse message is the <TT>WM_LBUTTONDOWN</TT> message, sent when
the primary mouse button is pressed. Because this is the left button for most mouse
users, the message is named <TT>WM_LBUTTONDOWN</TT>. A similar message is <TT>WM_RBUTTONDOWN</TT>,
sent when the secondary, usually right, mouse button is pressed.</P>
<P>These and other messages are sent to a window's <I>window procedure</I>. A window
procedure is a function that handles messages sent to it. When a window procedure
receives the message, the parameters passed along with the message are used to help
decide how the message should be handled.
<H2><FONT COLOR="#000077"><B>Handling Messages with ClassWizard</B></FONT></H2>
<P>ClassWizard (also called MFC ClassWizard) adds code that typically is used for
a particular message-handling function. This commonly reused, or &quot;boilerplate,&quot;
code can help reduce the number of errors still further, because it's guaranteed
to be correct. Listing 8.1 is an example of a function created by ClassWizard to
handle the <TT>WM_LBUTTONDOWN</TT> message.
<H4><FONT COLOR="#000077">TYPE: Listing 8.1. The OnLButtonDown function created by
ClassWizard.</FONT></H4>
<PRE><FONT COLOR="#0066FF"><TT>void CMyView::OnLButtonDown(UINT nFlags, CPoint point)</TT>
<TT>{</TT>
<TT>    //TODO: Add your message handler code here and/or call default</TT>

<TT>    CView::OnLButtonDown(nFlags, point);</TT>
<TT>}</TT> </FONT></PRE>
<P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>A <I>message map</I> connects
messages sent to a program with the functions that are meant to handle those messages.</P>

<P>When AppWizard or ClassWizard adds a message-handling function, an entry is added
to the class message map. Listing 8.2 shows an example of a message map.
<H4><FONT COLOR="#000077">TYPE: Listing 8.2. A message map for the CMyView class.</FONT></H4>
<PRE><FONT COLOR="#0066FF"><TT>BEGIN_MESSAGE_MAP(CMyView, CView)</TT>
<TT>    //{{AFX_MSG_MAP(CMyView)</TT>
<TT>    ON_WM_LBUTTONDOWN()</TT>
<TT>    //}}AFX_MSG_MAP</TT>
<TT>    // Standard printing commands</TT>
<TT>    ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)</TT>
<TT>    ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)</TT>
<TT>    ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)</TT>
<TT>END_MESSAGE_MAP()</TT></FONT></PRE>


<BLOCKQUOTE>
	<P>
<HR>
<B> </B><FONT COLOR="#000077"><B>CAUTION:</B></FONT><B> </B>The message map begins
	with the <TT>BEGIN_MESSAGE_MAP</TT> macro and ends with the <TT>END_MESSAGE_MAP</TT>
	macro. The lines reserved for use by ClassWizard start with <TT>//{{AFX_MSG_MAP</TT>
	and end with <TT>//}}AFX_MSG_MAP</TT>. If you make manual changes to the message
	map, do not change the entries reserved for ClassWizard; they are maintained automatically.
	
<HR>


</BLOCKQUOTE>

<H3><FONT COLOR="#000077"><B>Messages Handled by MouseTst</B></FONT></H3>
<P>The MouseTst program must handle four messages used to collect mouse events. The
messages used by MouseTst are listed in Table 8.1.
<H4><FONT COLOR="#000077">Table 8.1. Messages handled by MouseTst.</FONT></H4>
<P>
<TABLE BORDER="1">
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><B>Message</B></TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><B>Function</B></TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><B>Description</B></TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>WM_LBUTTONDOWN</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>OnLButtonDown</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP">Left mouse button clicked</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>WM_LBUTTONDBLCLK</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>OnLButtonDblClk</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP">Left mouse button double-clicked</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>WM_RBUTTONDOWN</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>OnRButtonDown</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP">Right mouse button clicked</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>WM_RBUTTONDBLCLK</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>OnRButtonDblClk</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP">Right mouse button double-clicked</TD>
	</TR>
</TABLE>
</P>
<P>In addition, when the <TT>WM_PAINT</TT> message is received, the MFC framework
calls the <TT>OnDraw</TT> member function. MouseTst will use <TT>OnDraw</TT> to update
the display with the current mouse position and last message.
<H3><FONT COLOR="#000077"><B>Updating the <TT>CMouseTst</TT> View Class</B></FONT></H3>
<P>All the work that keeps track of the mouse events will be done in the <TT>CMouseTstView</TT>
class. There are two steps to displaying the mouse event information in the MouseTst
program:

<DL>
	<DD>1. When one of the four mouse events occurs, the event type and mouse position
	are recorded, and the view's rectangle is invalidated. This causes a <TT>WM_PAINT</TT>
	message to be generated by Windows and sent to the MouseTst application.<BR>
	<BR>
	2. When a <TT>WM_PAINT</TT> message is received by MouseTst, the <TT>CMouseTstView::OnDraw</TT>
	member function is called, and the mouse event and position are displayed.
</DL>



<BLOCKQUOTE>
	<P>
<HR>
<B> </B><FONT COLOR="#000077"><B>Just a Minute:</B></FONT><B> </B>All output is done
	in response to a <TT>WM_PAINT</TT> message. <TT>WM_PAINT</TT> is sent when a window's
	client area is invalidated. This often is due to the window being uncovered or reopened.
	Because the window must be redrawn in response to a <TT>WM_PAINT</TT> message, most
	programs written for Windows do all their drawing in response to <TT>WM_PAINT</TT>
	and just invalidate their display window or view when the window should be updated.
	
<HR>


</BLOCKQUOTE>

<P>To keep track of the mouse event and position, you must add two member variables
to the <TT>CMouseTstView</TT> class. Add the three lines from Listing 8.3 as the
last three lines before the closing curly brace in <TT>CMouseTstView.h</TT>.
<H4><FONT COLOR="#000077">TYPE: Listing 8.3. New member variables for the CMouseTstView
class.</FONT></H4>
<PRE><FONT COLOR="#0066FF"><TT>private:</TT>
<TT>    CPoint   m_ptMouse;</TT>
</FONT></PRE>
<P><TT>CString m_szDescription;</TT> The constructor for <TT>CMouseTstView</TT> must
initialize the new member variables. Edit the constructor for <TT>CMouseTstView</TT>,
found in <TT>CMouseTstView.cpp</TT>, so it looks like the source code in Listing
8.4.
<H4><FONT COLOR="#000077">TYPE: Listing 8.4. The constructor for CMouseTstView.</FONT></H4>
<PRE><FONT COLOR="#0066FF"><TT>CMouseTstView::CMouseTstView()</TT>
<TT>{</TT>
<TT>    m_ptMouse = CPoint(0,0);</TT>
<TT>    m_szDescription.Empty();</TT>
</FONT></PRE>
<P><TT>}</TT> Using ClassWizard, add message-handling functions for the four mouse
events that you're handling in the MouseTst program. Open ClassWizard by pressing
Ctrl+W, or by right-clicking in a source-code window and selecting ClassWizard from
the menu. After ClassWizard appears, follow these steps:

<DL>
	<DD>1. Select the <TT>CMouseTstView</TT> class in the Object ID list box; a list

⌨️ 快捷键说明

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