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

📄 ch05.htm

📁 Learning language of Visual C++6
💻 HTM
📖 第 1 页 / 共 4 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>

<HEAD>
<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 += '<link rel="stylesheet" href="/includes/stylesheets/ebooks.css"></head>';
 zhtm += '<BODY bgcolor="#FFFFFF">';
 zhtm += '<IMG SRC="' + fullPath + pPage + '">';
 zhtm += '<P><B>' + pPage + '</B>';
 zhtm += '</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">

	
	<TITLE>Special Edition Using Visual C++ 6 -- Ch 5 -- Drawing on the Screen</TITLE>
</HEAD>

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

<CENTER>
<H1><IMG SRC="../button/que.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM" BORDER="0"><BR>
Special Edition Using Visual C++ 6</H1>
</CENTER>
<CENTER>
<P><A HREF="../ch04/ch04.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"
ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch06/ch06.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>
<CENTER>
<H1>- 5 -</H1>
</CENTER>
<CENTER>
<H1>Drawing on the Screen</H1>
</CENTER>

<UL>
	<LI><A HREF="#Heading1">Understanding Device Contexts</A>
	<LI><A HREF="#Heading2">Introducing the Paint1 Application</A>
	<LI><A HREF="#Heading3">Building the Paint1 Application</A>
	<UL>
		<LI><A HREF="#Heading4">Painting in an MFC Program</A>
		<LI><A HREF="#Heading5">Switching the Display</A>
		<LI><A HREF="#Heading6">Using Fonts</A>
		<LI><A HREF="#Heading7">Sizing and Positioning the Window</A>
		<LI><A HREF="#Heading8">Using Pens</A>
		<LI><A HREF="#Heading9">Using Brushes</A>
	</UL>
	<LI><A HREF="#Heading10">Scrolling Windows</A>
	<LI><A HREF="#Heading11">Building the Scroll Application</A>
	<UL>
		<LI><A HREF="#Heading12">Adding Code to Increase Lines</A>
		<LI><A HREF="#Heading13">Adding Code to Decrease Lines</A>
	</UL>
</UL>

<P>
<HR SIZE="4">

<CENTER>
<H1></H1>
</CENTER>
<H2><A NAME="Heading1"></A>Understanding Device Contexts</H2>
<P>Most applications need to display some type of data in their windows. You'd think
that, because Windows is a device-independent operating system, creating window displays
would be easier than luring a kitten with a saucer of milk. However, it's exactly
Windows' device independence that places a little extra burden on a programmer's
shoulders. Because you can never know in advance exactly what type of devices may
be connected to a user's system, you can't make many assumptions about display capabilities.
Functions that draw to the screen must do so indirectly through something called
a <I>device context</I> (DC).</P>
<P>Although device independence forces you, the programmer, to deal with data displays
indirectly, it helps you by ensuring that your programs run on all popular devices.
In most cases, Windows handles devices for you through the device drivers that users
have installed on the system. These device drivers intercept the data that the application
needs to display and then translates the data appropriately for the device on which
it will appear, whether that's a screen, a printer, or some other output device.</P>
<P>To understand how all this device independence works, imagine an art teacher trying
to design a course of study appropriate for all types of artists. The teacher creates
a course outline that stipulates the subject of a project, the suggested colors to
be used, the dimensions of the finished project, and so on. What the teacher doesn't
stipulate is the surface on which the project will be painted or the materials needed
to paint on that surface. In other words, the teacher stipulates only general characteristics.
The details of how these characteristics are applied to the finished project are
left to each specific artist.</P>
<P>For example, an artist using oil paints will choose canvas as his drawing surface
and oil paints, in the colors suggested by the instructor, as the paint. On the other
hand, an artist using watercolors will select watercolor paper and will, of course,
use watercolors instead of oils for paint. Finally, the charcoal artist will select
the appropriate drawing surface for charcoal and will use a single color.</P>
<P>The instructor in this scenario is much like a Windows programmer. The programmer
has no idea who may eventually use the program and what kind of system that user
may have. The programmer can recommend the colors in which data should be displayed
and the coordinates at which the data should appear, for example, but it's the device
driver--the Windows artist--who ultimately decides how the data appears.</P>
<P>A system with a VGA monitor may display data with fewer colors than a system with
a Super VGA monitor. Likewise, a system with a monochrome monitor displays the data
in only a single color. High-resolution monitors can display more data than lower-resolution
monitors. The device drivers, much like the artists in the imaginary art school,
must take the display requirements and fine-tune them to the device on which the
data will actually appear. And it's a data structure known as a <I>device context</I>
that links the application to the device's driver.</P>
<P>A device context (DC) is little more than a data structure that keeps track of
the attributes of a window's drawing surface. These attributes include the currently
selected pen, brush, and font that will be used to draw onscreen. Unlike an artist,
who can have many brushes and pens with which to work, a DC can use only a single
pen, brush, or font at a time. If you want to use a pen that draws wider lines, for
example, you need to create the new pen and then replace the DC's old pen with the
new one. Similarly, if you want to fill shapes with a red brush, you must create
the brush and <I>select it into the DC</I>, which is how Windows programmers describe
replacing a tool in a DC.</P>
<P>A window's client area is a versatile surface that can display anything a Windows
program can draw. The client area can display any type of data because everything
displayed in a window--whether it be text, spreadsheet data, a bitmap, or any other
type of data--is displayed graphically. MFC helps you display data by encapsulating
Windows' GDI functions and objects into its DC classes.</P>
<P>
<H2><A NAME="Heading2"></A>Introducing the Paint1 Application</H2>
<P>In this chapter, you will build the Paint1 application, which demonstrates fonts,
pens, and brushes. Paint1 will use the document/view paradigm discussed in Chapter
4, &quot;Documents and Views,&quot; and the view will handle displaying the data.
When run, the application will display text in several different fonts. When users
click the application, it displays lines drawn with several different pens. After
another click, it displays boxes filled with a variety of brushes.</P>
<P>The first step in creating Paint1 is to build an empty shell with AppWizard, as
first discussed in Chapter 1, &quot;Building Your First Windows Application.&quot;
Choose File, New, and select the Projects tab. As shown in Figure 5.1, fill in the
project name as <B>Paint1</B> and fill in an appropriate directory for the project
files. Make sure that MFC AppWizard (exe) is selected. Click OK.</P>
<P><A HREF="javascript:popUp('05uvc01.gif')"><B>FIG. 5.1</B></A><B> </B><I>Start
an AppWizard project workspace called Paint1.</I></P>
<P>Move through the AppWizard dialog boxes, change the settings to match those in
the list that follows, and then click Next to move to the next step.</P>
<P>Step 1: Select Single Document.</P>
<P>Step 2: Use default settings.</P>
<P>Step 3: Use default settings.</P>
<P>Step 4: Deselect all check boxes.</P>
<P>Step 5: Use default settings.</P>
<P>Step 6: Use default settings.</P>
<P>After you click Finish on the last step, the New Project Information box should
resemble Figure 5.2. Click OK to create the project.</P>
<P><A HREF="javascript:popUp('05uvc02.gif')"><B>FIG. 5.2</B></A><B> </B><I>The starter
application for Paint1 is very simple.</I></P>
<P>Now that you have a starter application, it's time to add code to make it demonstrate
some ways an MFC program can display data onscreen. By the time you get to the end
of this chapter, the words <I>display context</I> won't make you scratch your head
in perplexity.</P>


<BLOCKQUOTE>
	<P>
<HR>
<strong>NOTE:</strong> Your starter application has menus, but you will ignore them completely.
	It would be quite a bit of work to remove them; just pretend they aren't there.&#160;n
	
<HR>


</BLOCKQUOTE>

<H2><A NAME="Heading3"></A>Building the Paint1 Application</H2>
<P>To build the Paint1 application, you first need to understand how painting and
drawing work in an MFC program. Then you can set up the skeleton code to handle user
clicks and the three different kinds of display. Finally, you'll fill in the code
for each kind of display in turn.</P>
<P>
<H3><A NAME="Heading4"></A>Painting in an MFC Program</H3>
<P>In Chapter 3, &quot;Messages and Commands,&quot; you learned about message maps
and how you can tell MFC which functions to call when it receives messages from Windows.
One important message that every Windows program with a window must handle is WM_PAINT.
Windows sends the WM_PAINT message to an application's window when the window needs
to be redrawn. Several events cause Windows to send a WM_PAINT message:</P>

<UL>
	<LI>When users simply run the program: In a properly written Windows application,
	the application's window receives a WM_PAINT message almost immediately after being
	run, to ensure that the appropriate data is displayed from the very start.
	<P>
	<LI>When the window has been resized or has recently been uncovered (fully or partially)
	by another window: Part of the window that wasn't visible before is now onscreen
	and must be updated.
	<P>
	<LI>When a program indirectly sends itself a WM_PAINT message by invalidating its
	client area: This capability ensures that an application can change its window's
	contents almost any time it wants. For example, a word processor might invalidate
	its window after users paste some text from the Clipboard.
</UL>

<P>When you studied message maps, you learned to convert a message name to a message-map
macro and function name. You now know, for example, that the message-map macro for
a WM_PAINT message is ON_WM_PAINT(). You also know that the matching message-map
function should be called OnPaint(). This is another case where MFC has already done
most of the work of matching a Windows message with its message-response function.
(If all this message-map stuff sounds unfamiliar, you might want to review Chapter
3.)</P>
<P>You might guess that your next step is to catch the WM_PAINT message or to override
the OnPaint() function that your view class inherited from CView, but you won't do
that. Listing 5.1 shows the code for CView::OnPaint(). As you can see, WM_PAINT is
already caught and handled for you.</P>
<P>
<H4>Listing 5.1&#160;&#160;CView::OnPaint()</H4>
<PRE>void CView::OnPaint()
{
     // standard paint routine
     CPaintDC dc(this);
     OnPrepareDC(&amp;dc);
     OnDraw(&amp;dc);
</PRE>
<PRE>}
</PRE>
<P>CPaintDC is a special class for managing <I>paint DCs</I>--device contexts used
only when responding to WM_PAINT messages. An object of the CPaintDC class does more
than just create a DC; it also calls the BeginPaint() Windows API function in the
class's constructor and calls EndPaint() in its destructor. When a program responds
to WM_PAINT messages, calls to BeginPaint() and EndPaint() are required. The CPaintDC
class handles this requirement without your having to get involved in all the messy
details. As you can see, the CPaintDC constructor takes a single argument, which
is a pointer to the window for which you're creating the DC. The this pointer points
to the current view, so it's passed to the constructor to make a DC for the current
view.</P>
<P>OnPrepareDC() is a CView function that prepares a DC for use. You'll learn more
about it in Chapter 6, &quot;Printing and Print Preview.&quot;</P>
<P>OnDraw() does the actual work of visually representing the document. In most cases
you will write the OnDraw() code for your application and never touch OnPaint().</P>
<P>
<H3><A NAME="Heading5"></A>Switching the Display</H3>
<P>The design for Paint1 states that when you click the application's window, the
window's display changes. This seemingly magical feat is actually easy to accomplish.
You add a member variable to the view to store what kind of display is being done
and then change it when users click the window. In other words, the program routes
WM_LBUTTONDOWN messages to the OnLButtonDown() message-response function, which sets
the m_display flag as appropriate.</P>
<P>First, add the member variable. You must add it by hand rather than through the
shortcut menu because the type includes an enum declaration. Open Paint1View.h from
the FileView and add these lines after the //Attributes comment:</P>
<P>
<PRE>protected:
     enum {Fonts, Pens, Brushes} m_Display;
</PRE>


<BLOCKQUOTE>
	<P>
<HR>
<strong>TIP:</strong> This is an <I>anonymous</I> or unnamed enum. You can learn more about
	enum types in Appendix A, &quot; C++ Review and Object-Oriented Concepts.&quot; 
<HR>


</BLOCKQUOTE>

<P>Choose ClassView in the Project Workspace pane, expand the classes, expand CPaint1View,
and then double-click the constructor CPaint1View(). Add this line of code in place
of the TODO comment:</P>
<P>
<PRE>m_Display = Fonts;
</PRE>

⌨️ 快捷键说明

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