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

📄 ch12.htm

📁 Visual C++ 的学习资料 Visual C++ 的学习资料
💻 HTM
📖 第 1 页 / 共 4 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>

<HEAD>
	
	<TITLE>Teach Yourself Visual C++&#174; 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++&#174; 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"><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><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, &quot;Device Contexts,&quot;
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 Windows
GDI 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 object
has 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. For
example, rulers and grid lines are often drawn using cosmetic pens. You use geometric
pen 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 extremely
quick and are mapped directly into device units. This makes them useful for drawing
things like frames, borders, grid lines, and other screen objects that should not
be affected by the current device context-mapping mode. Geometric pens require more
CPU power but offer more styles. You can manipulate geometric pens using any of the
available 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 controls
in Windows; before Windows 95 was released, early versions of property pages used
pens to draw simulated &quot;tabs.&quot;
<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. Use
the <TT>CPen</TT> class to create and manage both cosmetic and geometric pens. When
creating 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 because
they are always drawn in device units. Therefore, they are useful where a line must
overlay another view that may be scaled. These basic styles are available for cosmetic
pens:

<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 that
can be done to a pen object; most of the fun occurs when the pen object is selected
into a device context. The <TT>CPen</TT> class provides three constructors: two simple
constructors primarily for cosmetic pens and another extremely flexible constructor
primarily 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 actually
create 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 to
be 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>                      &amp;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. The
second parameter for the constructor is the width; if the pen is cosmetic, it must
be set to 1. The third parameter is a pointer to a <TT>LOGBRUSH</TT> structure. In
Listing 12.1, <TT>lbrGrnHatch</TT> is defined as a diagonally cross-hatched green
brush.</P>
<P>The last two parameters are rarely used; they define a user-supplied pattern for
the 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, whereas
the fifth parameter is an array of <TT>DWORD</TT> values, each used to define the
length 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 were
discussed in Hour 11; they are GDI objects that belong to the operating system. Windows
provides 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 must
create 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 object
as a parameter, as follows:</P>
<PRE><FONT COLOR="#0066FF"><TT>CPen* pOldPen = pDC-&gt;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 drawing
functions can be performed with the device context. The <TT>CDC</TT> class used to
represent device contexts, as you learned in Hour 11, includes these drawing functions
often used with pens:

⌨️ 快捷键说明

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