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

📄 ch12.htm

📁 24小时学会vc++
💻 HTM
📖 第 1 页 / 共 4 页
字号:
	<LI><TT>HS_BDIAGONAL</TT>: Creates a brush with a downward hatch pattern. The lines	used in the hatch pattern run from left to right at 45 degrees.<BR>	<BR>		<LI><TT>HS_CROSS</TT>: Creates a hatch pattern with vertical and horizontal intersecting	lines.<BR>	<BR>		<LI><TT>HS_DIAGCROSS</TT>: Creates a cross-hatch pattern with each line angled at	45 degrees.<BR>	<BR>		<LI><TT>HS_FDIAGONAL</TT>: Creates a brush with an upward hatch pattern. The lines	used in the hatch pattern run from left to right at 45 degrees.<BR>	<BR>		<LI><TT>HS_HORIZONTAL</TT>: Creates a horizontal hatch pattern.<BR>	<BR>		<LI><TT>HS_VERTICAL</TT>: Creates a vertical hatch pattern.</UL><P>Figure 12.5 shows examples of these six hatching styles.</P><P><A NAME="05"></A><A HREF="05.htm"><B>Figure 12.5.</B> </A><I><BR>Examples of brush hatching styles.</I><H3><FONT COLOR="#000077"><B>Using the <TT>CBrush</TT> Class</B></FONT></H3><P>To use a brush in an MFC program, create a <TT>CBrush</TT> object and select itinto a device context. You can create the brush using single-step construction, likethis:</P><PRE><FONT COLOR="#0066FF"><TT>CBrush    brBlack( RGB(0,0,0) );</TT></FONT></PRE><P>Alternatively, use two-step construction, where the brush object is constructedand then explicitly created, like this:</P><PRE><FONT COLOR="#0066FF"><TT>CBrush    brBlack();</TT><TT>brBlack.CreateSolidBrush( RGB(0,0,0) );</TT></FONT></PRE><P>The advantage of using two-step construction is that the function used to createa brush returns <TT>FALSE</TT> if the function fails.</P><P>Unlike pens, which use style bits to determine the type of pen to be created,separate functions are used for different brush types. In two-step construction,you can use three functions to create a brush after you construct the <TT>CBrush</TT>object:<UL>	<LI><TT>CreateSolidBrush</TT>	<LI><TT>CreateHatchBrush</TT>	<LI><TT>CreatePatternBrush</TT></UL><P>Four different constructors are provided for <TT>CBrush</TT>. In addition to thedefault constructor, you can use three constructors to create a specific type ofbrush in one step. The second constructor is used to create a solid brush and takesone <TT>COLORREF</TT> parameter, indicating the color used for the brush.</P><PRE><FONT COLOR="#0066FF"><TT>CBrush    brGreen( RGB(0,0,255) );</TT></FONT></PRE><P>Using this brush is equivalent to using the default constructor and then calling<TT>CreateSolidBrush</TT>.</P><P>The third form of the <TT>CBrush</TT> constructor is used to create a hatchedbrush, and takes the hatching style and hatch color as parameters:</P><PRE><FONT COLOR="#0066FF"><TT>CBrush   brGray( HS_CROSS, RGB(192,192,192) );</TT></FONT></PRE><P>This constructor is equivalent to using the default constructor and then calling<TT>CreateHatchBrush</TT>.</P><P>Use the fourth and final constructor for <TT>CBrush</TT> to create brushes thathave bitmap patterns. You will learn more about bitmaps in Hour 15, &quot;Using Bitmaps&quot;;for now, just remember that a bitmap can be used as a pattern for a brush. The constructortakes a pointer to a <TT>CBitmap</TT> object as a parameter:</P><PRE><FONT COLOR="#0066FF"><TT>CBrush    brArrow( &amp;bmpArrow );</TT></FONT></PRE><P>The <TT>CBitmap</TT> object can be up to 8x8 pixels. If the bitmap is larger,only the upper-left eight pixel squares are used for the brush pattern.<H3><FONT COLOR="#000077"><B>Logical Brushes</B></FONT></H3><P>Logical brushes are defined using the <TT>LOGBRUSH</TT> structure. A logical brushoften is used when specifying how a brush should be constructed. For example, earlierin this hour you used a <TT>LOGBRUSH</TT> structure to specify the characteristicsof a geometric pen. Think of a <TT>LOGBRUSH</TT> as a recipe for a brush that mightbe created: It's not a brush yet, but it might help build a brush in the future.</P><P>The <TT>LOGBRUSH</TT> structure has three data members:<UL>	<LI><TT>lbrStyle</TT> contains the brush style.	<LI><TT>lbrColor</TT> stores a <TT>COLORREF</TT> value for the brush.	<LI><TT>lbrHatch</TT> stores a hatch style if needed.</UL><P>Each of the three <TT>LOGBRUSH</TT> data members corresponds to one of the styleattributes available for brushes, discussed earlier in this hour. To create a logicalbrush, just assign values to the three data members, as with any structure. Listing12.6 uses a logical brush to create a red hatched brush.<H4><FONT COLOR="#000077">TYPE: Listing 12.6. Filling a LOGBRUSH structure.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT>LOGBRUSH    lbrRed;</TT><TT>lbrRed.lbrStyle = BS_HATCH;</TT><TT>lbrRed.lbrColor = RGB(255,0,0);</TT><TT>lbrRed.lbrHatch = HS_CROSS;</TT><TT>CBrush      theRedBrush;</TT><TT>theRedBrush.CreateBrushIndirect( &amp;lbrRed );</TT></FONT></PRE><H3><FONT COLOR="#000077"><B>Using Stock Brushes</B></FONT></H3><P>Just like stock pens discussed earlier in this hour, Windows maintains a set ofstock brushes. Windows provides seven stock brushes:<UL>	<LI><TT>BLACK_BRUSH</TT>: Provides a black brush.	<LI><TT>DKGRAY_BRUSH</TT>: Provides a dark gray brush.	<LI><TT>GRAY_BRUSH</TT>: Provides a gray brush.	<LI><TT>HOLLOW_BRUSH</TT>: Equivalent to <TT>NULL_BRUSH</TT>.	<LI><TT>LTGRAY_BRUSH</TT>: Provides a light gray brush.	<LI><TT>NULL_BRUSH</TT>: Provides a null brush.	<LI><TT>WHITE_BRUSH</TT>: Provides a white brush.</UL><P>As with other stock objects, these brushes are used through a <TT>CDC</TT> objectby calling the <TT>SelectStockObject</TT> function, passing the stock object as aparameter, as follows:</P><PRE><FONT COLOR="#0066FF"><TT>CPen* pOldBrush = pDC-&gt;SelectStockObject( BLACK_BRUSH );</TT></FONT></PRE><H3><FONT COLOR="#000077"><B>Using the Common Color Dialog Box</B></FONT></H3><P>The Windows operating system includes a series of dialog boxes as part of theoperating system. These dialog boxes are guaranteed to be present, and using themrequires just a few lines of code. Use these dialog boxes for common operations whereit's beneficial for all Windows programs to have a similar look and feel. The commondialog boxes shipped with Windows can help you<UL>	<LI>Select a file to be opened	<LI>Choose a font	<LI>Choose a color	<LI>Create a standard Find and Replace dialog box	<LI>Choose options and print to a supported printer</UL><P>To use the Common Color dialog box, just create a <TT>CColorDialog</TT> objectand call <TT>DoModal</TT>, just as with any other dialog box:</P><PRE><FONT COLOR="#0066FF"><TT>CColorDialog    dlgColor;</TT><TT>if( dlgColor.DoModal() )</TT><TT>{ //....</TT></FONT></PRE><P>If <TT>IDOK</TT> is returned from the dialog box, the <TT>CColorDialog::GetColor</TT>function gets the selected color value. The example in the next section uses theCommon Color dialog box to choose a brush color. You will use other common dialogboxes in later hours. (For example, the font-selection dialog box is used in Hour13, &quot;Fonts.&quot;)<H3><FONT COLOR="#000077"><B>Changing the Mapping Mode Dialog Box and <TT>CMapModeDlg</TT>Class</B></FONT></H3><P>As an example of how to use brushes, continue to modify the DCTest project thatyou worked with earlier this hour. The new version of the project displays a coloredellipse on a gray view background. Both the ellipse and background color are filledusing <TT>CBrush</TT> objects. You can change the color of the ellipse using theCommon Color dialog box; as a bonus, the Mapping Mode dialog box color changes tomatch the ellipse.</P><P>Modify the Mapping Mode dialog box to allow the user to choose a color for thedialog box and a brush used for the view. The <TT>CMapModeDlg</TT> class needs twonew variables: a <TT>COLORREF</TT> for the currently selected color, and a <TT>CBrush</TT>object that has been created using the current color. Listing 12.7 contains the changesto the <TT>CMapModeDlg</TT> class declaration. Add the new code in the Dialog Datasection, just after the <TT>AFX_DATA</TT> comments.<H4><FONT COLOR="#000077">TYPE: Listing 12.7. Changes to the CMapModeDlg class declaration.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT>// Dialog Data</TT><TT>    //{{AFX_DATA(CMapModeDlg)</TT><TT>    enum { IDD = IDD_MAP_MODE };</TT><TT>    CString   m_szCombo;</TT><TT>    int       m_cyEllipse;</TT><TT>    int       m_cxEllipse;</TT><TT>    int       m_nPenWidth;</TT><TT>    //}}AFX_DATA</TT><TT>    // Variable added in Hour 12</TT><TT>public:</TT><TT>    COLORREF m_clrChoice;</TT><TT>private:</TT><TT>    CBrush   m_brControl;</TT></FONT></PRE><P>You must change the Mapping Mode dialog box slightly for this example. Removethe pen-width edit control and add a pushbutton control, as shown in Figure 12.6.Use ClassWizard to remove the <TT>m_nPenWidth</TT> member variable from the <TT>CMapModeDlg</TT>class.</P><P><A NAME="06"></A><A HREF="06.htm"><B>Figure 12.6.</B> </A><I><BR>The new version of the Mapping Mode dialog box.</I></P><P>Use the values from Table 12.2 for the new button control.<H4><FONT COLOR="#000077">Table 12.2. Values for the new Color button.</FONT></H4><P><TABLE BORDER="1">	<TR ALIGN="LEFT" rowspan="1">		<TD ALIGN="LEFT"><B>Resource ID</B></TD>		<TD ALIGN="LEFT"><B>Caption</B></TD>		<TD ALIGN="LEFT"><B>Function</B></TD>	</TR>	<TR ALIGN="LEFT" rowspan="1">		<TD ALIGN="LEFT"><TT>IDC_COLOR</TT></TD>		<TD ALIGN="LEFT">&amp;Color...</TD>		<TD ALIGN="LEFT"><TT>CMapModeDlg::OnColor</TT></TD>	</TR></TABLE>Using ClassWizard, add a new message-handling function to the <TT>CMapModeDlg</TT>class named <TT>CMapModeDlg::OnColor</TT>. The source code for <TT>OnColor</TT> isprovided in Listing 12.8.<H4><FONT COLOR="#000077">TYPE: Listing 12.8. The CMapModeDlg::OnColor member function.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT>void CMapModeDlg::OnColor()</TT><TT>{</TT><TT>    CColorDialog    dlgColor;</TT><TT>    if( dlgColor.DoModal() == IDOK )</TT><TT>    {</TT><TT>        m_clrChoice = dlgColor.GetColor();</TT><TT>        // If the brush already exists, delete the current</TT><TT>        // GDI object before calling CreateSolidBrush</TT><TT>        if( m_brControl.Detach() )</TT><TT>            m_brControl.DeleteObject();</TT><TT>        m_brControl.CreateSolidBrush( m_clrChoice );</TT><TT>        InvalidateRect( NULL );</TT><TT>    }</TT><TT>}</TT></FONT></PRE><P>The <TT>OnColor</TT> function creates a Common Color dialog box and displays itusing <TT>DoModal</TT>. If the user selects a new color, the color is collected andthe brush is updated. If the brush has previously been created, the <TT>Detach</TT>and <TT>DeleteObject</TT> functions must be called to destroy the current brush beforecreating a new brush.<H3><FONT COLOR="#000077"><B>Handling the <TT>WM_CTLCOLOR</TT> Message</B></FONT></H3><P>Before displaying any control or dialog box, Windows asks for the control's colorby sending a <TT>WM_CTLCOLOR</TT> message to the owner of the control. To specifya color to be used for the control or dialog box, return a solid brush containingthe color in response to this message, as shown in Listing 12.9. The <TT>m_brControl</TT>brush is a class member variable because it must survive for the life of the control.<H4><FONT COLOR="#000077">TYPE: Listing 12.9. Changing the color of a dialog boxby handling WM_CTLCOLOR.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT>HBRUSH CMapModeDlg::OnCtlColor(CDC* pDC,CWnd* pWnd,UINT nCtlColor)</TT><TT>{</TT><TT>    if( nCtlColor == CTLCOLOR_DLG || nCtlColor == CTLCOLOR_STATIC )</TT><TT>    {</TT><TT>        pDC-&gt;SetBkMode( TRANSPARENT );</TT><TT>        return (HBRUSH)m_brControl.GetSafeHandle();</TT><TT>    }</TT><TT>    else</TT><TT>        return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);</TT><TT>}</TT></FONT></PRE><BLOCKQUOTE>	<P><HR><B> </B><FONT COLOR="#000077"><B>Time Saver:</B></FONT><B> </B>The easiest way to	deal with colored dialog boxes is shown in Listing 12.9, where the text-drawing mode	is set to transparent by calling <TT>SetBkMode</TT>. If this line is commented out,	you will see that the static text has colored areas around each color. By setting	the drawing mode to transparent, the text is drawn without including the text background	color, allowing the dialog box color to show through. <HR>

⌨️ 快捷键说明

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