📄 ch15.htm
字号:
of the server item class. You use a few CDC member functions instead. It's a nice
touch to draw the item slightly differently to help remind the user that it is not
active, as shown in Listing 15.14. You can paste in the drawing code from the view's
OnDraw(), but change the colors slightly to give the user a reminder.</P>
<P>
<H4>Listing 15.14  SrvrItem.cpp--CShowStringSrvrItem::OnDraw()</H4>
<PRE>BOOL CShowStringSrvrItem::OnDraw(CDC* pDC, CSize& 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->SetMapMode(MM_ANISOTROPIC);
pDC->SetWindowOrg(0,0);
pDC->SetWindowExt(3000, 3000);
COLORREF oldcolor;
switch (pDoc->GetColor())
{
case 0:
oldcolor = pDC->SetTextColor(RGB(0x80,0x80,0x80)); //gray
break;
case 1:
oldcolor = pDC->SetTextColor(RGB(0xB0,0,0)); // dull red
break;
case 2:
oldcolor = pDC->SetTextColor(RGB(0,0xB0,0)); // dull green
break;
}
int DTflags = 0;
if (pDoc->GetHorizcenter())
{
DTflags |= DT_CENTER;
}
if (pDoc->GetVertcenter())
{
DTflags |= (DT_VCENTER|DT_SINGLELINE);
}
CRect rect;
rect.TopLeft() = pDC->GetWindowOrg();
rect.BottomRight() = rect.TopLeft() + pDC->GetWindowExt();
pDC->DrawText(pDoc->GetString(), &rect, DTflags);
pDC->SetTextColor(oldcolor);
return TRUE;
</PRE>
<PRE>}
</PRE>
<P>The function starts with the boilerplate from AppWizard. With an application that
doesn't just draw itself in whatever space is provided, you would want to add code
to determine the extent rather than just using (3000,3000). (You'd want to add the
code to OnGetExtent(), too.) But hardcoding the numbers works for this simple example.</P>
<P>Build the application, fix any typos or other simple errors, and then start Word
and insert a ShowString document into your worksheet. ShowString should run as before,
with Hello, world! in the center of the view. Convince yourself that the Options
dialog box still works and that you have restored all the old functionality. Be sure
to change at least one thing: the string, the color, or the centering. Then, press
Esc to finish editing in place. Oops! It still draws the old Hello, world! in gray
in the top left of the server area. Why?</P>
<P>Remember that in CShowStringDoc::OnToolsOptions(), after the user clicks OK, you
tell the document that it has been changed and arrange to have the view redrawn:</P>
<P>
<PRE> SetModifiedFlag();
UpdateAllViews(NULL);
</PRE>
<P>You need to add another line there to make sure that any containers that are containing
this document are also notified:</P>
<P>
<PRE> NotifyChanged();
</PRE>
<P>Now build it again and insert a different ShowString object into a Word document.
This time the changes are reflected in the inactive server display as well. Figure
15.12 shows a ShowString item being edited in place, and Figure 15.13 shows the same
item inactive.</P>
<BLOCKQUOTE>
<P>
<HR>
<strong>NOTE:</strong> If you turn on either centering option, the string will not appear
when the item is inactive. It seems that DrawText is centering the string within
a much larger rectangle than the one you pass to it. Simpler CDC functions, such
as DrawEllipse, don't have this problem. It might be wise to avoid centering text
with DrawText() if your inactive appearance is important. 
<HR>
</BLOCKQUOTE>
<P><A HREF="javascript:popUp('15uvc12.gif')"><B>FIG. 15.12</B></A><B> </B><I>This
ShowString item is being edited in place.</I></P>
<P><A HREF="javascript:popUp('15uvc13.gif')"><B>FIG. 15.13</B></A><B> </B><I>This
ShowString item is inactive.</I></P>
<P>Good old ShowString has been through a lot. It's time for one more transformation.</P>
<P>
<H2><A NAME="Heading4"></A>Applications That Are Both Container and Server</H2>
<P>As you might expect, adding container features to this version of ShowString is
as difficult as adding them to the ordinary ShowString of the previous chapter. If
you add these features, you gain an application that can tap the full power of ActiveX
to bring extraordinary power to your work and your documents.</P>
<P>
<H3><A NAME="Heading5"></A>Building Another Version of ShowString</H3>
<P>The way to get a ShowString that is both a container and a server is to follow
these steps:</P>
<P>
<DL>
<DT></DT>
<DD><B>1. </B>Build a new ShowString with AppWizard that is a container and a full
server. Run AppWizard as usual but in a different directory than the one where you
created the server-only ShowString. Be sure to select the Both Container And Server
radio button in Step 3. In Step 4, click the Advanced button and change the filename
types as you did earlier in this chapter. Finally, when asked whether you want to
use the same CLSID, click No. This is a different application.
<P>
<DT></DT>
<DD><B>2. </B>Make the container changes from the preceding chapter. When adding
the Tools, Options menu item and accelerator, add it to the main menu, the server
in-place menu, and the server-embedded menu.
<P>
<DT></DT>
<DD><B>3. </B>Make the server changes from this chapter.
<P>
<DT></DT>
<DD><B>4. </B>Add the ShowString functionality.
<P>
</DL>
<P>This section does not present the process of building a container and server application
in detail; that is covered in the "Adding Server Capabilities to ShowString"
section of this chapter and all of Chapter 14. Rather, the focus here is on the consequences
of building such an application.</P>
<P>
<H3><A NAME="Heading6"></A>Nesting and Recursion Issues</H3>
<P>After an application is both a server (meaning its documents can be embedded in
other applications) and a container, it is possible to create nested documents. For
example, Microsoft Word is both container and server. An Excel spreadsheet might
contain a Word document, which in turn contains a bitmap, as shown in Figure 15.14.</P>
<P>Within Excel, you can double-click the Word document to edit it in place, as shown
in Figure 15.15, but you cannot go on to double-click the bitmap and edit it in place,
too. You can edit it in a window of its own, as shown in Figure 15.16. It is a limitation
of ActiveX that you cannot nest in-place editing sessions indefinitely.</P>
<P>
<H2><A NAME="Heading7"></A>Active Documents</H2>
<P>The final, important recent addition to ActiveX is Active Documents, formerly
known as ActiveX Document Objects. An ordinary ActiveX server takes over the menus
and interface of a container application when the document is being edited in place
but does so in cooperation with the container application. An Active Document server
takes over far more dramatically, as you will shortly see.</P>
<P><A HREF="javascript:popUp('15uvc14.gif')"><B>FIG. 15.14</B></A><B> </B><I>This
Excel spreadsheet contains a Word document that contains a bitmap.</I></P>
<P><A HREF="javascript:popUp('15uvc15.gif')"><B>FIG. 15.15</B></A><B> </B><I>This
Word document is being edited in place.</I></P>
<P><A HREF="javascript:popUp('15uvc16.gif')"><B>FIG. 15.16</B></A><B> </B><I>This
bitmap is nested within a Word document within an Excel spreadsheet, and so cannot
be edited in place. Instead, it is edited in a separate window.</I><B></B></P>
<H3><A NAME="Heading8"></A>What Active Documents Do</H3>
<P>The first application to demonstrate the use of Active Documents is the Microsoft
Office Binder, shown in Figure 15.17. To the user, it appears that this application
can open any Office document. In reality, the documents are opened with their own
server applications while the frame around them and the list of other documents remain
intact. Microsoft Internet Explorer (version 3.0 and later) is also an Active Document
container--Figure 15.18 shows a Word document open in Explorer. Notice the menus
are mostly Word menus, but the Explorer toolbar can still be used. For example, clicking
the Back button closes this Word document and opens the document that was loaded
previously.</P>
<P>To users, this is a complete transition to a document-centered approach. No matter
what application the user is working with, any kind of document can be opened and
edited, using the code written to work with that document but the interface that
the user has learned for his or her own application.</P>
<P>
<H3><A NAME="Heading9"></A>Making ShowString an Active Document Server</H3>
<P>Making yet another version of ShowString, this one as an Active Document server,
is pretty simple. Follow the instructions from the "AppWizard's Server Boilerplate"
section at the beginning of this chapter, with two exceptions: in AppWizard's Step
3, select Active Document Server and in AppWizard's Step 4, click the Advanced button.
Fix the file type names and fill in the file extension as <B>.SST</B>, as shown in
Figure 15.19. This helps Active Document containers determine what application to
launch when you open a ShowString file.</P>
<P><A HREF="javascript:popUp('15uvc17.gif')"><B>FIG. 15.17</B></A><B> </B><I>The
Microsoft Office Binder makes it simple to pull Office documents together.</I></P>
<P><A HREF="javascript:popUp('15uvc18.gif')"><B>FIG. 15.18</B></A><B> </B><I>Microsoft
Internet Explorer is also a container for Active Documents.</I></P>
<P><A HREF="javascript:popUp('15uvc19.gif')"><B>FIG. 15.19</B></A><B> </B><I>The
Advanced Options dialog box of AppWizard's Step 4 is where you specify the extension
for ShowString files.</I></P>
<P><B>Document Extension Boilerplate  </B>Any one of the versions of ShowString
built up to this point could have had a document extension specified. AppWizard adds
these lines to CShowStringApp::InitInstance() when you specify a document extension
for an Active Document server application:</P>
<P>
<PRE> // Enable drag/drop open
m_pMainWnd->DragAcceptFiles();
// Enable DDE Execute open
EnableShellOpen();
RegisterShellFileTypes(TRUE);
</PRE>
<P>It is the call to RegisterShellFileTypes() that matters here, though the drag
and drop is a nice touch. You're able to drag files from your desktop or a folder
onto the ShowString icon or an open copy of ShowString, and the file opens in ShowString.</P>
<P><B>Active Document Server Boilerplate  </B>Selecting Active Document
support makes remarkably little difference to the code generated by AppWizard. In
CShowStringApp::InitInstance(), the versions of ShowString that were not Active Document
servers had this call to update the Registry:</P>
<P>
<PRE> m_server.UpdateRegistry(OAT_INPLACE_SERVER);
</PRE>
<P>The Active Document version of ShowString has this line:</P>
<P>
<PRE> m_server.UpdateRegistry(OAT_DOC_OBJECT_SERVER);
</PRE>
<P>In both cases, m_server is a CShowStringSrvrItem, but now the Active Document
server version has a server item that inherits from CDocObjectServerItem. This causes
a number of little changes throughout the source and includes files for CShowStringSrvrItem,
where base class functions are called. Similarly, the in-place frame object, CInPlaceFrame,
now inherits from COleDocIPFrameWnd.</P>
<P><B>Showing Off the Newest ShowString  </B>Restore the ShowString functionality
once again as described in the section "Showing a String Again," earlier
in this chapter. Also copy the OnDraw() code from an old version of ShowString to
CshowStringDoc::OnDraw(). Build the application, run it once to register it, and
then run Microsoft Binder (if you have Office installed). Choose Section Add to bring
up the Add Section dialog box shown in Figure 15.20. On the General tab, highlight
ShowString Document and click OK.</P>
<P><A HREF="javascript:popUp('15uvc20.gif')"><B>FIG. 15.20</B></A><B> </B><I>Not
many applications on the market are Active Document servers, but you can write one
in minutes.</I></P>
<P>The menus include ShowString's Tools menu, as before. Choose Tools, Options and
change something--for example, in Figure 15.21, the string has been changed to "Hello
from the Binder" and the horizontal centering has been turned on. You have access
to all of ShowString's functionality, although it doesn't look as though you are
running ShowString.</P>
<P>Now run ShowString alone and save a document by choosing File, Save. You don't
need to enter an extension: The extension .SST is used automatically. Open an Explorer
window and explore until you reach the file you saved. Bring up Internet Explorer
4.0 and drag the file you saved onto Internet Explorer.</P>
<P>Your ShowString document opens in Explorer, as you can see in Figure 15.22. The
toolbar is clearly the Explorer toolbar, but the menu has the Tools item, and you
can change the string, centering, and color as before. If you use the Back button
on the Explorer toolbar, you reload the document you had open. If you change the
ShowString document before clicking Back, you'll even be prompted to save your changes!
Microsoft plans to integrate the desktop in the next generation of Windows with the
Internet Explorer interface. What you see here is a sneak preview of how that will
work.</P>
<P><A HREF="javascript:popUp('15uvc21.gif')"><B>FIG. 15.21</B></A><B> </B><I>All
of ShowString's functionality is available from within the Binder.</I></P>
<P><A HREF="javascript:popUp('15uvc22.gif')"><B>FIG. 15.22</B></A><B> </B><I>Internet
Explorer appears to be able to read and write ShowString files now.</I></P>
<P>You can also arrange for your applications to be Active Document containers. Perhaps
you noticed the check box on AppWizard's Step 3 where you could ask AppWizard to
turn on this feature. It's not much harder to do than serving Active Documents, so
you can explore it on your own. If you would like your users to be able to open Word
files, Excel spreadsheets, or other Active Documents from within your application,
be sure to look into this feature.</P>
<P>Eventually Windows will look very much like Internet Explorer; Active Documents
will make that possible.</P>
<H1></H1>
<CENTER>
<P>
<HR>
<A HREF="ch14.htm" tppabs="http://www.fintech.ru/library/prog/SEUsingVC6/ch14/ch14.htm"><IMG SRC="previous.gif" tppabs="http://www.fintech.ru/library/prog/SEUsingVC6/button/previous.gif" WIDTH="128" HEIGHT="28"
ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="ch16.htm" tppabs="http://www.fintech.ru/library/prog/SEUsingVC6/ch16/ch16.htm"><IMG
SRC="next.gif" tppabs="http://www.fintech.ru/library/prog/SEUsingVC6/button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"
BORDER="0"></A><A HREF="index.htm" tppabs="http://www.fintech.ru/library/prog/SEUsingVC6/index.htm"><IMG SRC="contents.gif" tppabs="http://www.fintech.ru/library/prog/SEUsingVC6/button/contents.gif" WIDTH="128"
HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A> <BR>
<BR>
</P>
<P>© <A HREF="copy.htm" tppabs="http://www.fintech.ru/library/prog/SEUsingVC6/copy.htm">Copyright</A>, Macmillan Computer Publishing. All
rights reserved.
</CENTER>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -