?? ch12.htm
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><HTML><HEAD> <TITLE>Teach Yourself Visual C++® 5 in 24 Hours -- Hour 12 -- Using Pens and Brushes</TITLE></HEAD><BODY TEXT="#000000" BGCOLOR="#FFFFFF"><CENTER><H1><IMG SRC="../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><CENTER><P><A HREF="../ch11/ch11.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch13/ch13.htm"><IMGSRC="../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><FONT COLOR="#000077">- Hour 12 -<BR>Using Pens and Brushes</FONT></H1></CENTER><P>In this hour you will look at pens and brushes. Specifically, you will learn<UL> <LI>How pens are used to draw lines and geometric shapes in Windows programs<BR> <BR> <LI>How brushes are used to fill areas with colors and patterns<BR> <BR> <LI>MFC class library support that simplifies the use of pens and brushes</UL><P>You also modify the DCTest sample program from Hour 11, "Device Contexts,"to draw a variety of figures using pens and brushes.<H2><FONT COLOR="#000077"><B>What Is a Pen?</B></FONT></H2><P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>A <I>pen</I> is a WindowsGDI object used to draw lines and figures.</P><P>Think of a Windows pen as being like an ink pen at your desk. A Windows pen objecthas three attributes:<UL> <LI><I>Width:</I> Normally one pixel wide, although a pen can be as wide as you like<BR> <BR> <LI><I>Style:</I> Can be any of the pen styles discussed in this chapter<BR> <BR> <LI><I>Color:</I> Can be any Windows color packed into a <TT>COLORREF</TT> structure</UL><P>Programs written for Windows use two types of pens:<UL> <LI><I>Cosmetic pens</I>, which are always drawn in device units, regardless of the current mapping mode.<BR> <BR> <LI><I>Geometric pens</I>, which are drawn in logical units and are affected by the current mapping mode. Geometric pens have more style and drawing options than cosmetic pens.</UL><P>You use a cosmetic pen when you must always draw lines with a fixed size. Forexample, rulers and grid lines are often drawn using cosmetic pens. You use geometricpen lines to reflect the scaling provided by the current mapping mode.<BLOCKQUOTE> <P><HR><B> </B><FONT COLOR="#000077"><B>Just a Minute:</B></FONT><B> </B>A pen is perfect in situations in which you must draw a geometric shape or line. Although you can use a bitmap for complicated images, you easily can draw squares, rectangles, circles, and other basic shapes using GDI objects. <HR></BLOCKQUOTE><P>You can create and use pens with a variety of styles. Cosmetic pens are extremelyquick and are mapped directly into device units. This makes them useful for drawingthings like frames, borders, grid lines, and other screen objects that should notbe affected by the current device context-mapping mode. Geometric pens require moreCPU power but offer more styles. You can manipulate geometric pens using any of theavailable mapping modes.</P><P>Pens are also useful for drawing three-dimensional highlighting or other effects.It's not uncommon for pens and other GDI objects to be used to simulate controlsin Windows; before Windows 95 was released, early versions of property pages usedpens to draw simulated "tabs."<H3><FONT COLOR="#000077"><B>MFC Support for Pens</B></FONT></H3><P>Like other GDI objects, you normally use a pen by creating an MFC object. Usethe <TT>CPen</TT> class to create and manage both cosmetic and geometric pens. Whencreating a pen, you must specify at least three things:<UL> <LI>The pen's style <LI>The pen's width <LI>The pen's color</UL><BLOCKQUOTE> <P><HR><B> </B><FONT COLOR="#000077"><B>Time Saver:</B></FONT><B> </B>The number of styles available for geometric pens is much larger than for cosmetic pens. However, cosmetic pens have much less overhead. You should use cosmetic pens whenever possible. The next few sections discuss the various options available for cosmetic and geometric pens. <HR></BLOCKQUOTE><H3><FONT COLOR="#000077"><B>Using Cosmetic Pens</B></FONT></H3><P>Cosmetic pens are not affected by the current mapping mode's scaling factor becausethey are always drawn in device units. Therefore, they are useful where a line mustoverlay another view that may be scaled. These basic styles are available for cosmeticpens:<UL> <LI><TT>PS_SOLID</TT>: Creates a solid pen.<BR> <BR> <LI><TT>PS_DOT</TT>: Creates a dotted pen. This style is also valid only for pens with a width of one. Wider pens are drawn as <TT>PS_SOLID</TT>.<BR> <BR> <LI><TT>PS_DASH</TT>: Creates a dashed pen. If the pen width is greater than one, the pen is drawn as <TT>PS_SOLID</TT>.<BR> <BR> <LI><TT>PS_DASHDOT</TT>: Creates a pen with alternating dashes and dots. If the pen width is greater than one, a solid pen is drawn instead.<BR> <BR> <LI><TT>PS_DASHDOTDOT</TT>: Creates a pen with alternating dashes and double dots. If the pen width is greater than one, a solid pen is drawn instead.<BR> <BR> <LI><TT>PS_NULL</TT>: Creates a null pen; this pen doesn't draw at all.<BR> <BR> <LI><TT>PS_INSIDEFRAME</TT>: Creates a pen that draws a line inside the frame of closed shapes produced by GDI functions, such as the <TT>Ellipse</TT> and <TT>Rectangle</TT> functions.<BR> <BR> <LI><TT>PS_ALTERNATE</TT>: Can be applied only to cosmetic pens and creates a pen that sets every other pixel.</UL><P>Figure 12.1 shows examples of each of the pen styles.</P><P><A NAME="01"></A><A HREF="01.htm"><B>Figure 12.1.</B></A> <I><BR>Examples of the styles available for pens.</I><H3><FONT COLOR="#000077"><B>Using Geometric Pens</B></FONT></H3><P>Geometric pens can use all the styles available for cosmetic pens except for the<TT>PS_ALTERNATE</TT> style, and they also have access to four additional attributes:<UL> <LI>A pattern used to draw the pen<BR> <BR> <LI>A hatch style used for some types of patterns<BR> <BR> <LI>The type of end cap used to terminate a line<BR> <BR> <LI>A joining style, used when two lines intersect</UL><H3><FONT COLOR="#000077"><B>Using the <TT>CPen</TT> Class</B></FONT></H3><P>The <TT>CPen</TT> class is simple because there really are only a few things thatcan be done to a pen object; most of the fun occurs when the pen object is selectedinto a device context. The <TT>CPen</TT> class provides three constructors: two simpleconstructors primarily for cosmetic pens and another extremely flexible constructorprimarily for geometric pens.</P><P>The first constructor has no arguments:</P><PRE><FONT COLOR="#0066FF"><TT>CPen aGreenPen;</TT><TT>aGreenPen.Create( PS_SOLID, 1, RGB(0,255,0);</TT></FONT></PRE><P>If you use this constructor, use the <TT>Create</TT> member function to actuallycreate the pen and make it ready for use.</P><P>The second constructor provided for <TT>CPen</TT> also is used for cosmetic pens:</P><PRE><FONT COLOR="#0066FF"><TT>CPen penDottedAndRed( PS_DOT, 1, RGB(255,0,0) );</TT></FONT></PRE><P>This version of the constructor accepts three parameters: the pen style, width,and color. In this case, the <TT>CPen</TT> instance is a dotted red pen.</P><P>The third constructor used for <TT>CPen</TT> objects enables any type of pen tobe created. It also uses more parameters, as shown in Listing 12.1.<H4><FONT COLOR="#000077">TYPE: Listing 12.1. Creating a brush using a LOGBRUSH structure.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT>LOGBRUSH lbrGrnHatch;</TT><TT>lbrGrnHatch.lbStyle = BS_HATCHED;</TT><TT>lbrGrnHatch.lbColor = RGB(0,255,0);</TT><TT>lbrGrnHatch.lbHatch = HS_DIAGCROSS;</TT><TT>CPen penGeometric( PS_DOT | PS_GEOMETRIC | PS_ENDCAPROUND,</TT><TT> 50,</TT><TT> &lbrGrnHatch,</TT><TT> 0,</TT><TT> NULL );</TT></FONT></PRE><P>The constructor's first parameter is the pen's style, with the C++ <TT>OR</TT>operator, <TT>|</TT>, used to combine all styles that are applied to the pen. Thesecond parameter for the constructor is the width; if the pen is cosmetic, it mustbe set to 1. The third parameter is a pointer to a <TT>LOGBRUSH</TT> structure. InListing 12.1, <TT>lbrGrnHatch</TT> is defined as a diagonally cross-hatched greenbrush.</P><P>The last two parameters are rarely used; they define a user-supplied pattern forthe pen. These two parameters are used only if the pen is created with the <TT>PS_USERSTYLE</TT>attribute. The fourth parameter is the number of elements in the style array, whereasthe fifth parameter is an array of <TT>DWORD</TT> values, each used to define thelength of a dash or space in the pen's pattern.<H3><FONT COLOR="#000077"><B>Using Stock Pens</B></FONT></H3><P>The simplest pens to use are known as <I>stock objects</I>. Stock objects werediscussed in Hour 11; they are GDI objects that belong to the operating system. Windowsprovides three stock pens:<UL> <LI><TT>BLACK_PEN</TT>: Provides, oddly enough, a black pen<BR> <BR> <LI><TT>WHITE_PEN</TT>: Provides a white pen<BR> <BR> <LI><TT>NULL_PEN</TT>: Provides a null pen and is exactly the same as creating a pen with the <TT>PS_NULL</TT> style</UL><P>Each of these pens is exactly one unit wide. If you need a wider pen, you mustcreate one using the <TT>CPen</TT> class. These pens are used through a <TT>CDC</TT>object by calling the <TT>SelectStockObject</TT> function, passing the stock objectas a parameter, as follows:</P><PRE><FONT COLOR="#0066FF"><TT>CPen* pOldPen = pDC->SelectStockObject( BLACK_PEN );</TT></FONT></PRE><H2><FONT COLOR="#000077"><B>An Example that Draws with Pens</B></FONT></H2><P>After a pen has been selected into a device context, several different drawingfunctions can be performed with the device context. The <TT>CDC</TT> class used torepresent device contexts, as you learned in Hour 11, includes these drawing functionsoften used with pens:
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -