📄 ch14.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!--
function popUp(pPage) {
var fullURL = document.location;
var textURL = fullURL.toString();
var URLlen = textURL.length;
var lenMinusPage = textURL.lastIndexOf("/");
lenMinusPage += 1;
var fullPath = textURL.substring(0,lenMinusPage);
popUpWin = window.open('','popWin','resizable=yes,scrollbars=no,width=525,height=394');
figDoc= popUpWin.document;
zhtm= '<HTML><HEAD><TITLE>' + pPage + '</TITLE>';
zhtm += '<link rel="stylesheet" href="/includes/stylesheets/ebooks.css"></head>';
zhtm += '<BODY bgcolor="#FFFFFF">';
zhtm += '<IMG SRC="' + fullPath + pPage + '">';
zhtm += '<P><B>' + pPage + '</B>';
zhtm += '</BODY></HTML>';
window.popUpWin.document.write(zhtm);
window.popUpWin.document.close();
// Johnny Jackson 4/28/98
}
//-->
</SCRIPT>
<link rel="stylesheet" href="/includes/stylesheets/ebooks.css">
<TITLE>Special Edition Using Visual C++ 6 -- Ch 14 -- Building an ActiveX Container Application</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF">
<CENTER>
<H1><IMG SRC="../button/que.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM" BORDER="0"><BR>
Special Edition Using Visual C++ 6</H1>
</CENTER>
<CENTER>
<P><A HREF="../ch13/ch13.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"
ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch15/ch15.htm"><IMG
SRC="../button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"
BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../button/contents.gif" WIDTH="128"
HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A>
<HR>
</CENTER>
<CENTER>
<H1>- 14 -</H1>
</CENTER>
<CENTER>
<H1>Building an ActiveX Container Application</H1>
</CENTER>
<UL>
<LI><A HREF="#Heading1">Changing ShowString</A>
<UL>
<LI><A HREF="#Heading2">AppWizard-Generated ActiveX Container Code</A>
<LI><A HREF="#Heading3">Returning the ShowString Functionality</A>
</UL>
<LI><A HREF="#Heading4">Moving, Resizing, and Tracking</A>
<LI><A HREF="#Heading5">Handling Multiple Objects and Object Selection</A>
<UL>
<LI><A HREF="#Heading6">Hit Testing</A>
<LI><A HREF="#Heading7">Drawing Multiple Items</A>
<LI><A HREF="#Heading8">Handling Single Clicks</A>
<LI><A HREF="#Heading9">Handling Double-Clicks</A>
</UL>
<LI><A HREF="#Heading10">Implementing Drag and Drop</A>
<UL>
<LI><A HREF="#Heading11">Implementing a Drag Source</A>
<LI><A HREF="#Heading12">Implementing a Drop Target</A>
<LI><A HREF="#Heading13">Registering the View as a Drop Target</A>
<LI><A HREF="#Heading14">Setting Up Function Skeletons and Adding Member Variables</A>
<LI><A HREF="#Heading15">OnDragEnter()</A>
<LI><A HREF="#Heading16">OnDragOver()</A>
<LI><A HREF="#Heading17">OnDragLeave()</A>
<LI><A HREF="#Heading18">OnDragDrop()</A>
<LI><A HREF="#Heading19">Testing the Drag Target</A>
</UL>
<LI><A HREF="#Heading20">Deleting an Object</A>
</UL>
<P>
<HR SIZE="4">
<CENTER>
<H1></H1>
</CENTER>
<P>You can obtain a rudimentary ActiveX container by asking AppWizard to make you
one, but it will have a lot of shortcomings. A far more difficult task is to understand
how an ActiveX container works and what you have to do to really use it. In this
chapter, by turning the ShowString application of earlier chapters into an ActiveX
container and then making it a truly functional container, you get a backstage view
of ActiveX in action. Adding drag-and-drop support brings your application into the
modern age of intuitive, document-centered user interface design. If you have not
yet read Chapter 13, "ActiveX Concepts," it would be a good idea to read
it before this one. As well, this chapter will not repeat all the instructions of
Chapter 8, "Building a Complete Application: ShowString," so you should
have read that chapter or be prepared to refer to it as you progress through this
one.</P>
<P>
<H2><A NAME="Heading1"></A>Changing ShowString</H2>
<P>ShowString was built originally in Chapter 8, "Building a Complete Application:
ShowString," and has no ActiveX support. You could make the changes by hand
to implement ActiveX container support, but there would be more than 30 changes.
It's quicker to build a new ShowString application--this time asking for ActiveX
container support--and then make changes to that code to get the ShowString functionality
again.</P>
<P>
<H3><A NAME="Heading2"></A>AppWizard-Generated ActiveX Container Code</H3>
<P>Build the new ShowString in a different directory, making almost exactly the same
AppWizard choices you used when you built it in the "Creating an Empty Shell
with AppWizard" section of Chapter 8. Name the project ShowString, choose an
MDI Application, No Database Support, compound document support: Container, a Docking
Toolbar, Initial Status Bar, Printing and Print Preview, Context Sensitive Help,
and 3D Controls. Finally, select Source File Comments and a Shared DLL. Finish AppWizard
and, if you want, build the project.tm1713714470</P>
<BLOCKQUOTE>
<P>
<HR>
<strong>NOTE:</strong> Even though the technology is now called ActiveX, the AppWizard dialog
boxes refer to compound document support. Also, many of the classnames that are used
throughout this chapter have Ole in their names, and comments refer to OLE. Although
Microsoft has changed the name of the technology, it has not propagated that change
throughout Visual C++ yet. You have to live with these contradictions for a while. 
<HR>
</BLOCKQUOTE>
<P>There are many differences between the application you just built and a do-nothing
application without ActiveX container support. The remainder of this section explains
these differences and their effects.</P>
<P><B>Menus  </B>There's another menu, called IDR_SHOWSTTYPE_CNTR_IP, shown
in Figure 14.1. The name refers to a container whose <I>contained</I> object is being
edited <I>in</I> <I>place</I>. During in-place editing, the menu bar is built from
the container's in-place menu and the server's in-place menu. The pair of vertical
bars in the middle of IDR_SHOWSTTYPE_CNTR_IP are separators; the server menu items
will be put between them. This is discussed in more detail in Chapter 15, "Building
an ActiveX Server Application."</P>
<P><A HREF="javascript:popUp('14uvc01.gif')"><B>FIG. 14.1</B></A><B> </B><I>AppWizard
adds another menu for editing in place.</I></P>
<P>The IDR_SHOWSTTYPE Edit menu, shown in Figure 14.2, has four new items:</P>
<P><A HREF="javascript:popUp('14uvc02.gif')"><B>FIG. 14.2</B></A><B> </B><I>AppWizard
adds items to the Edit menu of the IDR_SHOWSTTYPE resource.</I></P>
<P>
<UL>
<LI><B>Paste Special.</B> The user chooses this item to insert an item into the container
from the Clipboard.
<P>
<LI><B>Insert New Object.</B> Choosing this item opens the Insert Object dialog box,
shown in Figures 14.3 and 14.4, so the user can insert an item into the container.
</UL>
<P><A HREF="javascript:popUp('14uvc03.gif')"><B>FIG. 14.3</B></A><B> </B><I>The Insert
Object dialog box can be used to embed new objects.</I></P>
<P><A HREF="javascript:popUp('14uvc04.gif')"><B>FIG. 14.4</B></A><B> </B><I>The Insert
Object dialog box can be used to embed or link objects that are in a file.</I></P>
<UL>
<LI><B>Links.</B> When an object has been linked into the container, choosing this
item opens the Links dialog box, shown in Figure 14.5, to allow control of how the
copy of the object is updated after a change is saved to the file.
<P>
<LI><B><<OLE VERBS GO HERE>>.</B> Each kind of item has different verbs
associated with it, like Edit, Open, or Play. When a contained item has focus, this
spot on the menu is replaced by an object type like those in the Insert Object dialog
box, with a menu cascading from it that lists the verbs for this type, like the one
shown in Figure 14.6.
</UL>
<P><B><I>CShowStringApp</I>  </B>CShowStringApp::InitInstance() has several
changes from the InitInstance() method provided by AppWizard for applications that
aren't ActiveX containers. The lines in Listing 14.1 initialize the ActiveX (OLE)
libraries.</P>
<P>
<H4>Listing 14.1   Excerpt from ShowString.cpp--Library Initialization</H4>
<PRE> // Initialize OLE libraries
if (!AfxOleInit())
{
AfxMessageBox(IDP_OLE_INIT_FAILED);
return FALSE;
</PRE>
<PRE> }
</PRE>
<P><A HREF="javascript:popUp('14uvc05.gif')"><B>FIG. 14.5</B></A><B> </B><I>The Links
dialog box controls the way linked objects are updated.</I></P>
<P><A HREF="javascript:popUp('14uvc06.gif')"><B>FIG. 14.6</B></A><B> </B><I>Each
object type adds a cascading menu item to the Edit menu when it has focus.</I></P>
<P>Still in CShowStringApp::InitInstance(), after the MultiDocTemplate is initialized
but before the call to AddDocTemplate(), this line is added to register the menu
used for in-place editing:</P>
<P>
<PRE>pDocTemplate->SetContainerInfo(IDR_SHOWSTTYPE_CNTR_IP);
</PRE>
<P><B><I>CShowStringDoc</I>  </B>The document class, CShowStringDoc, now
inherits from COleDocument rather than CDocument. This line is also added at the
top of ShowStringDoc.cpp:</P>
<P>
<PRE>#include "CntrItem.h"
</PRE>
<P>CntrItem.h describes the container item class, CShowStringCntrItem, discussed
later in this chapter. Still in ShowStringDoc.cpp, the macros in Listing 14.2 have
been added to the message map.</P>
<P>
<H4>Listing 14.2   Excerpt from ShowString.cpp--Message Map Additions</H4>
<PRE> ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE,
¬COleDocument::OnUpdatePasteMenu)
ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE_LINK,
¬COleDocument::OnUpdatePasteLinkMenu)
ON_UPDATE_COMMAND_UI(ID_OLE_EDIT_CONVERT,
¬COleDocument::OnUpdateObjectVerbMenu)
ON_COMMAND(ID_OLE_EDIT_CONVERT,
¬COleDocument::OnEditConvert)
ON_UPDATE_COMMAND_UI(ID_OLE_EDIT_LINKS,
¬COleDocument::OnUpdateEditLinksMenu)
ON_COMMAND(ID_OLE_EDIT_LINKS,
¬COleDocument::OnEditLinks)
ON_UPDATE_COMMAND_UI(ID_OLE_VERB_FIRST, ID_OLE_VERB_LAST,
</PRE>
<PRE>¬COleDocument::OnUpdateObjectVerbMenu)
</PRE>
<P>These commands enable and disable the following menu items:</P>
<UL>
<LI>Edit, Paste
<P>
<LI>Edit, Paste Link
<P>
<LI>Edit, Links
<P>
<LI>The OLE verbs section, including the Convert verb
</UL>
<P>The new macros also handle Convert and Edit, Links. Notice that the messages are
handled by functions of COleDocument and don't have to be written by you.</P>
<P>The constructor, CShowStringDoc::CShowStringDoc(), has a line added:</P>
<P>
<PRE> EnableCompoundFile();
</PRE>
<P>This turns on the use of compound files. CShowStringDoc::Serialize() has a line
added as well:</P>
<P>
<PRE> COleDocument::Serialize(ar);
</PRE>
<P>This call to the base class Serialize() takes care of serializing all the contained
objects, with no further work for you.</P>
<P><B><I>CShowStringView</I>  </B>The view class, CShowStringView, includes
CntrItem.h just as the document does. The view class has these new entries in the
message map:</P>
<P>
<PRE> ON_WM_SETFOCUS()
ON_WM_SIZE()
ON_COMMAND(ID_OLE_INSERT_NEW, OnInsertObject)
ON_COMMAND(ID_CANCEL_EDIT_CNTR, OnCancelEditCntr)
</PRE>
<P>These are in addition to the messages caught by the view before it was a container.
These catch WM_SETFOCUS, WM_SIZE, the menu item Edit, Insert New Object, and the
cancellation of editing in place. An accelerator has already been added to connect
this message to the Esc key.</P>
<P>In ShowStringView.h, a new member variable has been added, as shown in Listing
14.3.</P>
<P>
<H4>Listing 14.3  Excerpt from ShowStringView.h--m_pSelection</H4>
<PRE> // m_pSelection holds the selection to the current
// CShowStringCntrItem. For many applications, such
// a member variable isn't adequate to represent a
// selection, such as a multiple selection or a selection
// of objects that are not CShowStringCntrItem objects.
// This selection mechanism is provided just to help you
// get started.
// TODO: replace this selection mechanism with one appropriate
// to your app.
</PRE>
<PRE> CShowStringCntrItem* m_pSelection;
</PRE>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -