📄 ch15.htm
字号:
<pre><font color="#008000"> EnableCompoundFile();</font></pre>
<P>This turns on the use of compound files.</P>
<P>There is a new public function, inlined in the header file, so that other functions can access the server item:</P>
<pre><font color="#008000">CShowStringSrvrItem* GetEmbeddedItem()</font></pre>
<pre><font color="#008000"> { return (CShowStringSrvrItem*)COleServerDoc::GetEmbeddedItem(); }</font></pre>
<P>This calls the base class <font color="#008000">GetEmbeddedItem()</font>, which in turn calls the virtual function <font color="#008000">OnGetEmbedded Item()</font>. That function must be overridden in the ShowString document class as shown in Listing
15.5</P>
<p><img src="cd_rom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/cd_rom.gif" hspace=10>
<P><I>Listing 15.5— ShowStringDoc.cpp— </I><I>CShowStringDoc::OnGetEmbeddedItem()</I></P>
<pre><font color="#008000">COleServerItem* CShowStringDoc::OnGetEmbeddedItem()</font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000"> // OnGetEmbeddedItem is called by the framework to get the COleServerItem</font></pre>
<pre><font color="#008000"> // that is associated with the document. It is only called when necessary.</font></pre>
<pre><font color="#008000"> CShowStringSrvrItem* pItem = new CShowStringSrvrItem(this);</font></pre>
<pre><font color="#008000"> ASSERT_VALID(pItem);</font></pre>
<pre><font color="#008000"> return pItem;</font></pre>
<pre><font color="#008000">}</font></pre>
<P>This makes a new server item from this document and returns a pointer to it.</P>
<P><I>CShowStringView</I></P>
<P>The view class has a new entry in the message map:</P>
<pre><font color="#008000"> ON_COMMAND(ID_CANCEL_EDIT_SRVR, OnCancelEditSrvr)</font></pre>
<P>This catches <font color="#008000">ID_CANCEL_EDIT_SRVR</font>, and the cancellation of editing is in place. An accelerator has already been added to connect this message to Esc. The function that catches it looks like this:</P>
<pre><font color="#008000">void CShowStringView::OnCancelEditSrvr()</font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000"> GetDocument()->OnDeactivateUI(FALSE);</font></pre>
<pre><font color="#008000">}</font></pre>
<P>This function simply deactivates the item. There are no other view changes—server views are so much simpler than container views.</P>
<P><I>CShowStringSrvrItem</I></P>
<P>The server item class is a completely new addition to ShowString. It provides an interface between the container application that causes ShowString to launch and a ShowString document. It describes an entire ShowString document that is embedded into
another document, or a portion of a ShowString document that is linked to part of a container document. It has no member variables other than those inherited from the base class, <font color="#008000">COleServerItem</font>. It has overrides for eight
functions. They are as follows:</P>
<ul>
<li> <font color="#008000">A constructor</font></pre>
<li> <font color="#008000">A destructor</font></pre>
<li> <font color="#008000">GetDocument()</font></pre>
<li> <font color="#008000">AssertValid()</font></pre>
<li> <font color="#008000">Dump()</font></pre>
<li> <font color="#008000">Serialize()</font></pre>
<li> <font color="#008000">OnDraw()</font></pre>
<li> <font color="#008000">OnGetExtent()</font></pre>
</ul>
<P>The constructor simply passes the document pointer along to the base class. The destructor does nothing. <font color="#008000">GetDocument()</font> is an inline function that calls the base class function with the same name and casts the result. <font
color="#008000">AssertValid()</font> and <font color="#008000">Dump()</font> are debug functions that simply call the base class functions. <font color="#008000">Serialize()</font> actually does some work, as shown in Listing 15.6.</P>
<p><img src="cd_rom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/cd_rom.gif" hspace=10>
<P><I>Listing 15.6— SrvrItem.cpp— CShowStringSrvrItem::Serialize()</I></P>
<pre><font color="#008000">void CShowStringSrvrItem::Serialize(CArchive& ar)</font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000"> // CShowStringSrvrItem::Serialize will be called by the framework if</font></pre>
<pre><font color="#008000"> // the item is copied to the clipboard. This can happen automatically</font></pre>
<pre><font color="#008000"> // through the OLE callback OnGetClipboardData. A good default for</font></pre>
<pre><font color="#008000"> // the embedded item is simply to delegate to the document's Serialize</font></pre>
<pre><font color="#008000"> // function. If you support links, then you will want to serialize</font></pre>
<pre><font color="#008000"> // just a portion of the document.</font></pre>
<pre><font color="#008000"> if (!IsLinkedItem())</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"> pDoc->Serialize(ar);</font></pre>
<pre><font color="#008000"> }</font></pre>
<P>There is no need to duplicate effort here. If the item is embedded, then it is an entire document, and that document has a perfectly good <font color="#008000">Serialize()</font> that can handle the work. AppWizard doesn't provide boilerplate to handle
serializing a linked item, because it is application specific. You would save just enough information to describe what part of the document has been linked in, for example, cells A3 to D27 in a spreadsheet. This doesn't make sense for ShowString, so don't
add any code to <font color="#008000">Serialize()</font>.</P>
<P>You may feel that <font color="#008000">OnDraw()</font> is out of place here. It is normally thought of as a view function. But this <font color="#008000">OnDraw()</font> draws a depiction of the server item when it is inactive. It should look very
much like the view when it is active, and it makes sense to share the work between <font color="#008000">CShowStringView::OnDraw()</font> and <font color="#008000">CShowStringSrvrItem::OnDraw()</font>. The boilerplate that AppWizard provides is in Listing
15.7.</P>
<p><img src="cd_rom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/cd_rom.gif" hspace=10>
<P><I>Listing 15.7— SrvrItem.cpp—CShowStringSrvrItem::OnDraw()</I></P>
<pre><font color="#008000">BOOL CShowStringSrvrItem::OnDraw(CDC* pDC, CSize& rSize)</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"> // TODO: set mapping mode and extent</font></pre>
<pre><font color="#008000"> // (The extent is usually the same as the size returned from OnGetExtent)</font></pre>
<pre><font color="#008000"> pDC->SetMapMode(MM_ANISOTROPIC);</font></pre>
<pre><font color="#008000"> pDC->SetWindowOrg(0,0);</font></pre>
<pre><font color="#008000"> pDC->SetWindowExt(3000, 3000);</font></pre>
<pre><font color="#008000"> // TODO: add drawing code here. Optionally, fill in the HIMETRIC extent.</font></pre>
<pre><font color="#008000"> // All drawing takes place in the metafile device context (pDC).</font></pre>
<pre><font color="#008000"> return TRUE;</font></pre>
<P>This will change a great deal, but it's worth noting now that unlike <font color="#008000">CShowStringView::OnDraw()</font>, this function takes two parameters. The second is the size in which the inactive depiction is to be drawn. The extent, as
mentioned in the boilerplate comments, typically comes from <font color="#008000">OnGetExtent(),</font> which is shown in Listing 15.8.</P>
<p><img src="cd_rom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/cd_rom.gif" hspace=10>
<P><I>Listing 15.8— SrvrItem.cpp— CShowStringSrvrItem:: OnGetExtent()</I></P>
<pre><font color="#008000">BOOL CShowStringSrvrItem::OnGetExtent(DVASPECT dwDrawAspect, CSize& rSize)</font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000"> // Most applications, like this one, only handle drawing the content</font></pre>
<pre><font color="#008000"> // aspect of the item. If you wish to support other aspects, such</font></pre>
<pre><font color="#008000"> // as DVASPECT_THUMBNAIL (by overriding OnDrawEx), then this</font></pre>
<pre><font color="#008000"> // implementation of OnGetExtent should be modified to handle the</font></pre>
<pre><font color="#008000"> // additional aspect(s).</font></pre>
<pre><font color="#008000"> if (dwDrawAspect != DVASPECT_CONTENT)</font></pre>
<pre><font color="#008000"> return COleServerItem::OnGetExtent(dwDrawAspect, rSize);</font></pre>
<pre><font color="#008000"> // CShowStringSrvrItem::OnGetExtent is called to get the extent in</font></pre>
<pre><font color="#008000"> // HIMETRIC units of the entire item. The default implementation</font></pre>
<pre><font color="#008000"> // here simply returns a hard-coded number of units.</font></pre>
<pre><font color="#008000"> CShowStringDoc* pDoc = GetDocument();</font></pre>
<pre><font color="#008000"> ASSERT_VALID(pDoc);</font></pre>
<pre><font color="#008000"> // TODO: replace this arbitrary size</font></pre>
<pre><font color="#008000"> rSize = CSize(3000, 3000); // 3000 x 3000 HIMETRIC units</font></pre>
<pre><font color="#008000"> return TRUE;</font></pre>
<pre><font color="#008000">}</font></pre>
<P>You will replace this with real code very shortly.</P>
<P><I>CInPlaceFrame</I></P>
<P>The in-place frame class, which inherits from <font color="#008000">COleIPFrameWnd</font>, handles the frame around the server item and the toolbars, status bars, and dialog-box bars, collectively known as <I>control bars</I>, that it displays. It has
the following three protected member variables:</P>
<pre><font color="#008000"> CToolBar m_wndToolBar;</font></pre>
<pre><font color="#008000"> COleResizeBar m_wndResizeBar;</font></pre>
<pre><font color="#008000"> COleDropTarget m_dropTarget;</font></pre>
<P>The <font color="#008000">CToolBar</font> class is discussed in <A HREF="index10.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index10.htm" target="text">Chapter 10</A>, "Status Bars and Toolbars." <font color="#008000">COleDropTarget</font> is discussed in the drag and drop section of <A
HREF="index14.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index14.htm" target="text">Chapter 14</A>, "Building an ActiveX Container Application." <font color="#008000">COleResizeBar</font> looks just like a <font color="#008000">CRectTracker</font>, which was used extensively in <A
HREF="index14.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index14.htm" target="text">Chapter 14</A>, but allows the resizing of a server item rather than a container item.</P>
<P>The following are the seven member functions of <font color="#008000">CInPlaceFrame</font>:</P>
<ul>
<li> <font color="#008000">A constructor</font></pre>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -