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

📄 ch24.htm

📁 Visual C++ 的学习资料 Visual C++ 的学习资料
💻 HTM
📖 第 1 页 / 共 3 页
字号:
function draws an ellipse inside the bounding rectangle. This is extremely useful
if you happen to be creating an ellipse control. However, because OleEdit must look
like an edit control, you must change the <TT>OnDraw</TT> function. The changes to
<TT>OnDraw</TT> required for the OleEdit control are provided in Listing 24.1.
<H4><FONT COLOR="#000077">TYPE: Listing 24.1. The OnDraw function used by COleEditCtrl.</FONT></H4>
<PRE><FONT COLOR="#0066FF"><TT>void COleEditCtrl::OnDraw(</TT>
<TT>     CDC* pdc, const CRect&amp; rcBounds, const CRect&amp; rcInvalid)</TT>
<TT>{</TT>
<TT>    COLORREF  clrBackground = TranslateColor(GetBackColor());</TT>
<TT>    CBrush*   pOldBrush;</TT>
<TT>    CBrush    brBackground( clrBackground );</TT>
<TT>    pdc-&gt;FillRect( rcBounds, &amp;brBackground );</TT>
<TT>    pOldBrush = pdc-&gt;SelectObject( &amp;brBackground );</TT>
<TT>    pdc-&gt;SelectObject( pOldBrush );</TT>
<TT>    DoSuperclassPaint(pdc, rcBounds);</TT>
<TT>    CRect rc(rcBounds);</TT>
<TT>    pdc-&gt;DrawEdge( rc, EDGE_SUNKEN, BF_RECT );</TT>
<TT>}</TT>
</FONT></PRE>
<P>The code provided in Listing 24.1 does three things. First, it fills the control's
bounding rectangle with the ambient background color. Next, it calls <TT>DoSuperclassPaint</TT>
to give the edit control a chance to attempt to draw itself properly. Finally, it
draws a three-dimensional edge along the control's bounding rectangle.
<H3><FONT COLOR="#000077"><B>Defining Properties for OleEdit</B></FONT></H3>
<P>OleEdit uses four properties: the <TT>Font</TT> and <TT>Text</TT> stock properties
and the <TT>fTextAllowed</TT> and <TT>fNumbersAllowed</TT> custom properties. Using
ClassWizard, add the stock properties for the OleEdit control. Select the Automation
tab, and click the Add Property button. Fill in the dialog box using the values provided
in Table 24.1.
<H4><FONT COLOR="#000077">Table 24.1. Stock properties for the OleEdit control.</FONT></H4>
<P>
<TABLE BORDER="1">
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><B>External Name</B></TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><B>Implementation</B></TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>Font</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP">Stock</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>Text</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP">Stock</TD>
	</TR>
</TABLE>
</P>
<P>Use ClassWizard to add a custom property name <TT>fNumbersAllowed</TT> to the
OleEdit project. Click the Add Property button and use the values provided in Table
24.2.
<H4><FONT COLOR="#000077">Table 24.2. The fNumbersAllowed custom property for the
OleEdit control.</FONT></H4>
<P>
<TABLE BORDER="1">
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><B>Control</B></TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><B>Value</B></TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP">External name</TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>fNumbersAllowed</TT></TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP">Type</TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>BOOL</TT></TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP">Member variable name</TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>m_fNumbersAllowed</TT></TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP">Notification function</TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>OnFNumbersAllowedChanged</TT></TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP">Implementation</TD>
		<TD ALIGN="LEFT" VALIGN="TOP">Member variable</TD>
	</TR>
</TABLE>
</P>
<P>Use ClassWizard to add the <TT>fTextAllowed</TT> property, following the steps
used to add the previous properties. Use the values provided in Table 24.3.
<H4><FONT COLOR="#000077">Table 24.3. The fTextAllowed custom property for the OleEdit
control.</FONT></H4>
<P>
<TABLE BORDER="1">
	<TR ALIGN="LEFT" rowspan="1">
		<TD HEIGHT="27" ALIGN="LEFT" VALIGN="TOP"><B>Control</B></TD>
		<TD HEIGHT="27" ALIGN="LEFT" VALIGN="TOP"><B>Value</B></TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP">External name</TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>fTextAllowed</TT></TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP">Type</TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>BOOL</TT></TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP">Variable name</TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>m_fTextAllowed</TT></TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP">Notification function</TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>OnFTextAllowedChanged</TT></TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP">Implementation</TD>
		<TD ALIGN="LEFT" VALIGN="TOP">Member variable</TD>
	</TR>
</TABLE>
</P>
<P>Modify the <TT>COleEditCtrl</TT> class constructor to contain code that initializes
the custom properties added in the previous steps. The modified constructor is shown
in Listing 24.2.
<H4><FONT COLOR="#000077">TYPE: Listing 24.2. Modifications to the COleEditCtrl constructor.</FONT></H4>
<PRE><FONT COLOR="#0066FF"><TT>ColeEditCtrl::COleEditCtrl()</TT>
<TT>{</TT>
<TT>    InitializeIIDs(&amp;IID_DOleEdit, &amp;IID_DOleEditEvents);</TT>
<TT>    m_fTextAllowed = TRUE;</TT>
<TT>    m_fNumbersAllowed = TRUE;</TT>
<TT>}</TT>
</FONT></PRE>
<P>Every control created using ControlWizard includes a default property page. The
OleEdit property page is modified by adding two check boxes that control the states
of the <TT>m_fTextAllowed</TT> and <TT>m_fNumbersAllowed</TT> flags. Open the <TT>IDD_PROPPAGE_OLEEDIT</TT>
dialog box resource and add two check box controls, as shown in Figure 24.4.</P>
<P><A NAME="04"></A><A HREF="04.htm"><B>Figure 24.4.</B></A> <I><BR>
The property page used in OleEdit.</I></P>
<P>Table 24.4 lists the properties for the check box controls. All properties that
aren't listed should be set to the default values.
<H4><FONT COLOR="#000077">Table 24.4. Property values for check box controls in the
OleEdit property page.</FONT></H4>
<P>
<TABLE BORDER="1">
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><B>Control</B></TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><B>Resource ID</B></TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><B>Caption</B></TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP">Numbers check box</TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>IDC_CHECK_NUMBERS</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>&amp;Numbers Allowed</TT></TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP">Text check box</TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>IDC_CHECK_TEXT</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>&amp;Text Allowed</TT></TD>
	</TR>
</TABLE>
</P>
<P>Use ClassWizard to associate <TT>COleEditPropPage</TT> member variables with the
controls, using the values shown in Table 24.5.
<H4><FONT COLOR="#000077">Table 24.5. Values for new member variables in COleEditPropPage.</FONT></H4>
<P>
<TABLE BORDER="1">
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><B>Control ID</B></TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><B>Variable Name</B></TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><B>Category</B></TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><B>Type</B></TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><B>Property Name</B></TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>IDC_CHECK_NUMBERS</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>m_fNumbersAllowed</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP">Value</TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>BOOL</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>fNumbersAllowed</TT></TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>IDC_CHECK_TEXT</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>m_fTextAllowed</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP">Value</TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>BOOL</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>fTextAllowed</TT></TD>
	</TR>
</TABLE>
</P>
<P>ClassWizard uses the optional Property Name field to generate source code that
exchanges the values from the property sheet to the control class. The <TT>DDP</TT>
and <TT>DDX</TT> macros are used to transfer and validate property page data. The
code used to transfer the value of the <TT>IDC_CHECK_TEXT</TT> control looks like
this:</P>
<PRE><FONT COLOR="#0066FF"><TT>//{{AFX_DATA_MAP(COleEditPropPage)</TT>
<TT>DDP_Check(pDX, IDC_CHECK_TEXT, m_fTextAllowed, _T(&quot;fTextAllowed&quot;));</TT>
<TT>DDX_Check(pDX, IDC_CHECK_TEXT, m_fTextAllowed;</TT>
<TT>//}}AFX_DATA_MAP</TT>
<TT>DDP_PostProcessing(pDX);</TT>
</FONT></PRE>
<P>Inside the control class, you must collect the values from the property page during
<TT>DoPropExchange</TT>, as shown in Listing 24.3.
<H4><FONT COLOR="#000077">TYPE: Listing 24.3. Collecting property page data during
DoPropExchange.</FONT></H4>
<PRE><FONT COLOR="#0066FF"><TT>void COleEditCtrl::DoPropExchange(CPropExchange* pPX)</TT>
<TT>{</TT>
<TT>    ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));</TT>
<TT>    COleControl::DoPropExchange(pPX);</TT>

<TT>    PX_Bool(pPX, _T(&quot;fNumbersAllowed&quot;), m_fNumbersAllowed );</TT>
<TT>    PX_Bool(pPX, _T(&quot;fTextAllowed&quot;), m_fTextAllowed );</TT>
<TT>}</TT>
</FONT></PRE>
<P>The OleEdit control supports the stock font property. An easy way to give the
control access to all the available fonts is to add the standard font property page
to the control. The property pages associated with an ActiveX control are grouped
together between the <TT>BEGIN_PROPPAGEIDS</TT> and <TT>END_PROPPAGEIDS</TT> macros
in the control class implementation file.</P>
<P>Listing 24.4 shows how the standard font property page is added to the control
using the <TT>PROPPAGEID</TT> macro. Remember to change the second parameter passed
to the <TT>BEGIN_PROPPAGEIDS</TT> macro, the number of property pages used by the
control object. Locate the existing <TT>BEGIN_PROPPAGEIDS</TT> macro in the <TT>OleEditCtl.cpp</TT>
file, and change that section of the file so that it looks like the code in Listing
24.4.
<H4><FONT COLOR="#000077">TYPE: Listing 24.4. Adding the standard font property page
to OleEdit.</FONT></H4>
<PRE><FONT COLOR="#0066FF"><TT>BEGIN_PROPPAGEIDS(COleEditCtrl, 2)      // changed</TT>
<TT>    PROPPAGEID(COleEditPropPage::guid)</TT>
<TT>    PROPPAGEID(CLSID_CFontPropPage)     // changed</TT>
<TT>END_PROPPAGEIDS(COleEditCtrl)</TT>
</FONT></PRE>
<P>As you will see when you test the control later in the hour, adding the font property
page, along with exposing the stock font property, enables a user to easily change
the control font. The only code that is written to allow the user to change the control's
font is in Listing 24.4.
<H3><FONT COLOR="#000077"><B>Handling Character Input</B></FONT></H3>
<P>As discussed earlier, OleEdit uses exposed properties to determine whether characters
entered on the keyboard are stored in the edit control. If an invalid character is
input, an <TT>Error</TT> event is fired to the control's container. The message sent
to the control as characters are input to the control is <TT>WM_CHAR</TT>. Using
ClassWizard, add a message-handling function to the <TT>COleEditCtrl</TT> class,
using the values from Table 24.6.
<H4><FONT COLOR="#000077">Table 24.6. Handling the WM_CHAR message in COleEditCtrl.</FONT></H4>
<P>
<TABLE BORDER="1">
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><B>Class Name</B></TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><B>Object ID</B></TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><B>Message</B></TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><B>Function</B></TD>

⌨️ 快捷键说明

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