📄 ch16.htm
字号:
<P>These variables will be added as automation properties, so you will not type their names into the class definition for <font color="#008000">CShowStringDoc</font>. Bring up ClassWizard by clicking its toolbar button or choosing <U>V</U>iew,
Class<U>W</U>izard. Click the Automation tab, shown in Figure 16.1, to add properties and methods. Make sure that <font color="#008000">CShowStringDoc</font> is selected in the Class name box.</P>
<A HREF="Rfigs01.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch16/Rfigs01.gif"><b>Fig. 16.1</b></A>
<P><I>ClassWizard's Automation tab handles most of the work of building an </I><I>Automation server.</I></P>
<P>The first step in restoring the old ShowString functionality is to add member variables to the document class that will be exposed as properties of the automation server. There are two ways to expose properties: as a variable, and with functions.
Exposing a property as a variable is rather like declaring a public member variable of a C++ class; other applications can look at the value of the property and change it directly. A notification function within your server is called when the variable is
changed from the outside. Exposing with <font color="#008000">Get</font> and <font color="#008000">Set</font> functions is like implementing a private member variable with public access functions. Other applications appear to access the variable directly,
but the framework arranges for a call to your functions to <font color="#008000">Get</font> and <font color="#008000">Set</font> the property. Your <font color="#008000">Get</font> may make sure that the object is in a valid state (for example, that a
sorted list is currently sorted or that a total has been calculated) before returning the property value. Your <font color="#008000">Set</font> function may do error checking (validation) or may calculate other variables that depend on the property that
the outside application is changing. To make a property read-only, you add it as a <font color="#008000">Get</font>/<font color="#008000">Set</font> function property and then do not implement a <font color="#008000">Set</font> function.</P>
<P>For the purposes of this chapter, you will add the two centering flags to the <font color="#008000">CShowStringDoc</font> class with <font color="#008000">Get</font> and <font color="#008000">Set</font> functions, and the string and color properties as
direct-access properties. To do so, follow these steps:</P>
<ol>
<li><P> Make sure that <font color="#008000">CShowStringDoc</font> is the selected class, then click the Add P<U>r</U>operty button to bring up the Add Property dialog box.</P>
<li><P> Type <B><font color="#008000">String</font></B> in the External name box, and ClassWizard types along with you, filling in the Variable name and Notification function boxes for you.</P>
<li><P> Choose <font color="#008000">CString</font> from the drop-down list box for Type, and then the dialog box should resemble Figure 16.2.</P>
</ol>
<A HREF="Rfigs02.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch16/Rfigs02.gif"><b>Fig. 16.2</b></A>
<P><I>Add </I><I>String</I><I> as a direct-access property.</I></P>
<ol start=4>
<li><P> Click OK, click Add P<U>r</U>operty again, and then add <font color="#008000">Color</font> as a direct-access property, as shown in Figure 16.3. Use <font color="#008000">short</font> as the data type.</P>
</ol>
<A HREF="Rfigs03.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch16/Rfigs03.gif"><b>Fig. 16.3</b></A>
<P><I>Add </I><I>Color</I><I> as a direct-access property.</I></P>
<ol start=5>
<li><P> Click OK, click Add P<U>r</U>operty again, and then add <font color="#008000">HorizCenter</font>.</P>
<li><P> Choose <font color="#008000">BOOL</font> for the type and then select the Get/Set <U>m</U>ethods radio button. The Variable name and Notification function boxes are replaced by Get function and Set function, already filled in, as shown in Figure
16.4. (If the type changes from <font color="#008000">BOOL</font>, choose <font color="#008000">BOOL</font> again.) Click OK.</P>
<li><P> Add <font color="#008000">VertCenter</font> in the same way that you added <font color="#008000">HorizCenter</font>.</P>
</ol>
<A HREF="Rfigs04.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch16/Rfigs04.gif"><b>Fig. 16.4</b></A>
<P><I>Add </I><I>HorizCenter</I><I> as a Get/Set method property.</I></P>
<blockquote><p><img src="caution.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/caution.gif">
<P>Once you have clicked OK to add a property, you cannot change the type, external name, or other properties of the property. You will have to delete it and then add one that has the new type or external name, or whatever. Always look over the Add
Property dialog box before clicking OK.</P>
<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>
<P>Figure 16.5 shows the ClassWizard summary of exposed properties and methods. The details of each property are shown in the Implementation box below the list of properties. In Figure 16.5, <font color="#008000">VertCenter</font> is highlighted, and the
Implementation box reminds you that <font color="#008000">VertCenter</font> has a <font color="#008000">Get</font> function and a <font color="#008000">Set</font> function, showing their declarations. Click OK to close ClassWizard.</P>
<A HREF="Rfigs05.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch16/Rfigs05.gif"><b>Fig. 16.5</b></A>
<P><I>ClassWizard provides a summary of the properties you have added.</I></P>
<P>It should come as no surprise that as a result of these additions, ClassWizard has made changes to the header and source files for <font color="#008000">CShowStringDoc</font>. The new dispatch map in the header file is in Listing 16.7.</P>
<P><I>Listing 16.7—ShowStringDoc.h—Dispatch Map</I></P>
<pre><font color="#008000"> //{{AFX_DISPATCH(CShowStringDoc)</font></pre>
<pre><font color="#008000"> CString m_string;</font></pre>
<pre><font color="#008000"> afx_msg void OnStringChanged();</font></pre>
<pre><font color="#008000"> short m_color;</font></pre>
<pre><font color="#008000"> afx_msg void OnColorChanged();</font></pre>
<pre><font color="#008000"> afx_msg BOOL GetHorizCenter();</font></pre>
<pre><font color="#008000"> afx_msg void SetHorizCenter(BOOL bNewValue);</font></pre>
<pre><font color="#008000"> afx_msg BOOL GetVertCenter();</font></pre>
<pre><font color="#008000"> afx_msg void SetVertCenter(BOOL bNewValue);</font></pre>
<pre><font color="#008000"> //}}AFX_DISPATCH</font></pre>
<pre><font color="#008000"> DECLARE_DISPATCH_MAP()</font></pre>
<P>Two new member variables have been added: <font color="#008000">m_string</font> and <font color="#008000">m_color</font>.</P>
<blockquote><p><img src="note.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/note.gif">
<P>It's natural to wonder if these are actually public member variables; they are not. Just above this dispatch map is this line:</P>
<pre><font color="#008000">DECLARE_MESSAGE_MAP()</font></pre>
<P>And that macro, when it expands, declares a number of protected variables. Since these declarations are immediately afterward, they are protected member variables and protected functions. They are accessed in just the same way that protected
message-catching functions are: they are called by a member function hidden in the class that directs traffic using these maps.</P>
<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>
<P>A block of code has been added in the source file, but it's pretty boring, as you can see by looking at listing 16.8.</P>
<P><I>Listing 16.8—ShowStringDoc.cpp—Notification, Get, and Set functions</I></P>
<pre><font color="#008000">/////////////////////////////////////////////////////////</font></pre>
<pre><font color="#008000">// CShowStringDoc commands</font></pre>
<pre><font color="#008000">void CShowStringDoc::OnColorChanged() </font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000"> // TODO: Add notification handler code</font></pre>
<pre><font color="#008000">}</font></pre>
<pre><font color="#008000">void CShowStringDoc::OnStringChanged() </font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000"> // TODO: Add notification handler code</font></pre>
<pre><font color="#008000">}</font></pre>
<pre><font color="#008000">BOOL CShowStringDoc::GetHorizCenter() </font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000"> // TODO: Add your property handler here</font></pre>
<pre><font color="#008000"> return TRUE;</font></pre>
<pre><font color="#008000">}</font></pre>
<pre><font color="#008000">void CShowStringDoc::SetHorizCenter(BOOL bNewValue) </font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000"> // TODO: Add your property handler here</font></pre>
<pre><font color="#008000">}</font></pre>
<pre><font color="#008000">BOOL CShowStringDoc::GetVertCenter() </font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000"> // TODO: Add your property handler here</font></pre>
<pre><font color="#008000"> return TRUE;</font></pre>
<pre><font color="#008000">}</font></pre>
<pre><font color="#008000">void CShowStringDoc::SetVertCenter(BOOL bNewValue) </font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000"> // TODO: Add your property handler here</font></pre>
<pre><font color="#008000">}</font></pre>
<P>The class still does not have member variables for the centering flags. Add them by hand to the header file, as private member variables:</P>
<pre><font color="#008000">// Attributes</font></pre>
<pre><font color="#008000">private:</font></pre>
<pre><font color="#008000"> BOOL m_horizcenter;</font></pre>
<pre><font color="#008000"> BOOL m_vertcenter;</font></pre>
<P>Now you can write their <font color="#008000">Get</font> and <font color="#008000">Set</font> functions; Listing 16.9 shows the code.</P>
<P><I>Listing 16.9—ShowStringDoc.cpp—Get and Set functions for the centering flags</I></P>
<pre><font color="#008000">BOOL CShowStringDoc::GetHorizCenter() </font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000"> return m_horizcenter;</font></pre>
<pre><font color="#008000">}</font></pre>
<pre><font color="#008000">void CShowStringDoc::SetHorizCenter(BOOL bNewValue) </font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000"> m_horizcenter = bNewValue;</font></pre>
<pre><font color="#008000">}</font></pre>
<pre><font color="#008000">BOOL CShowStringDoc::GetVertCenter() </font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000"> return m_vertcenter;</font></pre>
<pre><font color="#008000">}</font></pre>
<pre><font color="#008000">void CShowStringDoc::SetVertCenter(BOOL bNewValue) </font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000"> m_vertcenter = bNewValue;</font></pre>
<pre><font color="#008000">}</font></pre>
<P><B>The OnDraw() function</B></P>
<P>Restoring the member variables takes you halfway to the old functionality of ShowString. Changing the view's <font color="#008000">OnDraw()</font> function will take you most of the rest of the way.</P>
<P>To write a version of <font color="#008000">OnDraw()</font> that shows a string properly, open an old version of ShowString—either from your own work in <A HREF="index09.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index09.htm" target="text">Chapter 9</A>, "Building a Complete Application:
ShowString," or from the CD that comes with this book—and then paste in the following bits of code. (If any of this code is unfamiliar to you, <A HREF="index09.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index09.htm" target="text">Chapter 9</A> explains it fully.) First, <font
color="#008000">CShowStringDoc::OnNewDocument()</font> (Listing 16.10) should initialize the member variables.</P>
<P><I>Listing 16.10—ShowStringDoc.cpp—CShowStringDoc::OnNewDocument()</I></P>
<pre><font color="#008000">BOOL CShowStringDoc::OnNewDocument()</font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000"> if (!CDocument::OnNewDocument())</font></pre>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -