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

📄 ch13.htm

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

<ol>

<li><P> Start Word and enter your text.</P>

<li><P> Click where you want the table to go.</P>

<li><P> Choose <U>I</U>nsert, <U>O</U>bject.</P>

<li><P> Select the Create From File tab.</P>

<li><P> Enter or select the file name as though this was a File Open dialog box.</P>

<li><P> Do not check the Lin<U>k</U> to File box.</P>

<li><P> Click OK.</P>

</ol>

<P>What's the difference? You'll see when you double-click the object to edit it. The menus and toolbars of Word disappear and are replaced with their Excel equivalents, as shown in Figure 13.6. Changes you make here are not made in the file you 
originally embedded. They are made in the copy of that file that has become part of your Word document.</P>

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

<P><I>Editing in place is the magic of OLE embedding.</I></P>

<P>You embed files into your documents if you plan to build a compound document and then use it as a self-contained whole, without using the individual parts again. Any changes you make do not affect any other files on your disk, not even the one you 
copied from in the first place. Embedding makes your document much larger than it was, but you can delete the original if space is a problem.</P>

<H3><B>Containers and Servers</B></H3>

<P>To embed or link one object into another, you need a <I>container</I> and a <I>server</I>. The container is the application into which the object is linked or embedded&#151;Word in these examples. The server is the application that made them, and that 
can be launched (perhaps in place) when the object is double-clicked&#151;Excel in these examples.</P>

<P>Why would you develop a container application? To save yourself work. Imagine you have a product already developed and in the hands of your users. It does a specific task like gets a sales team organized, or schedules games in a league sport, or 
calculates life insurance rates. Then your users tell you that they wish it had a spreadsheet capability, so they could do small calculations on-the-fly. How long will it take you to add that functionality? Do you really have time to learn how spreadsheet 
programs parse the functions that users type?</P>

<P>If your application is a container app, it doesn't take any time at all. Tell them to link or embed in an Excel sheet, and let Excel do the work. If they don't own a copy of Excel, they need some spreadsheet application that can be an ActiveX server. 
You get to piggyback on the effort of other developers.</P>

<P>It's not just spreadsheets, either. What if users want a scratch pad, a place to scribble a few notes? Let them embed a Word document. And for bitmaps and other illustrations? Microsoft Paint, or a more powerful graphics package if they have one and it 
can act as an ActiveX server. You don't have to concern yourself with adding functionality like this to your programs because you can just make your application a container and your users can embed whatever they want without any more work on your part.</P>


<P>Why would you develop a server application, then? Look back over the reasons for writing a container application. A lot of users are going to contact developers asking for a feature to be added, and be told they can have that feature 
immediately&#151;they just need an application that does spreadsheets, text, pictures, or whatever, and can act as an ActiveX server. If your application is an ActiveX server, people will buy it so that they can add its functionality to their container 
apps.</P>

<P>Together, container and server apps allow users to build the documents they want. They represent a move toward building block software and a document-centered approach to work. And if you want your application to carry the Windows 95 logo, it must be a 
server, a container, or both. But there is much more to ActiveX than just linking and embedding. </P>

<H3><B>Toward a More Intuitive User Interface</B></H3>

<P>What if the object you want to embed is not in a file, but is part of a document you have open at the moment? You may have already discovered that you can use the Clipboard to transfer ActiveX objects. For example, to embed part of a Word document into 
an Excel spreadsheet, you can follow these steps:</P>

<ol>

<li><P> Open Excel.</P>

<li><P> Open Word.</P>

<li><P> In Excel, select the portion you want to copy.</P>

<li><P> Choose <U>E</U>dit, <U>C</U>opy to copy the block onto the Clipboard.</P>

<li><P> Switch to Word and choose <U>E</U>dit, Paste <U>S</U>pecial.</P>

<li><P> Select the Paste radio button.</P>

<li><P> Select Microsoft Excel Worksheet from the list box.</P>

<li><P> Make sure that Display as Icon is not selected.</P>

<li><P> The dialog box should look like Figure 13.7. Click OK.</P>

</ol>

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

<P><I>The Paste Special dialog box is used to link or embed selected portions </I><I>of a document.</I></P>

<P>A copy of the block is now embedded into the spreadsheet. If you choose Paste Link, changes in the spreadsheet are reflected immediately in the Word document, not just when you save them. (You may have to click the selection in Word to get it updated.) 
This is true even if the spreadsheet document has no name and has never been saved. Try it yourself! This is certainly better than saving dummy files just to embed them into compound documents, then deleting them, isn't it?</P>

<P>Another way to embed part of a document into another is drag and drop. This is a user-interface paradigm that works in a variety of contexts. You click on something (an icon, a highlighted block of text, a selection in a list box) and hold the mouse 
button down while moving it. The thing you clicked moves with the mouse, and when you let go of the mouse button, it is dropped to the new location. That's very intuitive for things like moving or resizing windows, but now you can use it to do much, much 
more. For example, here's how that Excel-in-Word example would be done with drag and drop:</P>

<ol>

<li><P> Open Word and size it to less than full screen.</P>

<li><P> Open Excel and size it to less than full screen. If you can arrange the Word and Excel windows so they don't overlap, that's great.</P>

<li><P> In Excel, select the portion you want to copy by highlighting it with the mouse or cursor keys.</P>

<li><P> Click the border of the selected area (the thick black line) and hold.</P>

<li><P> Drag the block into the Word window and let go.</P>

</ol>

<P>The selected block is embedded into the Word document. If you double-click it, you are editing in place with Excel. Dragging and dropping also works within a document to move or copy a selection.</P>

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

<P>The block is moved by default, which means it is deleted from the Excel sheet. If you want a copy, hold down the Ctrl key while dragging, and release the mouse button before the Ctrl key.</P>

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

<P>You can also use drag and drop with icons. On your desktop, if you drag a file to a folder, it is moved there. (Hold down Ctrl while dragging to copy it.) If you drag it to a program icon, it is opened with that program. This is very useful when you 
have a document you use with two applications. For example, pages on the World Wide Web are HTML documents, often created with an HTML editor, but viewed with a World Wide Web browser like Netscape Navigator. If you double-click an HTML document icon, your 
browser is launched to view it. If you drag that icon onto the icon for your HTML editor, the editor is launched and opens the file you dragged. After you realize you can do this, you will find your work speeds up dramatically.</P>

<P>All of this is ActiveX, and all of this requires a little bit of work from programmers to make it happen. So what's going on?</P>

<H3><B>The Component Object Model</B></H3>

<P>The heart of modern ActiveX is the Component Object Model. This is an incredibly complex topic that deserves a book of its own.  Luckily, the Microsoft Foundation Classes and the Visual C++ AppWizard do much of the behind-the-scenes work for you, and 
so the discussion in these chapters is just what you need to know to use OLE as a developer.</P>

<P>The Component Object Model (COM) is a binary standard for Windows objects. That means that the executable code (in a .DLL or .EXE) that describes an object can be executed by other objects. Even if two objects were written in different languages, they 
are able to interact using the COM standard.</P>

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

<P>Because the code in a DLL executes in the same process as the calling code, it's the fastest way for applications to communicate. When two separate applications communicate through COM, function calls from one application to another must be 
<I>marshaled</I>: COM gathers up all the parameters and invokes the function itself. A standalone server (.EXE) is therefore slower than an in-process server (.DLL).</P>

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

<P>How do they interact? Through an <I>interface</I>. An ActiveX interface is a collection of functions, or really just function names. It's a C++ class with no data, only pure virtual functions. Your objects inherit from this class and provide code for 
the functions. (Remember, as discussed in Reference A, &quot;C++ Review and (O O Concepts),&quot; a class that inherits a pure virtual function does not inherit code for that function.) Other programs get to your code by calling these functions. All 
ActiveX objects must have an interface called <font color="#008000">IUnknown</font> (and usually have many more, all with names that start with <font color="#008000">I</font>, the prefix for interfaces).</P>

<P>The <font color="#008000">IUnknown</font> interface has only one purpose:finding other interfaces. It has a function called <font color="#008000">QueryInterface()</font> that takes an interface ID and returns a pointer to that interface for this 
object. All the other interfaces inherit from <font color="#008000">IUnknown</font>, so they have a <font color="#008000">QueryInterface()</font> too, and you have to write the code, or you would if there was no MFC. MFC implements a number of macros that 
simplify the job of writing interfaces and their functions, as you will shortly see. The full declaration of <font color="#008000">IUnknown</font> is in Listing 13.1. The macros take care of some of the work of declaring an interface and won't be discussed 
here. There are three functions declared: <font color="#008000">QueryInterface()</font>, <font color="#008000">AddRef()</font>, and <font color="#008000">Release()</font>. These latter two functions are used to keep track of which applications are using an 
interface. All three of these functions are inherited by all interfaces and must be implemented by the developer of the interface.</P>

<P><I>Listing 13.1&#151;IUnknown, defined in \DevStudio\vc\include\unknwn.h</I></P>

<pre><font color="#008000">interface IUnknown</font></pre>

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

<pre><font color="#008000">    public:</font></pre>

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

<pre><font color="#008000">        virtual HRESULT STDMETHODCALLTYPE QueryInterface( </font></pre>

<pre><font color="#008000">            /* [in] */ REFIID riid,</font></pre>

<pre><font color="#008000">            /* [iid_is][out] */ void __RPC_FAR *__RPC_FAR *ppvObject) = 0;</font></pre>

⌨️ 快捷键说明

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