?? ch11.htm
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Symantec Visual Page Mac 1.1">
<TITLE>Teach Yourself Visual C++® 5 in 24 Hours -- Hour 11 -- Device Contexts</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF">
<H1 ALIGN="CENTER"><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++® 5 in 24 Hours</FONT></H1>
<CENTER>
<P><A HREF="ch10.htm" tppabs="http://www.mcp.com/824169600/0-672/0-672-31242-5/ch10/ch10.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="ch12.htm" tppabs="http://www.mcp.com/824169600/0-672/0-672-31242-5/ch12/ch12.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>
<H1 ALIGN="CENTER"><FONT COLOR="#000077">- Hour 11 -<BR>
Device Contexts</FONT></H1>
<P>All output in a program written for Windows must be done using a device context.
A <I>device context</I> is an important part of the Windows Graphics Device Interface,
or GDI. Device contexts are used by Windows and applications to provide device-independent
output.</P>
<P>In this hour, you will learn
<UL>
<LI>The different type of device contexts used in Windows and the MFC classes that
support them<BR>
<BR>
<LI>How to use device mapping modes<BR>
<BR>
<LI>How to use GDI objects in your Windows application<BR>
<BR>
<LI>How to use the different options for text output in Windows
</UL>
<P>You also will build a sample program that demonstrates how a device context is
used with text output.
<H2><FONT COLOR="#000077"><B>What Are Device Contexts?</B></FONT></H2>
<P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>A <I>device context</I>,
often abbreviated as <I>DC</I>, is a structure that stores information needed when
a program written for Windows must display output to a device. Device contexts are
maintained by Windows. The device context stores information about the drawing surface
and its capabilities. Before using any of the GDI output functions, you must create
a device context.
<BLOCKQUOTE>
<P>
<HR>
<B> </B><FONT COLOR="#000077"><B>Just a Minute:</B></FONT><B> </B>Deep down inside
Windows, device contexts are actually structures that the GDI uses to track the current
output state for a window. However, you never have access to the individual members
of a device context; instead, all access to device contexts occurs through function
calls.
<HR>
</BLOCKQUOTE>
<P>The simplest reason to use device contexts is because they are required; there's
simply no other way to perform output in a Windows program without them. However,
using a device context is the first step toward using many of the GDI features that
are available under Windows. Understanding how device contexts work can also help
make your Windows programs more efficient.
<H3><FONT COLOR="#000077"><B>Types of GDI Objects</B></FONT></H3>
<P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>Associating a GDI object
with a device context is commonly referred to as <I>selecting</I> a GDI object into
the device context.</P>
<P>A GDI object can be selected into a device context in order to provide specific
drawing capabilities for the DC. Each GDI object can be used to create a different
type of output. For example, a GDI pen object can be used to draw lines, whereas
brush objects are used to fill screen areas. The GDI objects most commonly used with
device contexts are listed in Table 11.1.
<H4><FONT COLOR="#000077">Table 11.1. Commonly used GDI objects in Windows programs.</FONT></H4>
<P>
<TABLE BORDER="1">
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><B>Object</B></TD>
<TD ALIGN="LEFT"><B>Purpose</B></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">Pen</TD>
<TD ALIGN="LEFT">Drawing lines</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">Brush</TD>
<TD ALIGN="LEFT">Filling regions</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">Bitmap</TD>
<TD ALIGN="LEFT">Displaying images</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">Font</TD>
<TD ALIGN="LEFT">Typeface characteristics</TD>
</TR>
</TABLE>
<H3><FONT COLOR="#000077"><B>Types of Device Contexts</B></FONT></H3>
<P>Windows and the MFC class library provide the following four different basic types
of device contexts. Although you use these device contexts in different situations,
the basic rules for their use are consistent.
<UL>
<LI>Display DCs, used to display information to a standard video terminal. These
are the most commonly used device contexts in a Windows program.<BR>
<BR>
<LI>Printer DCs, used to display output on a printer or plotter.<BR>
<BR>
<LI>Memory DCs, sometimes called <I>compatible device contexts</I>, used to perform
drawing operations on a bitmap.<BR>
<BR>
<LI>Information DCs, used to collect information on a device. These DCs cannot be
used for actual output. However, they are extremely fast and have little overhead,
and therefore are ideal for use when information is being collected.
</UL>
<P>With the exception of the information device contexts, each of the different DC
types is used to create a different type of output. In addition, the MFC class library
offers several different types of display device contexts, which are covered in the
section "How to Use Device Contexts."
<H3><FONT COLOR="#000077"><B>Hardware Independence</B></FONT></H3>
<P>The goal behind using device contexts is to give programs written for Windows
hardware independence. With a little care, your program can run on any display or
printer that's supported by a Windows hardware driver. Most new output devices supply
a driver if Windows doesn't currently provide automatic support. This means that
programs you write today will work with display devices that have not been developed
yet.
<BLOCKQUOTE>
<P>
<HR>
<B> </B><FONT COLOR="#000077"><B>Just a Minute:</B></FONT><B> </B>Because of the
way device contexts insulate a program written for Windows from the actual device
hardware, output is often said to be "written to a device context." This
independence from hardware makes it easy to send output to a printer that looks very
much like screen output. You will learn more about printing in detail in Hour 21,
"Printing."
<HR>
</BLOCKQUOTE>
<P>In order to achieve true hardware independence, you must take a few precautions:
<UL>
<LI>Don't hard-code any dimensions into your program. Larger or smaller screens or
printers will cause hard-coded dimensions to look skewed or distorted.<BR>
<BR>
<LI>Don't assume that Windows is running on a display with a particular resolution.
Making assumptions about video monitors, in particular, is a bad idea. It's a sure
bet that many people don't use your screen resolution or dimensions.<BR>
<BR>
<LI>Don't assume that a certain set of colors are available or are appropriate in
all cases. For example, don't assume the workspace background is always white. A
large number of Windows users have laptops that simulate VGA displays. Other users
often change the available color scheme. The selection of colors used is strictly
up to the user.
</UL>
<P>Device contexts can help by providing much of the information you need to stay
hardware-independent.
<H2><FONT COLOR="#000077"><B>How to Use Device Contexts</B></FONT></H2>
<P>When using Developer Studio, you almost always use an MFC class to gain access
to a device context. The MFC class library offers not just one, but four different
device context classes that can help make your life easier, at least when displaying
output in a Windows program:
<UL>
<LI><TT>CDC</TT>: The base class for all the device context classes<BR>
<BR>
<LI><TT>CPaintDC</TT>: Performs some useful housekeeping functions that are needed
when a window responds to <TT>WM_PAINT</TT><BR>
<BR>
<LI><TT>CClientDC</TT>: Used when a device context will be used only for output to
a window's client area<BR>
<BR>
<LI><TT>CWindowDC</TT>: Used when the entire window can be drawn on
</UL>
<P>There are more MFC device context classes, but they are used for specialized purposes
and are discussed elsewhere in this book. For example, the <TT>CPrinterDC</TT> class
is discussed in Hour 21.
<H3><FONT COLOR="#000077"><B>Wizard Support for Device Contexts</B></FONT></H3>
<P>When you create a class using ClassWizard or AppWizard, often code that uses or
creates a device context is provided automatically. For example, the <TT>OnDraw</TT>
function for a typical view class is provided in Listing 11.1.
<H4><FONT COLOR="#000077">TYPE: Listing 11.1. A typical OnDraw function.</FONT></H4>
<PRE><FONT COLOR="#0066FF"><TT>void CDisplayView::OnDraw(CDC* pDC)</TT>
<TT>{</TT>
<TT> CDocument* pDoc = GetDocument();</TT>
<TT> // TODO: add draw code here</TT>
<TT>}</TT>
</FONT></PRE>
<P>The device context used for the <TT>OnDraw</TT> function is created by the MFC
framework before the <TT>OnDraw</TT> function is called. Because every <TT>OnDraw</TT>
function must display some output, the device context is provided for you automatically,
without the need for you to write any code.</P>
<P>Most functions that need a device context have one provided as a parameter, just
like <TT>OnDraw</TT>. This is one of the ways MFC helps make your code easier to
write and more reliable at the same time.
<H3><FONT COLOR="#000077"><B>Selecting a GDI Object</B></FONT></H3>
<P>One of the most common mistakes made when using device contexts occurs when selecting
a GDI object into a DC. When a device context is created, it contains a set of default
GDI objects, as shown in Figure 11.1.</P>
<P><A NAME="01"></A><A HREF="01.htm" tppabs="http://www.mcp.com/824169600/0-672/0-672-31242-5/ch11/01.htm"><B>Figure 11.1.</B></A> <BR>
<I>A device context created with a collection of default GDI objects.</I></P>
<P>When a new GDI object--for example, a bitmap--is selected into a device context,
the default GDI bitmap is passed as a return value to the caller. This return value
must be saved so that it can be returned to the device context later. Listing 11.2
is an example of selecting a new bitmap into a DC and returning the previously selected
GDI object at the end of the function.
<H4><FONT COLOR="#000077">TYPE: Listing 11.2. Selecting and restoring a GDI object.</FONT></H4>
<PRE><FONT COLOR="#0066FF"><TT>CBitmap bmpHello;</TT>
<TT>bmpHello.LoadBitmap( IDB_HELLO );</TT>
<TT>CBitmap* pbmpOld = dcMem.SelectObject( &bmpHello );</TT>
<TT>if( pbmpOld != NULL )</TT>
<TT>{</TT>
<TT> //</TT>
<TT> // Use the bitmap...</TT>
<TT> //</TT>
<TT> dcMem.SelectObject( pbmpOld );</TT>
<TT>}</TT>
</FONT></PRE>
<P>Notice that the <TT>pbmpOld</TT> value is checked to make sure that it isn't <TT>NULL</TT>.
If the call to <TT>SelectObject</TT> fails, the original bitmap is not returned.
In that case, there's no need to return the original bitmap to the DC, because a
new one was never selected.
<BLOCKQUOTE>
<P>
<HR>
<B> </B><FONT COLOR="#000077"><B>CAUTION:</B></FONT><B> </B>You must restore the
device context to its original state when you are finished with it. If you don't,
resources that you have selected into a device context might not be properly released,
causing your program to consume more and more GDI resources. This type of problem
is known as a resource leak and is very difficult to track down--getting into the
habit of always restoring the original object back into the device context is much
better.
<HR>
</BLOCKQUOTE>
<H2><FONT COLOR="#000077"><B>Stock Objects</B></FONT></H2>
<P>A group of commonly used GDI objects known as stock objects are maintained by
Windows. These objects are much easier to use than objects that you create yourself.
To select a stock object into a device context, use the <TT>SelectStockObject</TT>
function:</P>
<PRE><FONT COLOR="#0066FF"><TT>HPEN hOldPen = pDC->SelectStockObject(BLACK_PEN);</TT>
</FONT></PRE>
<P>Although stock objects do not need to be destroyed after they are used, attempting
to destroy a stock object is not harmful. Table 11.2 describes the 16 stock objects
available for use in your Windows programs.
<H4><FONT COLOR="#000077">Table 11.2. Windows stock objects.</FONT></H4>
<P>
<TABLE BORDER="1">
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -