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

📄 ch12.htm

📁 Visual C++ 的学习资料 Visual C++ 的学习资料
💻 HTM
📖 第 1 页 / 共 4 页
字号:

</BLOCKQUOTE>

<P>The <TT>GetSafeHandle</TT> function is used with all GDI objects to return a handle
to the underlying object. A <TT>CBrush</TT> object returns an <TT>HBRUSH</TT> handle;
a <TT>CPen</TT> object returns an <TT>HPEN</TT>, and so on.</P>
<P>The <TT>WM_CTLCOLOR</TT> message is sent for every control type found in the dialog
box. It's possible to set different colors for each control type by testing for the
values found in Table 12.3. If a brush is not returned, determine the return value
by calling <TT>CDialog::OnCtlColor</TT>.
<H4><FONT COLOR="#000077">Table 12.3. Control-type message return value.</FONT></H4>
<P>
<TABLE BORDER="1">
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><B>Control Message Value</B></TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><B>Control Type</B></TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>CTLCOLOR_BTN</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP">Button control</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>CTLCOLOR_DLG</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP">Dialog box</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>CTLCOLOR_EDIT</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP">Edit control</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>CTLCOLOR_LISTBOX</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP">List box control</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>CTLCOLOR_MSGBOX</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP">Message box</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>CTLCOLOR_SCROLLBAR</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP">Scrollbar control</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>CTLCOLOR_STATIC</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP">Static control</TD>
	</TR>
</TABLE>

<H3><FONT COLOR="#000077"><B>Updating the <TT>CDCTestView</TT> Class</B></FONT></H3>
<P>The <TT>CDCTestView</TT> class must store the color and brush selected by the
user to color the ellipse and Mapping Mode dialog box. One new variable is added
to the attributes section of the <TT>CDCTestView</TT> class, as shown in Listing
12.10: The <TT>m_clrChoice</TT> variable stores the currently selected color for
the ellipse.
<H4><FONT COLOR="#000077">TYPE: Listing 12.10. Changes to the CDCTestView class declaration.</FONT></H4>
<PRE><FONT COLOR="#0066FF"><TT>// Attributes</TT>
<TT>private:</TT>
<TT>    // Variables added for Hour 11</TT>
<TT>    CMap&lt; int, int, CString, CString &gt; m_map;</TT>
<TT>    int     m_nMapMode;</TT>
<TT>    // Variables added for Hour 12 - pens</TT>
<TT>    int     m_cxEllipse;</TT>
<TT>    int     m_cyEllipse;</TT>
<TT>    // Variable added for Hour 12 - brushes</TT>
<TT>    COLORREF m_clrChoice;</TT>
</FONT></PRE>
<H3><FONT COLOR="#000077"><B>Changes to <TT>CDCTestView</TT> Member Functions</B></FONT></H3>
<P>To update the DCTest project to use brushes instead of pens, you must make four
basic changes to the <TT>CDCTestView</TT> member functions:

<UL>
	<LI>All references to <TT>m_nPenWidths</TT> must be removed.
	<LI>The new variable, <TT>m_clrChoice</TT>, must be initialized in the constructor.
	<LI>The <TT>OnViewMapMode</TT> function must update the <TT>m_clrChoice</TT> variable
	if the user changes the color.
	<LI>The <TT>OnDraw</TT> function must be changed to use brushes instead of pens.
</UL>

<P>Edit the constructor for <TT>CDCTestView</TT> so it looks like the source code
provided in Listing 12.11. The <TT>m_nPenWidth</TT> variable has been removed, and
one line has been added to initialize the <TT>m_clrChoice</TT> variable.
<H4><FONT COLOR="#000077">TYPE: Listing 12.11. The CDCTestView constructor.</FONT></H4>
<PRE><FONT COLOR="#0066FF"><TT>CDCTestView::CDCTestView()</TT>
<TT>{</TT>
<TT>    m_nMapMode = MM_TEXT;</TT>
<TT>    m_map.SetAt( MM_ANISOTROPIC, &quot;MM_ANISOTROPIC&quot; );</TT>
<TT>    m_map.SetAt( MM_HIENGLISH, &quot;MM_HIENGLISH&quot; );</TT>
<TT>    m_map.SetAt( MM_HIMETRIC, &quot;MM_HIMETRIC&quot; );</TT>
<TT>    m_map.SetAt( MM_ISOTROPIC, &quot;MM_ISOTROPIC&quot; );</TT>
<TT>    m_map.SetAt( MM_LOENGLISH, &quot;MM_LOENGLISH&quot; );</TT>
<TT>    m_map.SetAt( MM_LOMETRIC, &quot;MM_LOMETRIC&quot; );</TT>
<TT>    m_map.SetAt( MM_TEXT, &quot;MM_TEXT&quot; );</TT>
<TT>    m_map.SetAt( MM_TWIPS, &quot;MM_TWIPS&quot; );</TT>
<TT>    // The next two lines are added for Hour 12 - pens</TT>
<TT>    m_cxEllipse = 100;</TT>
<TT>    m_cyEllipse = 200;</TT>
<TT>    // The next line is added for Hour 12 - brushes</TT>
<TT>    m_clrChoice = RGB(0,0,0);</TT>
<TT>}</TT>
</FONT></PRE>
<P>Modify the <TT>CDCTestView::OnViewMapMode</TT> function as shown in Listing 12.12.
The code in this listing removes all references to the <TT>m_nPenWidth</TT> variable,
and the function now tracks the color selected by the user. A total of two lines
have been removed and one line added to the existing function.
<H4><FONT COLOR="#000077">TYPE: Listing 12.12. The OnViewMapMode function.</FONT></H4>
<PRE><FONT COLOR="#0066FF"><TT>void CDCTestView::OnViewMapMode()</TT>
<TT>{</TT>
<TT>    CMapModeDlg dlg;</TT>
<TT>    // The next two lines are added for Hour 12 - pens</TT>
<TT>    dlg.m_cxEllipse = m_cxEllipse;</TT>
<TT>    dlg.m_cyEllipse = m_cyEllipse;</TT>
<TT>    // The next line is added for Hour 12 - brushes</TT>
<TT>    dlg.m_clrChoice = m_clrChoice;</TT>
<TT>    if( dlg.DoModal() == IDOK )</TT>
<TT>    {</TT>
<TT>        // The next two lines are added for Hour 12 - pens</TT>
<TT>        m_cxEllipse = dlg.m_cxEllipse;</TT>
<TT>        m_cyEllipse = dlg.m_cyEllipse;</TT>
<TT>        // The next line is added for Hour 12 - brushes</TT>
<TT>        m_clrChoice = dlg.m_clrChoice;</TT>
<TT>        POSITION    pos;</TT>
<TT>        pos = m_map.GetStartPosition();</TT>
<TT>        while( pos != NULL )</TT>
<TT>        {</TT>
<TT>            CString szMapMode;</TT>
<TT>            int     nMapMode;</TT>
<TT>            m_map.GetNextAssoc( pos, nMapMode, szMapMode );</TT>
<TT>            if( szMapMode == dlg.m_szCombo )</TT>
<TT>            {</TT>
<TT>                m_nMapMode = nMapMode;</TT>
<TT>                break;</TT>
<TT>            }</TT>
<TT>        }</TT>
<TT>        InvalidateRect( NULL );</TT>
<TT>    }</TT>
<TT>}</TT>
</FONT></PRE>
<P>Modify the <TT>CDCTestView::OnDraw</TT> function as shown in Listing 12.13. The
new version of <TT>OnDraw</TT> uses a <TT>CBrush</TT> object to fill the view window
with a red brush. Another <TT>CBrush</TT> object is used to draw an ellipse in the
center of the view using a user-defined color to fill the figure.
<H4><FONT COLOR="#000077">TYPE: Listing 12.13. The OnDraw member function modified
to use brushes.</FONT></H4>
<PRE><FONT COLOR="#0066FF"><TT>void CDCTestView::OnDraw(CDC* pDC)</TT>
<TT>{</TT>
<TT>    CRect rcClient;</TT>
<TT>    GetClientRect( rcClient );</TT>
<TT>    pDC-&gt;DPtoLP( rcClient );</TT>
<TT>    CBrush  brBackground( RGB( 255, 255, 100 ) );</TT>
<TT>    pDC-&gt;FillRect( rcClient, &amp;brBackground );</TT>
<TT>    CPoint  ptCenter( rcClient.Width()/2, rcClient.Height()/2 );</TT>
<TT>    CRect   rcEllipse( ptCenter.x - ( m_cxEllipse/2 ),</TT>
<TT>                       ptCenter.y - ( m_cyEllipse/2 ),</TT>
<TT>                       ptCenter.x + ( m_cxEllipse/2 ),</TT>
<TT>                       ptCenter.y + ( m_cyEllipse/2 ) );</TT>
<TT>    CBrush  brEllipse( m_clrChoice );</TT>
<TT>    CBrush* pOldBrush = pDC-&gt;SelectObject( &amp;brEllipse );</TT>
<TT>    pDC-&gt;Ellipse( rcEllipse );</TT>
<TT>    pDC-&gt;SelectObject( &amp;pOldBrush );</TT>
<TT>}</TT>
</FONT></PRE>
<P>Compile and run the DCTest project, and experiment by changing the values in the
Mapping Mode dialog box. Also experiment with different colors for the dialog box
and ellipse by clicking the Color button. Figure 12.7 shows an example of the DCTest
project running.</P>
<P><A NAME="07"></A><A HREF="07.htm"><B>Figure 12.7.</B></A> <I><BR>
The DCTest example after adding brush GDI objects.</I>
<H2><FONT COLOR="#000077"><B>Summary</B></FONT></H2>
<P>In this hour you learned about two GDI objects--pens and brushes--and how they
are used to draw figures and fill shapes in Windows programs. You also learned about
the MFC classes <TT>CPen</TT> and <TT>CBrush</TT> that are used to manage pen and
brush objects. Finally, you modified the DCTest program to use the MFC <TT>CPen</TT>
and <TT>CBrush</TT> classes.
<H2><FONT COLOR="#000077"><B>Q&amp;A</B></FONT></H2>

<DL>
	<DD><B>Q Can a pen and a brush be used at the same time?</B><BR>
	<BR>
	<B>A</B> Yes, in fact, you are always drawing with both a pen and a brush. Earlier
	in this hour the ellipse and rectangle were drawn with specific pens; the shapes
	weren't filled in because the default brush for a device context is the hollow, or
	null brush. Because this brush has no effect, it seems as though no brush is being
	used.<BR>
	<BR>
	<B>Q Why aren't pushbuttons affected by handling <TT>WM_CTLCOLORBTN</TT>?</B><BR>
	<BR>
	<B>A</B> The <TT>WM_CTLCOLORBTN</TT> message will affect radio buttons and check
	boxes, but not pushbuttons. To change the color of a pushbutton you must make it
	an owner-drawn pushbutton and take over responsibility for drawing every aspect of
	the button.
</DL>

<H2><FONT COLOR="#000077"><B>Workshop</B></FONT></H2>
<P>The Workshop is designed to help you anticipate possible questions, review what
you've learned, and begin thinking ahead to putting your knowledge into practice.
The answers to the quiz are in Appendix B, &quot;Quiz Answers.&quot;
<H3><FONT COLOR="#000077"><B>Quiz</B></FONT></H3>

<DL>
	<DD>1. What are the three attributes of a pen?<BR>
	<BR>
	2. What are the two types of pens?<BR>
	<BR>
	3. What MFC class is used to manage pens?<BR>
	<BR>
	4. What stock pens are maintained by Windows?<BR>
	<BR>
	5. What styles are available for cosmetic pens?<BR>
	<BR>
	6. What styles are available for geometric pens?<BR>
	<BR>
	7. What are the four types of brushes?<BR>
	<BR>
	8. What stock brushes are maintained by Windows?<BR>
	<BR>
	9. What MFC class is used to manage brushes?<BR>
	<BR>
	10. What function is used to draw a circle?
</DL>

<H3><FONT COLOR="#000077"><B>Exercises</B></FONT></H3>

<DL>
	<DD>1. Modify the DCTest example, and change the edit control color to red.<BR>
	<BR>
	2. Modify the DCTest example so that the ellipse is drawn with a red outline.
</DL>

<CENTER>
<P>
<HR>
<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> <BR>
<BR>
<BR>
<IMG SRC="../button/corp.gif" WIDTH="284" HEIGHT="45" ALIGN="BOTTOM" ALT="Macmillan Computer Publishing USA"
BORDER="0"></P>

<P>&#169; <A HREF="../copy.htm">Copyright</A>, Macmillan Computer Publishing. All
rights reserved.
</CENTER>


</BODY>

</HTML>

⌨️ 快捷键说明

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