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

📄 ch15.htm

📁 A very good resource on Visual C++ 6.0 environment. It teaches through step by step approach and fin
💻 HTM
📖 第 1 页 / 共 4 页
字号:
<PRE>}
</PRE>
<P>This makes a new server item from this document and returns a pointer to it.</P>
<P><B><I>CShowStringView</I>&#160;&#160;</B>The view class has a new entry in the
message map:</P>
<P>
<PRE>     ON_COMMAND(ID_CANCEL_EDIT_SRVR, OnCancelEditSrvr)
</PRE>
<P>This catches ID_CANCEL_EDIT_SRVR, 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>
<P>
<PRE>void CShowStringView::OnCancelEditSrvr()
{
     GetDocument()-&gt;OnDeactivateUI(FALSE);
}
</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><B><I>CShowStringSrvrItem</I>&#160;&#160;</B>The server item class is a completely
new addition to ShowString. It provides an interface between the container application
that launches ShowString to and opens 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, COleServerItem. It has overrides
for eight functions. They are as follows:</P>
<P>

<UL>
	<LI>A constructor
	<P>
	<LI>A destructor
	<P>
	<LI>GetDocument()
	<P>
	<LI>AssertValid()
	<P>
	<LI>Dump()
	<P>
	<LI>Serialize()
	<P>
	<LI>OnDraw()
	<P>
	<LI>OnGetExtent()
</UL>

<P>The constructor simply passes the document pointer along to the base class. The
destructor does nothing. GetDocument() is an inline function that calls the base
class function with the same name and casts the result. AssertValid() and Dump()
are debug functions that simply call the base class functions. Serialize() actually
does some work, as shown in Listing 15.6.</P>
<P>
<H4>Listing 15.6&#160;&#160;SrvrItem.cpp--CShowStringSrvrItem::Serialize()</H4>
<PRE>void CShowStringSrvrItem::Serialize(CArchive&amp; ar)
{
     // CShowStringSrvrItem::Serialize will be called by the framework if
     //  the item is copied to the clipboard.  This can happen automatically
     //  through the OLE callback OnGetClipboardData.  A good default for
     //  the embedded item is simply to delegate to the document's Serialize
     //  function.  If you support links, then you will want to serialize
     //  just a portion of the document.
     if (!IsLinkedItem())
     {
          CShowStringDoc* pDoc = GetDocument();
          ASSERT_VALID(pDoc);
          pDoc-&gt;Serialize(ar);
</PRE>
<PRE>     }
</PRE>
<P>There is no need to duplicate effort here. If the item is embedded, it is an entire
document, and that document has a perfectly good Serialize() 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 Serialize().</P>
<P>You may feel that OnDraw() is out of place here. It is normally thought of as
a view function. But this OnDraw() 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 CShowStringView::OnDraw() and CShowStringSrvrItem::OnDraw().
The boilerplate that AppWizard provides is in Listing 15.7.</P>
<P>
<H4>Listing 15.7&#160;&#160;SrvrItem.cpp--CShowStringSrvrItem::OnDraw()</H4>
<PRE>BOOL CShowStringSrvrItem::OnDraw(CDC* pDC, CSize&amp; rSize)
{
     CShowStringDoc* pDoc = GetDocument();
     ASSERT_VALID(pDoc);
     // TODO: set mapping mode and extent
     //  (The extent is usually the same as the size returned from OnGetExtent)
     pDC-&gt;SetMapMode(MM_ANISOTROPIC);
     pDC-&gt;SetWindowOrg(0,0);
     pDC-&gt;SetWindowExt(3000, 3000);
     // TODO: add drawing code here.  Optionally, fill in the HIMETRIC extent.
     //  All drawing takes place in the metafile device context (pDC).
     return TRUE;
</PRE>
<PRE>}
</PRE>
<P>This will change a great deal, but it's worth noting now that unlike CShowStringView::OnDraw(),
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 OnGetExtent(), which is shown in Listing 15.8.</P>
<P>
<H4>Listing 15.8&#160;&#160;SrvrItem.cpp--CShowStringSrvrItem:: OnGetExtent()</H4>
<PRE>BOOL CShowStringSrvrItem::OnGetExtent(DVASPECT dwDrawAspect, CSize&amp; rSize)
{
     // Most applications, like this one, only handle drawing the content
     //  aspect of the item.  If you wish to support other aspects, such
     //  as DVASPECT_THUMBNAIL (by overriding OnDrawEx), then this
     //  implementation of OnGetExtent should be modified to handle the
     //  additional aspect(s).
     if (dwDrawAspect != DVASPECT_CONTENT)
          return COleServerItem::OnGetExtent(dwDrawAspect, rSize);
     // CShowStringSrvrItem::OnGetExtent is called to get the extent in
     //  HIMETRIC units of the entire item.  The default implementation
     //  here simply returns a hard-coded number of units.
     CShowStringDoc* pDoc = GetDocument();
     ASSERT_VALID(pDoc);
     // TODO: replace this arbitrary size
     rSize = CSize(3000, 3000);   // 3000 x 3000 HIMETRIC units
     return TRUE;
</PRE>
<PRE>}
</PRE>
<P>You will replace this with real code very shortly.</P>
<P><B><I>CInPlaceFrame</I>&#160;&#160;</B>The in-place frame class, which inherits
from COleIPFrameWnd, 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>
<P>
<PRE>     CToolBar    m_wndToolBar;
     COleResizeBar   m_wndResizeBar;
     COleDropTarget m_dropTarget;
</PRE>
<P>The CToolBar class is discussed in Chapter 9, &quot;Status Bars and Toolbars.&quot;
COleDropTarget is discussed in the drag and drop section of Chapter 14. COleResizeBar
looks just like a CRectTracker, which was used extensively in Chapter 14, but allows
the resizing of a server item rather than a container item.</P>
<P>The following are the seven member functions of CInPlaceFrame:</P>
<P>

<UL>
	<LI>A constructor
	<P>
	<LI>A destructor
	<P>
	<LI>AssertValid()
	<P>
	<LI>Dump()
	<P>
	<LI>OnCreate()
	<P>
	<LI>OnCreateControlBars()
	<P>
	<LI>PreCreateWindow()
</UL>

<P>The constructor and destructor do nothing. AssertValid() and Dump() are debug
functions that simply call the base class functions. OnCreate() actually has code,
shown in Listing 15.9.</P>
<P>
<H4>Listing 15.9&#160;&#160;IPFrame.cpp--CInPlaceFrame::OnCreate()</H4>
<PRE>int CInPlaceFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
     if (COleIPFrameWnd::OnCreate(lpCreateStruct) == -1)
          return -1;
     // CResizeBar implements in-place resizing.
     if (!m_wndResizeBar.Create(this))
     {
          TRACE0(&quot;Failed to create resize bar\n&quot;);
          return -1;      // fail to create
     }
     // By default, it is a good idea to register a drop-target that does
     //  nothing with your frame window.  This prevents drops from
     //  &quot;falling through&quot; to a container that supports drag-drop.
     m_dropTarget.Register(this);
     return 0;
</PRE>
<PRE>}
</PRE>
<P>This function catches the WM_CREATE message that is sent when an in-place frame
is created and drawn onscreen. It calls the base class function and then creates
the resize bar. Finally, it registers a drop target so that if anything is dropped
over this in-place frame, it is dropped on this server rather than the underlying
container.</P>
<P>When a server document is activated in place, COleServerDoc::ActivateInPlace()
calls CInPlaceFrame::OnCreateControlBars(), which is shown in Listing 15.10.</P>
<P>
<H4>Listing 15.10&#160;&#160;IPFrame.cpp--CInPlaceFrame::OnCreateControlBars()</H4>
<PRE>BOOL CInPlaceFrame::OnCreateControlBars(CFrameWnd* pWndFrame, 
                                        CFrameWnd* pWndDoc)
{
     // Set owner to this window, so messages are delivered to correct app
     m_wndToolBar.SetOwner(this);
     // Create toolbar on client's frame window
     if (!m_wndToolBar.Create(pWndFrame) || 
          !m_wndToolBar.LoadToolBar(IDR_SHOWSTTYPE_SRVR_IP))
     {
          TRACE0(&quot;Failed to create toolbar\n&quot;);
          return FALSE;
     }
     // TODO: Remove this if you don't want tool tips or a resizeable toolbar
     m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() |
          CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
     // TODO: Delete these three lines if you don't want the toolbar to
     //  be dockable
     m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
     pWndFrame-&gt;EnableDocking(CBRS_ALIGN_ANY);
     pWndFrame-&gt;DockControlBar(&amp;m_wndToolBar);
     return TRUE;
</PRE>
<PRE>}
</PRE>
<P>This function creates a docking, resizable toolbar with ToolTips, docked against
the edge of the main frame window for the application.</P>


<BLOCKQUOTE>
	<P>
<HR>
<strong>TIPP:</strong> If you are developing an MDI application and prefer the toolbar against
	the document frame, use pWndDoc instead of PWndFrame, in the call to m_wndToolBar.Create()
	but be sure to check that it is not NULL.
<HR>


</BLOCKQUOTE>

<P>The last function in CInPlaceFrame is PreCreateWindow(). At the moment, it just
calls the base class, as shown in Listing 15.11.</P>
<P>
<H4>Listing 15.11&#160;&#160;IPFrame.cpp--CInPlaceFrame::PreCreateWindow()</H4>
<PRE>BOOL CInPlaceFrame::PreCreateWindow(CREATESTRUCT&amp; cs)
{
     // TODO: Modify the Window class or styles here by modifying
     //  the CREATESTRUCT cs
     return COleIPFrameWnd::PreCreateWindow(cs);
</PRE>
<PRE>}
</PRE>
<P>This function is called before OnCreate() and sets up the styles for the frame
window through a CREATESTRUCT.</P>
<P>


<BLOCKQUOTE>
	<P>
<HR>
<strong>CAUTION:</strong><B> </B>Modifying these styles is not for the faint of heart. The
	Microsoft documentation recommends reading the source code for all the classes in
	the hierarchy of your CInPlaceFrame (Cwnd, CFrameWnd, COleIPFrameWnd) to see what
	CREATESTRUCT elements are already set before making any changes. For this sample
	application, don't change the CREATESTRUCT.
<HR>


</BLOCKQUOTE>

<P><B>Shortcomings of This Server&#160;&#160;</B>Apart from the fact that the starter
application from AppWizard doesn't show a string, what's missing from this server?
The OnDraw() and GetExtent()TODOs are the only significant tasks left for you by
AppWizard. Try building ShowString, and then run it once standalone just to register
it.</P>
<P>Figure 15.4 shows the Object dialog box in Microsoft Word, reached by choosing
Insert, Object. ShowString appears in this list as ShowSt Document--not surprising
considering the menu name was IDR_SHOWSTTYPE. Developer Studio calls this document
a ShowSt document. This setting could have been overriden in AppWizard by choosing
the Advanced button in Step 4 of AppWizard. Figure 15.5 shows this dialog box and
the long and short names of the file type.</P>
<P><A HREF="javascript:popUp('15uvc04.gif')"><B>FIG. 15.4</B></A><B> </B><I>The ShowString
document type, called ShowSt document, now appears in the Object dialog box when
inserting a new object into a Word document.</I></P>

<P><A HREF="javascript:popUp('15uvc05.gif')"><B>FIG. 15.5</B></A><B> </B><I>The Advanced
Options dialog box of Step 4 in AppWizard provides an opportunity to change the name
of the file type.</I></P>

<P>So, the file type names used by the Registry have been set incorrectly for this
project. The next few pages take you on a tour of the way file type names are stored
and show you how difficult they are to change.</P>
<P>The file type name has been stored in the string table. It is the caption of the
IDR_SHOWSTTYPE resource, and AppWizard has set it to:</P>
<P>
<PRE>\nShowSt\nShowSt\n\n\nShowString.Document\nShowSt Document
</PRE>
<P>To look at this string, choose String Table from the Resource View, open the only

⌨️ 快捷键说明

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