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

📄 ch11.htm

📁 Visual C++ 的学习资料 Visual C++ 的学习资料
💻 HTM
📖 第 1 页 / 共 3 页
字号:
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><B>Stock Object</B></TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><B>Description</B></TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>BLACK_BRUSH</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP">A black brush.</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>DKGRAY_BRUSH</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP">A dark gray brush.</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>GRAY_BRUSH</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP">A gray brush.</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>HOLLOW_BRUSH</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP">This is the same as <TT>NULL_BRUSH</TT>.</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>LTGRAY_BRUSH</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP">A light gray brush.</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>NULL_BRUSH</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP">A null brush, which is a brush that has no effect.</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>WHITE_BRUSH</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP">A white brush.</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>BLACK_PEN</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP">A black pen.</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>NULL_PEN</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP">A null pen, which is a pen that has no effect.</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>WHITE_PEN</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP">A white pen.</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>ANSI_FIXED_FONT</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP">A fixed-pitch system font.</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>ANSI_VAR_FONT</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP">A variable-pitch system font.</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>DEVICE_DEFAULT_FONT</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP">A device-dependent font. This stock object is available only on Windows NT.</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>DEFAULT_GUI_FONT</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP">The default font for user interface objects such as menus and dialog boxes.</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>OEM_FIXED_FONT</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP">The OEM dependent fixed-pitch font.</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>SYSTEM_FONT</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP">The system font.</TD>
	</TR>
</TABLE>
</P>
<P>When a device context is created, it already has several stock objects selected.
For example, the default pen is a stock object <TT>BLACK_PEN</TT>, and the default
brush is the stock object <TT>NULL_BRUSH</TT>.


<BLOCKQUOTE>
	<P>
<HR>
<B> </B><FONT COLOR="#000077"><B>Time Saver:</B></FONT><B> </B>Stock objects can
	be used to restore a device context to its original state before the device context
	is deleted. Selecting a stock object into a device context will deselect a currently
	selected GDI object. This will prevent a memory leak when the device context is released:</P>
	<PRE><FONT COLOR="#0066FF"><TT>hBrNew = CreateSolidBrush(RGB(255,255,0));</TT>
<TT>SelectObject(hDC, hBrNew);  // Original handle not stored</TT>
<TT>.</TT>
<TT>[Use the device context]</TT>
<TT>.</TT>
<TT>SelectObject(hDC, GetStockObject(BLACK_BRUSH));</TT></FONT></PRE>
	<P>This technique is commonly used when it is not practical to store the handle to
	the original GDI object. 
<HR>


</BLOCKQUOTE>

<H2><FONT COLOR="#000077"><B>DCTest: A Device Context Example</B></FONT></H2>
<P>To demonstrate some basic ideas about how device contexts can be used, create
a sample SDI program named DCTest. To help reduce the amount of typing required for
GDI examples, the DCTest program will be used through Hour 14, &quot;Icons and Cursors.&quot;</P>
<P>DCTest is an SDI program that displays some information about the current device
context, its map mode, and information about the default font. It will be possible
to change the view's map mode using a dialog box.</P>
<P>To begin building the DCTest example, create an SDI program named DCTest using
MFC AppWizard. Feel free to experiment with options offered by AppWizard because
none of the options will affect the sample project.
<H3><FONT COLOR="#000077"><B>Creating the Mapping Mode Dialog Box</B></FONT></H3>
<P>Using Figure 11.2 as a guide, create a dialog box that changes the map mode for
the view display. Give the dialog box a resource ID of <TT>IDD_MAP_MODE</TT>.</P>
<P><A NAME="02"></A><A HREF="02.htm" tppabs="http://www.mcp.com/824169600/0-672/0-672-31242-5/ch11/02.htm"><B>Figure 11.2.</B> </A><I><BR>
The <TT>IDD_MAP_MODE</TT> dialog box in the resource editor.</I></P>
<P>The dialog box contains one new control, a drop-down combo box with a resource
ID of <TT>IDC_COMBO</TT>.</P>
<P>Using ClassWizard, add a class named <TT>CMapModeDlg</TT> to handle the <TT>IDD_MAP_MODE</TT>
dialog box. Add a <TT>CString</TT> variable to the class as shown in Table 11.3.
<H4><FONT COLOR="#000077">Table 11.3. New CString member variable for the CMapModeDlg
class.</FONT></H4>
<P>
<TABLE BORDER="1">
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><B>Resource ID</B></TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><B>Name</B></TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><B>Category</B></TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><B>Variable Type</B></TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>IDC_COMBO</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>m_szCombo</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP">Value</TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>CString</TT></TD>
	</TR>
</TABLE>
</P>
<P>That's the start of the DCTest project. It doesn't do much now, but you will add
source code to the example as new device context topics are discussed for the remainder
of this hour.
<H3><FONT COLOR="#000077"><B>Setting Mapping Modes</B></FONT></H3>
<P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>The <I>mapping mode</I> is
the current coordinate system used by a device context.</P>
<P>In Windows, you use mapping modes to define the size and direction of units used
in drawing functions. As a Windows programmer, several different coordinate systems
are available to you. Mapping modes can use physical or logical dimensions, and they
can start at the top, at the bottom, or at an arbitrary point on the screen.</P>
<P>A total of eight different mapping modes are available in Windows. You can retrieve
the current mapping mode used by a device context using the <TT>GetMapMode</TT> function,
and set a new mapping mode using <TT>SetMapMode</TT>. Here are the available mapping
modes:

<UL>
	<LI><TT>MM_ANISOTROPIC</TT>: Uses a viewport to scale the logical units to an application
	defined value. The <TT>SetWindowExt</TT> and <TT>SetViewportExt</TT> member functions
	are used to change the units, orientation, and scaling.<BR>
	<BR>
	
	<LI><TT>MM_HIENGLISH</TT>: Each logical unit is converted to a physical value of
	0.001 inch. Positive x is to the right; positive y is up.<BR>
	<BR>
	
	<LI><TT>MM_HIMETRIC</TT>: Each logical unit is converted to a physical value of 0.01
	millimeter. Positive x is to the right; positive y is up.<BR>
	<BR>
	
	<LI><TT>MM_ISOTROPIC</TT>: Similar to <TT>MM_ANISOTROPIC</TT>, where logical units
	are converted to arbitrary units with equally scaled axes. This means that 1 unit
	on the x-axis is always equal to 1 unit on the y-axis. Use the <TT>SetWindowExt</TT>
	and <TT>SetViewportExt</TT> member functions to specify the units and orientation
	of the axes.<BR>
	<BR>
	
	<LI><TT>MM_LOENGLISH</TT>: Each logical unit is converted to a physical value of
	0.01 inch. Positive x is to the right; positive y is up.<BR>
	<BR>
	
	<LI><TT>MM_LOMETRIC</TT>: Each logical unit is converted to a physical value of 0.1
	millimeter. Positive x is to the right; positive y is up.<BR>
	<BR>
	
	<LI><TT>MM_TEXT</TT>: Each logical unit is converted to 1 device pixel. Positive
	x is to the right; positive y is down.<BR>
	<BR>
	
	<LI><TT>MM_TWIPS</TT>: Each logical unit is converted to 1/20 of a point. Because
	a point is 1/72 inch, a <I>twip</I> is 1/1440 inch. This mapping mode is useful when
	sending output to a printer. Positive x is to the right; positive y is up.
</UL>

<H4><FONT COLOR="#000077">Modifying the Mapping Mode Dialog Box</FONT></H4>
<P>Modify the combo box in the <TT>IDD_MAPMODE</TT> dialog box so that it can be
used to track the current mapping mode. You can set the contents of the combo box
in the resource editor, just as you set other combo box attributes. Because this
combo box contains a fixed list of items, adding them in the resource editor is easier
than in <TT>OnInitDialog</TT>. Add the items from the following list to the combo
box:</P>
<PRE><FONT COLOR="#0066FF"><TT>MM_ANISOTROPIC</TT>
<TT>MM_HIENGLISH</TT>
<TT>MM_HIMETRIC</TT>
<TT>MM_ISOTROPIC</TT>
<TT>MM_LOENGLISH</TT>
<TT>MM_LOMETRIC</TT>
<TT>MM_TEXT</TT>
<TT>MM_TWIPS</TT>
</FONT></PRE>
<H4><FONT COLOR="#000077">Adding a Menu Item</FONT></H4>
<P>Use the values from Table 11.4 to add a menu item and a message-handling function
to the <TT>CDCTestView</TT> class. Unlike menu-handling functions you have seen earlier,
the view class must handle this menu selection because the dialog box changes data
that is interesting only for the view class.
<H4><FONT COLOR="#000077">Table 11.4. New member functions for the CDCTestView class.</FONT></H4>
<P>
<TABLE BORDER="1">
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><B>Menu ID</B></TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><B>Caption</B></TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><B>Event</B></TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><B>Function Name</B></TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>ID_VIEW_MAP_MODE</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP">Map Mode...</TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>COMMAND</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>OnViewMapMode</TT></TD>
	</TR>
</TABLE>
</P>
<P>Listing 11.3 contains the source code for the <TT>OnViewMapMode</TT> function,
which handles the message sent when the new menu item is selected. If OK is clicked,
the new map mode is calculated based on the value contained in the combo box. The
view rectangle is invalidated, which causes the view to be redrawn.
<H4><FONT COLOR="#000077">TYPE: Listing 11.3. The CDCTestView::OnViewMapMode function.</FONT></H4>
<PRE><FONT COLOR="#0066FF"><TT>void CDCTestView::OnViewMapMode()</TT>
<TT>{</TT>
<TT>    CMapModeDlg dlg;</TT>

<TT>    if( dlg.DoModal() == IDOK )</TT>
<TT>    {</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>Add an <TT>#include</TT> statement to the <TT>DCTestView.cpp</TT> file so the
<TT>CDCTestView</TT> class can have access to the <TT>CMapModeDlg</TT> class declaration.
Add the following line near the top of the <TT>DCTestView.cpp</TT> file, just after
the other <TT>#include</TT> statements:</P>
<PRE><FONT COLOR="#0066FF"><TT>#include &quot;MapModeDlg.h&quot;</TT>
</FONT></PRE>
<H3><FONT COLOR="#000077"><B>Collecting Information from a Device Context</B></FONT></H3>
<P>The <TT>CDC</TT> class has two member functions that are commonly used to collect
information:

<UL>
	<LI><TT>GetDeviceCaps</TT>, used to return information about the physical device
	that is associated with a device context.<BR>
	<BR>
	
	<LI><TT>GetTextMetrics</TT>, used to retrieve information about the currently selected
	font.
</UL>

<H4><FONT COLOR="#000077">Using the GetDeviceCaps Function</FONT></H4>
<P>One common use for <TT>GetDeviceCaps</TT> is to determine the number of pixels
per logical inch for the device associated with the DC. To retrieve the number of
pixels per logical inch in the horizontal direction (also known as the x-axis), call
<TT>GetDeviceCaps</TT> and pass an index value of <TT>LOGPIXELSX</TT> as a parameter:</P>
<PRE><FONT COLOR="#0066FF"><TT>int cxLog = pDC-&gt;GetDeviceCaps( LOGPIXELSX );</TT>
</FONT></PRE>

⌨️ 快捷键说明

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