?? ch03.htm
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1">
<SCRIPT LANGUAGE="JavaScript">
<!--
function popUp(pPage) {
var fullURL = document.location;
var textURL = fullURL.toString();
var URLlen = textURL.length;
var lenMinusPage = textURL.lastIndexOf("/");
lenMinusPage += 1;
var fullPath = textURL.substring(0,lenMinusPage);
popUpWin = window.open('','popWin','resizable=yes,scrollbars=no,width=525,height=394');
figDoc= popUpWin.document;
zhtm= '<HTML><HEAD><TITLE>' + pPage + '</TITLE>';
zhtm += '</head>';
zhtm += '<BODY bgcolor="#FFFFFF">
<!-- Spidersoft WebZIP Ad Banner Insert -->
<TABLE width=100% border="0" cellpadding="0" cellspacing="0">
<TR>
<TD>
<ILAYER id=ad1 visibility=hidden height=60></ILAYER>
<NOLAYER>
<IFRAME SRC="http://www.spidersoft.com/ads/bwz468_60.htm" width="100%" height="60" marginwidth=0 marginheight=0 hspace=0 vspace=0 frameborder=0 scrolling=no></IFRAME>
</NOLAYER>
</TD>
</TR>
</TABLE>
<!-- End of Spidersoft WebZIP Ad Banner Insert-->
';
zhtm += '<IMG SRC="' + fullPath + pPage + '">';
zhtm += '<P><B>' + pPage + '</B>';
zhtm += '
<layer src="http://www.spidersoft.com/ads/bwz468_60.htm" visibility=hidden id=a1 width=600 onload="moveToAbsolute(ad1.pageX,ad1.pageY); a1.clip.height=60;visibility='show';"></layer>
</BODY></HTML>';
window.popUpWin.document.write(zhtm);
window.popUpWin.document.close();
// Johnny Jackson 4/28/98
}
//-->
</SCRIPT>
<link rel="stylesheet" href="../../../../includes/stylesheets/ebooks.css">
<META NAME="GENERATOR" Content="Symantec Visual Page Mac 1.1.1">
<TITLE>Teach Yourself Visual C++ 6 in 21 Days -- Ch 3 -- Allowing User Interaction--Integrating the Mouse and Keyboard in Your Application</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF">
<H1 ALIGN="CENTER"><IMG SRC="../button/sams.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM"
BORDER="0"><BR>
Teach Yourself Visual C++ 6 in 21 Days</H1>
<CENTER>
<P><A HREF="../ch02/ch02.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"
ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch04/ch04.htm"><IMG
SRC="../button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"
BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../button/contents.gif" WIDTH="128"
HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A>
<HR>
</CENTER>
<H1 ALIGN="CENTER">- 3 -<BR>
Allowing User Interaction--Integrating the Mouse and Keyboard in Your Application</H1>
<H1></H1>
<UL>
<LI><A HREF="#Heading1">Understanding Mouse Events</A>
<UL>
<LI><A HREF="#Heading2">Drawing with the Mouse</A>
<LI><A HREF="#Heading3">Improving the Drawing Program</A>
<LI><A HREF="#Heading4">Adding the Finishing Touches</A>
</UL>
<LI><A HREF="#Heading5">Capturing Keyboard Events</A>
<UL>
<LI><A HREF="#Heading6">Changing the Drawing Cursor</A>
<LI><A HREF="#Heading7">Making the Change Stick</A>
</UL>
<LI><A HREF="#Heading8">Summary</A>
<LI><A HREF="#Heading9">Q&A</A>
<LI><A HREF="#Heading10">Workshop</A>
<UL>
<LI><A HREF="#Heading11">Quiz</A>
</UL>
<LI><A HREF="#Heading12">Exercises</A>
</UL>
<P>
<HR SIZE="4">
<BR>
Depending on the type of application you are creating, you might need to notice what
the user is doing with the mouse. You need to know when and where the mouse was clicked,
which button was clicked, and when the button was released. You also need to know
what the user did while the mouse button was being held down.</P>
<P>Another thing that you might need to do is read the keyboard events. As with the
mouse, you might need to know when a key was pressed, how long it was held down,
and when it was released.</P>
<P>Today you are going to learn</P>
<P>
<UL>
<LI>What mouse events are available for use and how to determine which one is appropriate
for your application's needs.
<P>
<LI>How you can listen to mouse events and how to react to them in your Visual C++
application.
<P>
<LI>What keyboard events are available for use and what actions will trigger each
of these events.
<P>
<LI>How to capture keyboard events and take action based on what the user pressed.
</UL>
<H2><A NAME="Heading1"></A>Understanding Mouse Events</H2>
<P>As you learned yesterday, when you are working with most controls, you are limited
to a select number of events that are available in the Class Wizard. When it comes
to mouse events, you are limited for the most part to click and double-click events.
Just looking at your mouse tells you that there must be more to capturing mouse events
than recognizing these two. What about the right mouse button? How can you tell if
it has been pressed? And what about drawing programs? How can they follow where you
drag the mouse?</P>
<P>If you open the Class Wizard in one of your projects, select the dialog in the
list of object IDs, and then scroll through the list of messages that are available,
you will find a number of mouse-related events, which are also listed in Table 3.1.
These event messages enable you to perform any task that might be required by your
application.</P>
<P>
<H4>TABLE 3.1. MOUSE EVENT MESSAGES.</H4>
<P>
<TABLE BORDER="1">
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT"><I>Message</I></TD>
<TD ALIGN="LEFT"><I>Description</I></TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">WM_LBUTTONDOWN </TD>
<TD ALIGN="LEFT">The left mouse button has been pressed. </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">WM_LBUTTONUP </TD>
<TD ALIGN="LEFT">The left mouse button has been released. </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">WM_LBUTTONDBLCLK </TD>
<TD ALIGN="LEFT">The left mouse button has been double-clicked. </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">WM_RBUTTONDOWN </TD>
<TD ALIGN="LEFT">The right mouse button has been pressed. </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">WM_RBUTTONUP </TD>
<TD ALIGN="LEFT">The right mouse button has been released. </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">WM_RBUTTONDBLCLK </TD>
<TD ALIGN="LEFT">The right mouse button has been double-clicked. </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">WM_MOUSEMOVE </TD>
<TD ALIGN="LEFT">The mouse is being moved across the application window space. </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">WM_MOUSEWHEEL </TD>
<TD ALIGN="LEFT">The mouse wheel is being moved. </TD>
</TR>
</TABLE>
<H3><A NAME="Heading2"></A>Drawing with the Mouse</H3>
<P>Today you are going to build a simple drawing program that uses some of the available
mouse events to let the user draw simple figures on a dialog window. This application
depends mostly on the WM_MOUSEMOVE event message, which signals that the mouse is
being moved. You will look at how you can tell within this event function whether
the left mouse button is down or up. You will also learn how you can tell where the
mouse is on the window. Sound's fairly straight ahead, so let's get going by following
these steps:</P>
<P>
<DL>
<DT></DT>
<DD><B>1. </B>Create a new MFC AppWizard workspace project, calling the project <B>Mouse</B>.
<P>
<DT></DT>
<DD><B>2. </B>Specify that this project will be a dialog-based application in the
first AppWizard step.
<P>
<DT></DT>
<DD><B>3. </B>Use the default settings in the AppWizard. In the second step, specify
a suitable dialog title, such as <B>Mouse and Keyboard</B>.
<P>
<DT></DT>
<DD><B>4. </B>After the application shell is created, remove all controls from the
dialog window. This provides the entire dialog window surface for drawing. This step
is also necessary for your application to capture any keyboard events.
<P>
</DL>
<BLOCKQUOTE>
<P>
<HR>
<STRONG>NOTE:</STRONG> If there are any controls on a dialog, all keyboard events are directed
to the control that currently has input focus--the control that is highlighted or
has the cursor visible in it. To capture any keyboard events in a dialog, you have
to remove all controls from the dialog.
<HR>
</BLOCKQUOTE>
<DL>
<DT><B></B></DT>
<DD><B>5. </B>Open the Class Wizard. Select WM_MOUSEMOVE from the list of messages,
and add a function by clicking the Add Function button. Click the OK button to accept
the suggested function name.
<P>
<DT></DT>
<DD><B>6. </B>Click the Edit Code button to edit the OnMouseMove function you just
created, adding the code in Listing 3.1.
<P>
</DL>
<H4>LISTING 3.1. THE OnMouseMove FUNCTION.</H4>
<PRE> 1: void CMouseDlg::OnMouseMove(UINT nFlags, CPoint point)
2: {
3: // TODO: Add your message handler code here and/or call default
4:
5: ///////////////////////
6: // MY CODE STARTS HERE
7: ///////////////////////
8:
9: // Check to see if the left mouse button is down
10: if ((nFlags & MK_LBUTTON) == MK_LBUTTON)
11: {
12: // Get the Device Context
13: CClientDC dc(this);
14:
15: // Draw the pixel
16: dc.SetPixel(point.x, point.y, RGB(0, 0, 0));
17: }
18:
19: ///////////////////////
20: // MY CODE ENDS HERE
21: ///////////////////////
22:
23: CDialog::OnMouseMove(nFlags, point);
24: }
</PRE>
<P>Look at the function definition at the top of the listing. You will notice that
two arguments are passed into this function. The first of these arguments is a set
of flags that can be used to determine whether a mouse button is depressed (and which
one). This determination is made in the first line of your code with the if statement:</P>
<P>
<PRE>if ((nFlags & MK_LBUTTON) == MK_LBUTTON)
</PRE>
<P>In the first half of the condition being evaluated, the flags are filtered down
to the one that indicates that the left mouse button is down. In the second half,
the filtered flags are compared to the flag that indicates that the left mouse button
is down. If the two match, then the left mouse button is down.</P>
<P>The second argument to this function is the location of the mouse. This argument
gives you the coordinates on the screen where the mouse currently is. You can use
this information to draw a spot on the dialog window.</P>
<P>Before you can draw any spots on the dialog window, you need to get the device
context for the dialog window. This is done by declaring a new instance of the CClientDC
class. This class encapsulates the device context and most of the operations that
can be performed on it, including all the screen drawing operations. In a sense,
the device context is the canvas upon which you can draw with your application. Until
you have a canvas, you cannot do any drawing or painting. After the device context
object is created, you can call its SetPixel function, which colors the pixel at
the location specified in the first two arguments with the color specified in the
third argument. If you compile and run your program, you can see how it allows you
to draw on the window surface with the mouse, as shown in Figure 3.1.</P>
<P><A HREF="javascript
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -