📄 ch08.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 8 -- Adding Flash--Incorporating Graphics, Drawing, and Bitmaps</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="../ch07/ch07.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"
ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch09/ch09.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">- 8 -<BR>
Adding Flash--Incorporating Graphics, Drawing, and Bitmaps</H1>
<H1></H1>
<UL>
<LI><A HREF="#Heading1">Understanding the Graphics Device Interface</A>
<UL>
<LI><A HREF="#Heading2">Device Contexts</A>
<LI><A HREF="#Heading3">Mapping Modes and Coordinate Systems</A>
</UL>
<LI><A HREF="#Heading4">Creating a Graphics Application</A>
<UL>
<LI><A HREF="#Heading5">Generating the Application Shell</A>
<LI><A HREF="#Heading7">Adding the Graphics Capabilities</A>
</UL>
<LI><A HREF="#Heading9">Summary</A>
<LI><A HREF="#Heading10">Q&A</A>
<LI><A HREF="#Heading11">Workshop</A>
<UL>
<LI><A HREF="#Heading12">Quiz</A>
<LI><A HREF="#Heading13">Exercises</A>
</UL>
</UL>
<P>
<HR SIZE="4">
<BR>
You've probably noticed that a large number of applications use graphics and display
images. This adds a certain level of flash and polish to the application. With some
applications, graphics are an integral part of their functionality. Having a good
understanding of what's involved in adding these capabilities to your applications
is a key part of programming for the Windows platform. You've already learned how
you can draw lines and how you can string a series of these lines together to make
a continuous drawing. Today, you're going to go beyond that capacity and learn how
you can add more advanced graphics capabilities to your applications. Today, you
will learn</P>
<P>
<UL>
<LI>How Windows uses a device context to translate drawing instructions into graphics
output.
<P>
<LI>How you can determine the level of control you have over the graphics output
through different mapping modes.
<P>
<LI>How Windows uses pens and brushes to draw different portions of the graphics
image.
<P>
<LI>How you can load and display bitmaps dynamically.
</UL>
<H2><A NAME="Heading1"></A>Understanding the Graphics Device Interface</H2>
<P>The Windows operating system provides you with a couple of levels of abstraction
for creating and using graphics in your applications. During the days of DOS programming,
you needed to exercise a great deal of control over the graphics hardware to draw
any kind of images in an application. This control required an extensive knowledge
and understanding of the various types of graphics cards that users might have in
their computers, along with their options for monitors and resolutions. There were
a few graphics libraries that you could buy for your applications, but overall, it
was fairly strenuous programming to add this capability to your applications.</P>
<P>With Windows, Microsoft has made the job much easier. First, Microsoft provides
you with a virtual graphics device for all of your Windows applications. This virtual
device doesn't change with the hardware but remains the same for all possible graphics
hardware that the user might have. This consistency provides you with the ability
to create whatever kind of graphics you want in your applications because you know
that the task of converting them to something that the hardware understands isn't
your problem.</P>
<P>
<H3><A NAME="Heading2"></A>Device Contexts</H3>
<P>Before you can create any graphics, you must have the device context in which
the graphics will be displayed. The device context contains information about the
system, the application, and the window in which you are drawing any graphics. The
operating system uses the device context to learn in which context a graphic is being
drawn, how much of the area is visible, and where on the screen it is currently located.</P>
<P>When you draw graphics, you always draw them in the context of an application
window. At any time, this window may be full view, minimized, partly hidden, or completely
hidden. This status is not your concern because you draw your graphics on the window
using its device context. Windows keeps track of each device context and uses it
to determine how much and what part of the graphics you draw to actually display
for the user. In essence, the device context you use to display your graphics is
the visual context of the window in which you draw them.</P>
<P>The device context uses two resources to perform most of its drawing and graphics
functions. These two resources are pens and brushes. Much like their real-world counterparts,
pens and brushes perform similar yet different tasks. The device context uses pens
to draw lines and shapes, whereas brushes paint areas of the screen. It's the same
idea as working on paper when you use a pen to draw an outline of an image and then
pick up a paintbrush to fill in the color between the lines.</P>
<P>
<H4>The Device Context Class</H4>
<P>In Visual C++, the MFC device context class (CDC) provides numerous drawing functions
for drawing circles, squares, lines, curves, and so on. All these functions are part
of the device context class because they all use the device context information to
draw on your application windows.</P>
<P>You create a device context class instance with a pointer to the window class
that you want to associate with the device context. This allows the device context
class to place all of the code associated with allocating and freeing a device context
in the class constructor and destructors.</P>
<BLOCKQUOTE>
<P>
<HR>
<STRONG>NOTE:</STRONG> Device context objects, as well as all of the various drawing objects,
are classified as resources in the Windows operating system. The operating system
has only a limited amount of these resources. Although the total number of resources
is large in recent versions of Windows, it is still possible to run out of resources
if an application allocates them and doesn't free them correctly. This loss is known
as a resource leak, and much like a memory leak, it can eventually lock up a user's
system. As a result, it's advisable to create these resources in the functions where
they will be used and then delete them as soon as you are finished with them.<BR>
Following this advised approach to using device contexts and their drawing resources,
you use them almost exclusively as local variables within a single function. The
only real exception is when the device context object is created by Windows and passed
into the event-processing function as an argument.
<HR>
</BLOCKQUOTE>
<H4>The Pen Class</H4>
<P>You have already seen how you can use the pen class, CPen, to specify the color
and width for drawing lines onscreen. CPen is the primary resource tool for drawing
any kind of line onscreen. When you create an instance of the CPen class, you can
specify the line type, color, and thickness. After you create a pen, you can select
it as the current drawing tool for the device context so that it is used for all
of your drawing commands to the device context. To create a new pen, and then select
it as the current drawing pen, you use the following code:</P>
<P>
<PRE>// Create the device context
CDC dc(this);
// Create the pen
CPen lPen(PS_SOLID, 1, RGB(0, 0, 0));
// Select the pen as the current drawing pen
dc.SelectObject(&lPen);
</PRE>
<P>You can use a number of different pen styles. These pen styles all draw different
patterns when drawing lines. Figure 8.1 shows the basic styles that can be used in
your applications with any color.</P>
<P><A HREF="javascript
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -