?? ch24.htm
字號:
function draws an ellipse inside the bounding rectangle. This is extremely usefulif you happen to be creating an ellipse control. However, because OleEdit must looklike 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& rcBounds, const CRect& rcInvalid)</TT><TT>{</TT><TT> COLORREF clrBackground = TranslateColor(GetBackColor());</TT><TT> CBrush* pOldBrush;</TT><TT> CBrush brBackground( clrBackground );</TT><TT> pdc->FillRect( rcBounds, &brBackground );</TT><TT> pOldBrush = pdc->SelectObject( &brBackground );</TT><TT> pdc->SelectObject( pOldBrush );</TT><TT> DoSuperclassPaint(pdc, rcBounds);</TT><TT> CRect rc(rcBounds);</TT><TT> pdc->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'sbounding 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, itdraws 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 propertiesand the <TT>fTextAllowed</TT> and <TT>fNumbersAllowed</TT> custom properties. UsingClassWizard, add the stock properties for the OleEdit control. Select the Automationtab, and click the Add Property button. Fill in the dialog box using the values providedin 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 theOleEdit project. Click the Add Property button and use the values provided in Table24.2.<H4><FONT COLOR="#000077">Table 24.2. The fNumbersAllowed custom property for theOleEdit 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 stepsused 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 OleEditcontrol.</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 initializesthe custom properties added in the previous steps. The modified constructor is shownin 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(&IID_DOleEdit, &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. TheOleEdit property page is modified by adding two check boxes that control the statesof 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 thataren't listed should be set to the default values.<H4><FONT COLOR="#000077">Table 24.4. Property values for check box controls in theOleEdit 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>&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>&Text Allowed</TT></TD> </TR></TABLE></P><P>Use ClassWizard to associate <TT>COleEditPropPage</TT> member variables with thecontrols, 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 thatexchanges 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. Thecode used to transfer the value of the <TT>IDC_CHECK_TEXT</TT> control looks likethis:</P><PRE><FONT COLOR="#0066FF"><TT>//{{AFX_DATA_MAP(COleEditPropPage)</TT><TT>DDP_Check(pDX, IDC_CHECK_TEXT, m_fTextAllowed, _T("fTextAllowed"));</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 duringDoPropExchange.</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("fNumbersAllowed"), m_fNumbersAllowed );</TT><TT> PX_Bool(pPX, _T("fTextAllowed"), m_fTextAllowed );</TT><TT>}</TT></FONT></PRE><P>The OleEdit control supports the stock font property. An easy way to give thecontrol access to all the available fonts is to add the standard font property pageto the control. The property pages associated with an ActiveX control are groupedtogether between the <TT>BEGIN_PROPPAGEIDS</TT> and <TT>END_PROPPAGEIDS</TT> macrosin the control class implementation file.</P><P>Listing 24.4 shows how the standard font property page is added to the controlusing the <TT>PROPPAGEID</TT> macro. Remember to change the second parameter passedto the <TT>BEGIN_PROPPAGEIDS</TT> macro, the number of property pages used by thecontrol 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 Listing24.4.<H4><FONT COLOR="#000077">TYPE: Listing 24.4. Adding the standard font property pageto 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 propertypage, along with exposing the stock font property, enables a user to easily changethe control font. The only code that is written to allow the user to change the control'sfont 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 charactersentered on the keyboard are stored in the edit control. If an invalid character isinput, an <TT>Error</TT> event is fired to the control's container. The message sentto the control as characters are input to the control is <TT>WM_CHAR</TT>. UsingClassWizard, 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 + -