📄 ch12.htm
字号:
</BLOCKQUOTE><P>The <TT>GetSafeHandle</TT> function is used with all GDI objects to return a handleto 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 dialogbox. It's possible to set different colors for each control type by testing for thevalues found in Table 12.3. If a brush is not returned, determine the return valueby 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 theuser to color the ellipse and Mapping Mode dialog box. One new variable is addedto the attributes section of the <TT>CDCTestView</TT> class, as shown in Listing12.10: The <TT>m_clrChoice</TT> variable stores the currently selected color forthe 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< int, int, CString, CString > 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 fourbasic 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 codeprovided in Listing 12.11. The <TT>m_nPenWidth</TT> variable has been removed, andone 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, "MM_ANISOTROPIC" );</TT><TT> m_map.SetAt( MM_HIENGLISH, "MM_HIENGLISH" );</TT><TT> m_map.SetAt( MM_HIMETRIC, "MM_HIMETRIC" );</TT><TT> m_map.SetAt( MM_ISOTROPIC, "MM_ISOTROPIC" );</TT><TT> m_map.SetAt( MM_LOENGLISH, "MM_LOENGLISH" );</TT><TT> m_map.SetAt( MM_LOMETRIC, "MM_LOMETRIC" );</TT><TT> m_map.SetAt( MM_TEXT, "MM_TEXT" );</TT><TT> m_map.SetAt( MM_TWIPS, "MM_TWIPS" );</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 lineshave 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. Thenew version of <TT>OnDraw</TT> uses a <TT>CBrush</TT> object to fill the view windowwith a red brush. Another <TT>CBrush</TT> object is used to draw an ellipse in thecenter of the view using a user-defined color to fill the figure.<H4><FONT COLOR="#000077">TYPE: Listing 12.13. The OnDraw member function modifiedto 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->DPtoLP( rcClient );</TT><TT> CBrush brBackground( RGB( 255, 255, 100 ) );</TT><TT> pDC->FillRect( rcClient, &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->SelectObject( &brEllipse );</TT><TT> pDC->Ellipse( rcEllipse );</TT><TT> pDC->SelectObject( &pOldBrush );</TT><TT>}</TT></FONT></PRE><P>Compile and run the DCTest project, and experiment by changing the values in theMapping Mode dialog box. Also experiment with different colors for the dialog boxand ellipse by clicking the Color button. Figure 12.7 shows an example of the DCTestproject 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 theyare used to draw figures and fill shapes in Windows programs. You also learned aboutthe MFC classes <TT>CPen</TT> and <TT>CBrush</TT> that are used to manage pen andbrush 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&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 whatyou've learned, and begin thinking ahead to putting your knowledge into practice.The answers to the quiz are in Appendix B, "Quiz Answers."<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"><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> <BR><BR><BR><IMG SRC="../button/corp.gif" WIDTH="284" HEIGHT="45" ALIGN="BOTTOM" ALT="Macmillan Computer Publishing USA"BORDER="0"></P><P>© <A HREF="../copy.htm">Copyright</A>, Macmillan Computer Publishing. Allrights reserved.</CENTER></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -