📄 ch13.htm
字号:
<pre><font color="#008000"> </font></pre>
<pre><font color="#008000"> virtual ULONG STDMETHODCALLTYPE AddRef( void) = 0;</font></pre>
<pre><font color="#008000"> </font></pre>
<pre><font color="#008000"> virtual ULONG STDMETHODCALLTYPE Release( void) = 0;</font></pre>
<pre><font color="#008000"> </font></pre>
<pre><font color="#008000"> END_INTERFACE</font></pre>
<pre><font color="#008000"> };</font></pre>
<H3><B>ActiveX Automation</B></H3>
<P>An ActiveX Automation application lets other applications tell it what to do. It <I>exposes</I> functions and data, called <I>methods</I> and <I>properties</I>. For example, Microsoft Excel is an ActiveX Automation object, and programs written in
Visual C++ or Visual Basic can call Excel functions and set properties like column widths. That means you don't need to write a scripting language for your application any more. If you expose all the functions and properties of your application, any
programming language that can use an ActiveX Automation application can be a scripting language for your application. Your users, your customers, may already know your scripting language. They essentially will have no learning curve for writing macros to
automate your application (though they will need to learn the names of the methods and properties you expose).</P>
<P>The important thing to know about interacting with ActiveX Automation is that one program is always in control, calling the methods or changing the properties of the other running application. The application in control is called an ActiveX Automation
controller. The application that exposes methods and functions is called an ActiveX Automation server. Excel, Word, and other members of the Microsoft Office suite are ActiveX Automation servers, and your programs can use the functions of these
applications to really save you coding time.</P>
<P>For example, imagine being able to use the function called by the Word menu item F<U>o</U>rmat, Change Ca<U>s</U>e to convert the blocks of text your application uses to all uppercase, all lowercase, sentence case (the first letter of the first word in
each sentence is uppercase, the rest are not), or title case (the first letter of every word is uppercase, the rest are not).</P>
<P>The description of how ActiveX Automation really works is far longer and more complex than the interface summary of the previous section. It involves a special interface called <font color="#008000">IDispatch</font>, a simplified interface that works
from a number of different languages, including those like Visual Basic that can't use pointers. The declaration of <font color="#008000">IDispatch</font> is in Listing 13.2.</P>
<P><I>Listing 13.2—IDispatch, defined in \DevStudio\vc\include\oaidl.h</I></P>
<pre><font color="#008000">interface IDispatch : public IUnknown</font></pre>
<pre><font color="#008000"> {</font></pre>
<pre><font color="#008000"> public:</font></pre>
<pre><font color="#008000"> virtual HRESULT STDMETHODCALLTYPE GetTypeInfoCount( </font></pre>
<pre><font color="#008000"> /* [out] */ UINT __RPC_FAR *pctinfo) = 0;</font></pre>
<pre><font color="#008000"> </font></pre>
<pre><font color="#008000"> virtual HRESULT STDMETHODCALLTYPE GetTypeInfo( </font></pre>
<pre><font color="#008000"> /* [in] */ UINT iTInfo,</font></pre>
<pre><font color="#008000"> /* [in] */ LCID lcid,</font></pre>
<pre><font color="#008000"> /* [out] */ ITypeInfo __RPC_FAR *__RPC_FAR *ppTInfo) = 0;</font></pre>
<pre><font color="#008000"> </font></pre>
<pre><font color="#008000"> virtual HRESULT STDMETHODCALLTYPE GetIDsOfNames( </font></pre>
<pre><font color="#008000"> /* [in] */ REFIID riid,</font></pre>
<pre><font color="#008000"> /* [size_is][in] */ LPOLESTR __RPC_FAR *rgszNames,</font></pre>
<pre><font color="#008000"> /* [in] */ UINT cNames,</font></pre>
<pre><font color="#008000"> /* [in] */ LCID lcid,</font></pre>
<pre><font color="#008000"> /* [size_is][out] */ DISPID __RPC_FAR *rgDispId) = 0;</font></pre>
<pre><font color="#008000"> </font></pre>
<pre><font color="#008000"> virtual /* [local] */ HRESULT STDMETHODCALLTYPE Invoke( </font></pre>
<pre><font color="#008000"> /* [in] */ DISPID dispIdMember,</font></pre>
<pre><font color="#008000"> /* [in] */ REFIID riid,</font></pre>
<pre><font color="#008000"> /* [in] */ LCID lcid,</font></pre>
<pre><font color="#008000"> /* [in] */ WORD wFlags,</font></pre>
<pre><font color="#008000"> /* [out][in] */ DISPPARAMS __RPC_FAR *pDispParams,</font></pre>
<pre><font color="#008000"> /* [out] */ VARIANT __RPC_FAR *pVarResult,</font></pre>
<pre><font color="#008000"> /* [out] */ EXCEPINFO __RPC_FAR *pExcepInfo,</font></pre>
<pre><font color="#008000"> /* [out] */ UINT __RPC_FAR *puArgErr) = 0;</font></pre>
<pre><font color="#008000"> </font></pre>
<pre><font color="#008000"> };</font></pre>
<P>Although <font color="#008000">IDispatch</font> seems more complex than <font color="#008000">IUnknown</font>, it declares only a few more functions: <font color="#008000">GetTypeInfoCount()</font>, <font color="#008000">GetTypeInfo()</font>, <font
color="#008000">GetIDsOfNames()</font>, and <font color="#008000">Invoke()</font>. Sice it inherits from Iunknown, it has also inherited <font color="#008000">QueryInterface()</font>, <font color="#008000">AddRef()</font>, and <font
color="#008000">Release()</font>. They are all pure virtual functions, so any COM class that inherits from IDispatch must implement these functions. The most important of these is <font color="#008000">Invoke()</font>, used to call functions of the
Automation server and to access its properties.</P>
<H3><B>ActiveX Controls</B></H3>
<P>ActiveX controls are tiny little ActiveX Automation servers that load <I>in </I><I>process</I>. That means they are remarkably fast. They were originally called OLE Custom Controls and were designed to replace VBX controls, 16-bit controls written for
use in Visual Basic and Visual C++. (There are a number of good technical reasons why the VBX technology could not be extended to the 32-bit world.) Since OLE Custom Controls were traditionally kept in files with the extension .OCX, many people referred to
an OLE Custom Control as an OCX control or just an OCX. Although the OLE has been supplanted by ActiveX, ActiveX controls produced by Visual C++ 4.2 are still kept in files with the .OCX extension.</P>
<P>The original purpose of VBX controls was to allow programmers to provide unusual interface controls to their users. Controls that looked like gas gauges or volume knobs became easy to develop. But almost immediately, VBX programmers moved beyond simple
controls to modules that involved significant amounts of calculation and processing. In the same way, many ActiveX controls are far more than just controls; they are <I>components</I> that can be used to build powerful applications quickly and easily.</P>
<blockquote><p><img src="note.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/note.gif">
<P>If you have built an OCX in earlier versions of Visual C++, you might think it is a difficult thing to do. The Control Developer Kit, now integrated into Visual C++, takes care of the ActiveX aspects of the job and allows you to concentrate on the
calculations, display, or whatever else it is that makes your control worth using. The ActiveX Control Wizard makes getting started with an empty ActiveX control simple.</P>
<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>
<P>Because controls are little ActiveX Automation servers, they need to be used by an ActiveX Automation controller, but the terminology is too confusing if there are controls and controllers, so we say that ActiveX controls are used by <I>container</I>
applications. Visual C++ and Visual Basic are both container applications, as are many members of the Office suite and many non-Microsoft products.</P>
<P>In addition to properties and methods, ActiveX Controls have <I>events</I>. To be specific, a control is said to <I>fire</I> an event, and it does so when there is something that the container needs to be aware of. For example, when the user clicks a
portion of the control, the control deals with it, perhaps changing its appearance or making a calculation, but it may also need to pass on word of that click to the container application so that a file can be opened or some other container action can be
performed.</P>
<H3><B>From Here...</B></H3>
<P>This chapter has given you a brief tour through the concepts and terminology used in ActiveX technology, and a glimpse of the power you can add to your applications by incorporating ActiveX into them. The remainder of the chapters in this part work you
through the creation of ActiveX applications using MFC and the wizards in Visual C++. Check out the following:</P>
<ul>
<li> <A HREF="index14.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index14.htm" target="text">Chapter 14</A>, "Building an ActiveX Container Application," demonstrates a simple ActiveX container, a program that can contain embedded or linked objects.</P>
<li> <A HREF="index15.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index15.htm" target="text">Chapter 15</A>, "Building an ActiveX Server Application," demonstrates a simple ActiveX server. This application can create objects that can be embedded or linked into ActiveX container applications.</P>
<li> <A HREF="index16.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index16.htm" target="text">Chapter 16</A>, "Building an Automation Server," builds an ActiveX Automation object that can use Visual Basic as its scripting language.</P>
<li> <A HREF="index17.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index17.htm" target="text">Chapter 17</A>, "Building an ActiveX Control," builds an ActiveX control that can be embedded into a Visual C++ or Visual Basic program.</P>
<li> <A HREF="index20.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index20.htm" target="text">Chapter 20</A>, "Building an Internet ActiveX Control," modifies the control built in <A HREF="index17.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index17.htm" target="text">Chapter 17</A> to work over the Internet.</P>
<li> <A HREF="index21.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index21.htm" target="text">Chapter 21</A>, "The Active Template Library," demonstrates another way to build ActiveX controls.</P>
</ul>
<p><hr></p>
<center>
<p><font size=-2>
© 1997, QUE Corporation, an imprint of Macmillan Publishing USA, a
Simon and Schuster Company.</font></p>
</center>
</BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -