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

📄 ch16.htm

📁 VC使用大全。里面集合了VC使用的各种使用技巧。非常有用。
💻 HTM
📖 第 1 页 / 共 4 页
字号:
<pre><font color="#008000">          return FALSE;</font></pre>

<pre><font color="#008000">     m_string = &quot;Hello, world!&quot;;</font></pre>

<pre><font color="#008000">     m_color = 0;     //black</font></pre>

<pre><font color="#008000">     m_horizcenter = TRUE;</font></pre>

<pre><font color="#008000">     m_vertcenter = TRUE;</font></pre>

<pre><font color="#008000">     return TRUE;</font></pre>

<pre><font color="#008000">}</font></pre>

<P>Next, edit the document's <font color="#008000">Serialize</font> function: the new code is shown in Listing 16.11.</P>

<P><I>Listing 16.11&#151;ShowStringDoc.cpp&#151; CShowStringDoc::Serialize()</I></P>

<pre><font color="#008000">void CShowStringDoc::Serialize(CArchive&amp; ar)</font></pre>

<pre><font color="#008000">{</font></pre>

<pre><font color="#008000">     if (ar.IsStoring())</font></pre>

<pre><font color="#008000">     {</font></pre>

<pre><font color="#008000">          ar &lt;&lt; m_string;</font></pre>

<pre><font color="#008000">          ar &lt;&lt; m_color;</font></pre>

<pre><font color="#008000">          ar &lt;&lt; m_horizcenter;</font></pre>

<pre><font color="#008000">          ar &lt;&lt; m_vertcenter;</font></pre>

<pre><font color="#008000">     }</font></pre>

<pre><font color="#008000">     else</font></pre>

<pre><font color="#008000">     {</font></pre>

<pre><font color="#008000">          ar &gt;&gt; m_string;</font></pre>

<pre><font color="#008000">          ar &gt;&gt; m_color;</font></pre>

<pre><font color="#008000">          ar &gt;&gt; m_horizcenter;</font></pre>

<pre><font color="#008000">          ar &gt;&gt; m_vertcenter;</font></pre>

<pre><font color="#008000">     }</font></pre>

<pre><font color="#008000">}</font></pre>

<P>Finally, the view's <font color="#008000">OnDraw()</font> function (Listing 16.12) actually shows the string.</P>

<P><I>Listing 16.12&#151;ShowStringView.cpp&#151; CShowStringView::OnDraw()</I></P>

<pre><font color="#008000">void CShowStringView::OnDraw(CDC* pDC)</font></pre>

<pre><font color="#008000">{</font></pre>

<pre><font color="#008000">     CShowStringDoc* pDoc = GetDocument();</font></pre>

<pre><font color="#008000">     ASSERT_VALID(pDoc);</font></pre>

<pre><font color="#008000">     COLORREF oldcolor;</font></pre>

<pre><font color="#008000">     switch (pDoc-&gt;GetColor())</font></pre>

<pre><font color="#008000">     {</font></pre>

<pre><font color="#008000">     case 0:</font></pre>

<pre><font color="#008000">          oldcolor = pDC-&gt;SetTextColor(RGB(0,0,0)); //black</font></pre>

<pre><font color="#008000">          break;</font></pre>

<pre><font color="#008000">     case 1:</font></pre>

<pre><font color="#008000">          oldcolor = pDC-&gt;SetTextColor(RGB(0xFF,0,0)); //red</font></pre>

<pre><font color="#008000">          break;</font></pre>

<pre><font color="#008000">     case 2:</font></pre>

<pre><font color="#008000">          oldcolor = pDC-&gt;SetTextColor(RGB(0,0xFF,0)); //green</font></pre>

<pre><font color="#008000">          break;</font></pre>

<pre><font color="#008000">     }</font></pre>

<pre><font color="#008000">     int DTflags = 0;</font></pre>

<pre><font color="#008000">     if (pDoc-&gt;GetHorizcenter())</font></pre>

<pre><font color="#008000">     {</font></pre>

<pre><font color="#008000">          DTflags |= DT_CENTER;</font></pre>

<pre><font color="#008000">     }</font></pre>

<pre><font color="#008000">     if (pDoc-&gt;GetVertcenter())</font></pre>

<pre><font color="#008000">     {</font></pre>

<pre><font color="#008000">          DTflags |= (DT_VCENTER|DT_SINGLELINE);</font></pre>

<pre><font color="#008000">     }</font></pre>

<pre><font color="#008000">     </font></pre>

<pre><font color="#008000">     CRect rect;</font></pre>

<pre><font color="#008000">     GetClientRect(&amp;rect);</font></pre>

<pre><font color="#008000">     pDC-&gt;DrawText(pDoc-&gt;GetString(), &amp;rect, DTflags);</font></pre>

<pre><font color="#008000">     pDC-&gt;SetTextColor(oldcolor);</font></pre>

<pre><font color="#008000">}</font></pre>

<P>When you added <font color="#008000">m_string</font>, <font color="#008000">m_color</font>, <font color="#008000">m_horizcenter</font>, and <font color="#008000">m_vertcenter</font> to the document with ClassWizard, they were added as protected member 
variables. This view code needs access to them. As you can see, the view calls public functions to get to these member variables of the document.</P>

<blockquote><p><img src="note.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/note.gif">

<P>You could have chosen instead to make the view a friend to the document so that it could access the member variables directly, but that would give view functions the ability to use and change all the private and protected member variables of the 
document. This more limited access is more appropriate, and better preserves encapsulation.</P>

<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>

<P>There are already several functions in the document class that access these variables, but they are protected functions for use by ActiveX. The four public functions you will add have not-so-good names. Add them inline, as shown in Listing 16.13, to 
ShowStringDoc.h.</P>

<P><I>Listing 16.13&#151;ShowStringDoc.h&#151;Public Access Functions</I></P>

<pre><font color="#008000">public:</font></pre>

<pre><font color="#008000">     CString GetDocString() {return m_string;}</font></pre>

<pre><font color="#008000">     int     GetDocColor() {return m_color;}</font></pre>

<pre><font color="#008000">     BOOL GetHorizcenter() {return m_horizcenter;}</font></pre>

<pre><font color="#008000">     BOOL GetVertcenter() {return m_vertcenter;}</font></pre>

<P>In <font color="#008000">CShowStringView::OnDraw(),</font> change the call to <font color="#008000">GetColor()</font> to a call to <font color="#008000">GetDocColor()</font>, and the change the call to <font color="#008000">GetString()</font> to a call 
to <font color="#008000">GetDocString()</font>. Build the project to check for any typing mistakes or forgotten changes. While it may be tempting to run ShowString now, it's not going to do what you expect until you make a few more changes.</P>

<P><B>Showing the Window</B></P>

<P>By default, automation servers do not have a main window. Remember the little snippet from <font color="#008000">CShowStringApp::InitInstance()</font> in Listing 16.14.</P>

<P><I>Listing 16.14&#151;ShowString.cpp&#151; How the app was launched</I></P>

<pre><font color="#008000">     // Check to see if launched as OLE server</font></pre>

<pre><font color="#008000">     if (cmdInfo.m_bRunEmbedded || cmdInfo.m_bRunAutomated)</font></pre>

<pre><font color="#008000">     {</font></pre>

<pre><font color="#008000">          // Application was run with /Embedding or /Automation.  Don't show the</font></pre>

<pre><font color="#008000">          //  main window in this case.</font></pre>

<pre><font color="#008000">          return TRUE;</font></pre>

<pre><font color="#008000">     }</font></pre>

<P>This code returns before showing the main window. While you could remove this test so that ShowString always shows its window, it's more common to add a <font color="#008000">ShowWindow()</font> method for the controller application to call. You'll 
also need to add a <font color="#008000">RefreshWindow()</font> method that updates the view after a variable is changed; ClassWizard makes it simple to add these functions. Bring up ClassWizard, click the Automation tab, make sure that <font 
color="#008000">CShowStringDoc</font> is still the selected class, and then click <U>A</U>dd Method. Fill in the external name as <font color="#008000">ShowWindow</font>. ClassWizard fills in the internal name for you, and there's no need to change it. 
Choose <font color="#008000">void</font> from the Return type drop-down list box. Figure 16.6 shows the dialog box after it has been filled in.</P>

<A HREF="Rfigs06.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch16/Rfigs06.gif"><b>Fig. 16.6</b></A>

<P><I>ClassWizard makes it simple to add a ShowWindow() method.</I></P>

<P>Click OK the dialog box, and <font color="#008000">ShowWindow()</font> appears in the middle of the list of properties, which turns out to be a list of properties and methods in alphabetical order. The <I>C</I> next to the properties reminds you that 
these properties are custom properties (other types of properties are discussed in the &quot;Displaying the Current Value&quot; section of the next chapter, &quot;Building an ActiveX Control&quot;). The <I>M</I> next to the methods reminds you that these 
are methods. With <font color="#008000">ShowWindow()</font> highlighted, click <U>E</U>dit Code and then type in the function, as shown in Listing 16.15</P>

<P><I>Listing 16.15&#151;ShowStringDoc.cpp&#151;CShowStringDoc::ShowWindow()</I></P>

<pre><font color="#008000">void CShowStringDoc::ShowWindow() </font></pre>

<pre><font color="#008000">{</font></pre>

<pre><font color="#008000">    POSITION pos = GetFirstViewPosition();</font></pre>

<pre><font color="#008000">    CView* pView = GetNextView(pos);</font></pre>

<pre><font color="#008000">    if (pView != NULL)</font></pre>

<pre><font color="#008000">    {</font></pre>

<pre><font color="#008000">        CFrameWnd* pFrameWnd = pView-&gt;GetParentFrame();</font></pre>

<pre><font color="#008000">        pFrameWnd-&gt;ActivateFrame(SW_SHOW);</font></pre>

<pre><font color="#008000">        pFrameWnd = pFrameWnd-&gt;GetParentFrame();</font></pre>

<pre><font color="#008000">        if (pFrameWnd != NULL)</font></pre>

<pre><font color="#008000">            pFrameWnd-&gt;ActivateFrame(SW_SHOW);</font></pre>

<pre><font color="#008000">    }</font></pre>

<pre><font color="#008000">}</font></pre>

<P>This code activates the view and asks for it to be shown. Bring up ClassWizard again, click <U>A</U>dd Method, and add <font color="#008000">RefreshWindow()</font>, returning <font color="#008000">void</font>. Click OK and then <U>E</U>dit Code. The 
code for <font color="#008000">RefreshWindow()</font>, shown in Listing 16.16, is even simpler.</P>

<P><I>Listing 16.16&#151;ShowStringDoc.cpp&#151;CShowStringDoc::RefreshWindow()</I></P>

<pre><font color="#008000">void CShowStringDoc::RefreshWindow() </font></pre>

<pre><font color="#008000">{</font></pre>

<pre><font color="#008000">     UpdateAllViews(NULL);</font></pre>

<pre><font color="#008000">     SetModifiedFlag();</font></pre>

<pre><font color="#008000">}</font></pre>

<P>This arranges for the view (now that it's active) and its parent frame to be redrawn. And, because a change to the document is almost certainly the reason for the redraw, this is a handy place to put the call to <font 
color="#008000">SetModifiedFlag()</font>, though if you prefer, you can put it in each <font color="#008000">Set</font> function and the notification functions for the direct-access properties. You will add a call to <font 
color="#008000">RefreshWindow()</font> to each of those functions now. For example, <font color="#008000">SetHorizCenter()</font> is shown in Listing 16.17.</P>

<P><I>Listing 16.17&#151;ShowStringDoc.cpp&#151;CShowStringDoc::SetHorizCenter()</I></P>

<pre><font color="#008000">void CShowStringDoc::SetHorizCenter(BOOL bNewValue) </font></pre>

⌨️ 快捷键说明

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