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

📄 ch14.htm

📁 VC使用大全。里面集合了VC使用的各种使用技巧。非常有用。
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<HTML>

<HEAD>

<TITLE>Special Edition Using Visual C++ 5 - Chapter 14</TITLE>

<LINK REL="Next" HREF="ch15.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/ch15.htm">

<LINK REL="Previous" HREF="ch13.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/ch13.htm"></HEAD>

<BODY BGCOLOR="#FFFFFF" TEXT="#000000">



<H2><A ID="I1" NAME="I1"><B>Chapter 1</B></A><B>4</B></H2>

<H2><A ID="I2" NAME="I2"><B>Building an ActiveX Container Application</B></A></H2>

<hr>

<P>You can get a rudimentary ActiveX container by asking AppWizard to make you one, but it will have a lot of shortcomings. A far harder 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.</P>

<ul>

<li> <B>Transforming ShowString into a container</B></P>

<P>  An ActiveX container can contain documents that were created in another application. This section shows you how to build one, and how they work.</P>

<li> <B>Moving, resizing, and tracking the contained object</B></P>

<P>  The container code generated for you does not handle some of the user interface tasks well. This section shows you how to handle those tasks yourself.</P>

<li> <B>Handling multiple objects and object selection</B></P>

<P>  Improving your container so that it can contain more than one object and letting the user click an object to select it is another enhancement to your user interface.</P>

<li> <B>Implementing drag and drop</B></P>

<P>  Users love drag and drop. This section shows you how to implement drag and drop in your ActiveX container.</P>

<li> <B>Deleting a contained object</B></P>

<P>  A final improvement to your user interface is enabling the user to delete an object that has been embedded or linked into the container.</P>

</ul>

<H3><A ID="I3" NAME="I3"><B>Changing ShowString</B></A></H3>

<P>ShowString was built originally in <A HREF="index09.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index09.htm" target="text">Chapter 9</A>, &quot;Building a Complete Application: ShowString,&quot; 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&#151;this time asking for ActiveX container support&#151;and then make changes to that code to get the ShowString functionality again.</P>

<P><A ID="I4" NAME="I4"><B>AppWizard Generated ActiveX Container Code</B></A></P>

<P>Build the new ShowString in a different directory, making almost exactly the same AppWizard choices as before: call it ShowString, choose an MDI application, no database support, compound document support: container, a docking toolbar, status bar, 
printing and print preview, context sensitive Help, and 3D controls. Finally, select source file comments and a shared DLL.</P>

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

<P>Even though the technology is now called ActiveX, the AppWizard dialogs refer to compound document support. Also, many of the class names that are used throughout this chapter have <font color="#008000">ole</font> in their names, and comments refer to 
OLE. While 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 until the next release of Visual C++.</P>

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

<P>There are a lot of differences between the application you have just built and a do-nothing application without ActiveX container support. The remainder of this section describes and explains these differences and their effects.</P>

<P><A ID="I5" NAME="I5"><B>Menus</B></A></P>

<P>There's another menu, called <font color="#008000">IDR_SHOWSTTYPE_CNTR_IP</font>, 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 <font color="#008000">IDR_SHOWSTTYPE_CNTR_IP</font> are separators; the server menu items will be put between them. This is discussed in more 
detail in <A HREF="index15.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index15.htm" target="text">Chapter 15</A>, &quot;Building an ActiveX Server Application.&quot;</P>

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

<P><I>AppWizard adds another menu for editing in place.</I></P>

<P>The <U>E</U>dit menu, shown in Figure 14.2, has four new items:</P>

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

<P><I>AppWizard adds items to the Edit menu of the </I><I><font color="#008000">IDR_SHOWSTTYPE</font></I><I> resource.</I></P>

<ul>

<li> <B>Paste </B><B><U>S</U></B><B>pecial.</B> The user chooses this item to insert an item into the container from the Clipboard.</P>

<li> <B>Insert </B><B><U>N</U></B><B>ew Object.</B> Choosing this item brings up the Insert Object dialog box, shown in Figures 14.3 and 14.4, so the user can insert an item into the container.</P>

</ul>

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

<P><I>The Insert Object dialog box can be used to embed new objects.</I></P>

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

<P><I>The Insert Object dialog box can be used to embed or link objects that are in a file.</I></P>

<ul>

<li> <B>Lin</B><B><U>k</U></B><B>s.</B> When an object has been linked into the container, choosing this item brings up the Links dialog box, shown in Figure 14.5, to allow control of the way that the copy of the object is updated after a change is saved 
to the file.</P>

</ul>

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

<P><I>The Links dialog box controls the way linked objects are updated.</I></P>

<ul>

<li> <B>&lt;&lt;OLE VERBS GO HERE&gt;&gt;.</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.</P>

</ul>

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

<P><I>Each object type adds a cascading menu item to the Edit menu when it has focus.</I></P>

<P><A ID="I7" NAME="I7"></A><I>CShowStringApp</I></P>

<p><font color="#008000">CShowStringApp::InitInstance()</font> has several changes from the <font color="#008000">InitInstance()</font> method provided by AppWizard for applications that are not ActiveX containers. The lines in Listing 14.1 initialize the 
ActiveX (OLE) libraries.</P>

<P><I>Listing 14.1&#151;Excerpt from ShowString.cpp&#151;Library initialization</I></P>

<pre><font color="#008000">     // Initialize OLE libraries</font></pre>

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

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

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

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

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

<P>Still in <font color="#008000">CShowStringApp::InitInstance()</font>, after the <font color="#008000">MultiDocTemplate</font> is initialized, but before the call to <font color="#008000">AddDocTemplate()</font>, this line is added to register the menu 
used for in-place editing:</P>

<pre><font color="#008000">pDocTemplate-&gt;SetContainerInfo(IDR_SHOWSTTYPE_CNTR_IP);</font></pre>

<P><A ID="I8" NAME="I8"><I>CShowStringDoc</I></A></P>

<P>The document class, <font color="#008000">CShowStringDoc</font>, now inherits from <font color="#008000">COleDocument</font> rather than <font color="#008000">CDocument</font>. This line is also added at the top of ShowStringDoc.cpp:</P>

<pre><font color="#008000">#include &quot;CntrItem.h&quot;</font></pre>

<p><font color="#008000">CntrItem.h</font> describes the container item class, <font color="#008000">CShowStringCntrItem</font>, discussed later. Still in ShowStringDoc.cpp, the macros in Listing 14.2 have been added to the message map.</P>

<P><I>Listing 14.2&#151;Excerpt from ShowString.cpp&#151;Message Map Additions</I></P>

<pre><font color="#008000">     ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE, </font></pre>

<pre><font color="#008000">[ccc]COleDocument::OnUpdatePasteMenu)</font></pre>

<pre><font color="#008000">     ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE_LINK, </font></pre>

<pre><font color="#008000">[ccc]COleDocument::OnUpdatePasteLinkMenu)</font></pre>

<pre><font color="#008000">     ON_UPDATE_COMMAND_UI(ID_OLE_EDIT_CONVERT, </font></pre>

<pre><font color="#008000">[ccc]COleDocument::OnUpdateObjectVerbMenu)</font></pre>

<pre><font color="#008000">     ON_COMMAND(ID_OLE_EDIT_CONVERT, </font></pre>

<pre><font color="#008000">[ccc]COleDocument::OnEditConvert)</font></pre>

<pre><font color="#008000">     ON_UPDATE_COMMAND_UI(ID_OLE_EDIT_LINKS, </font></pre>

<pre><font color="#008000">[ccc]COleDocument::OnUpdateEditLinksMenu)</font></pre>

<pre><font color="#008000">     ON_COMMAND(ID_OLE_EDIT_LINKS, </font></pre>

<pre><font color="#008000">[ccc]COleDocument::OnEditLinks)</font></pre>

<pre><font color="#008000">     ON_UPDATE_COMMAND_UI(ID_OLE_VERB_FIRST, </font></pre>

<pre><font color="#008000">[ccc]COleDocument::OnUpdateObjectVerbMenu)</font></pre>

<P>These commands enable and disable the following menu items:</P>

<ul>

<li> <U>E</U>dit, <U>P</U>aste</P>

<li> <U>E</U>dit, Paste Link</P>

<li> <U>E</U>dit, <U>L</U>inks</P>

<li> The OLE verbs section, including the <U>C</U>onvert verb</P>

</ul>

<P>The new macros also handle <U>C</U>onvert and <U>E</U>dit, <U>L</U>inks. Notice that the messages are handled by functions of <font color="#008000">COleDocument</font> and don't have to be written by you.</P>

<P>The constructor, <font color="#008000">CShowStringDoc::CShowStringDoc()</font>, has a line added:</P>

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

<P>This turns on the use of compound files. <font color="#008000">CShowStringDoc::Serialize()</font> has a line added as well:</P>

<pre><font color="#008000">     COleDocument::Serialize(ar);</font></pre>

<P>This call to the base class <font color="#008000">Serialize()</font> takes care of serializing all the contained objects, with no further work for you.</P>

<P><A ID="I9" NAME="I9"><I>CShowStringView</I></A></P>

<P>The view class, <font color="#008000">CShowStringView</font>, includes <font color="#008000">CntrItem.h</font> just as the document does. The view class has these new entries in the message map:</P>

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

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

<pre><font color="#008000">     ON_COMMAND(ID_OLE_INSERT_NEW, OnInsertObject)</font></pre>

<pre><font color="#008000">     ON_COMMAND(ID_CANCEL_EDIT_CNTR, OnCancelEditCntr)</font></pre>

<P>These are in addition to the messages caught by the view before it was a container. These catch <font color="#008000">WM_SETFOCUS</font>, <font color="#008000">WM_SIZE</font>, the menu item <U>E</U>dit, Insert <U>N</U>ew Object, and the cancellation of 
editing in place. An accelerator has already been added to connect this message to <font color="#008000">Esc</font>.</P>

<P>In <font color="#008000">ShowStringView.h</font>, a new member variable has been added, as shown in Listing 14.3.</P>

<P><I>Listing 14.3&#151;Excerpt from </I>ShowStringView.h<I>&#151;</I>m_pSelection</P>

<pre><font color="#008000">     // m_pSelection holds the selection to the current </font></pre>

<pre><font color="#008000">     // CShowStringCntrItem. For many applications, such </font></pre>

<pre><font color="#008000">     // a member variable isn't adequate to represent a </font></pre>

<pre><font color="#008000">     // selection, such as a multiple selection or a selection</font></pre>

<pre><font color="#008000">     // of objects that are not CShowStringCntrItem objects.  </font></pre>

<pre><font color="#008000">     // This selection mechanism is provided just to help you </font></pre>

⌨️ 快捷键说明

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