📄 ch09.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1">
<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 += '</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">-->
<META NAME="GENERATOR" Content="Symantec Visual Page Mac 1.1.1">
<TITLE>Teach Yourself Visual C++ 6 in 21 Days -- Ch 9 -- Adding ActiveX Controls to Your Application88</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF">
<H1 ALIGN="CENTER"><IMG SRC="../button/sams.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM"
BORDER="0"><BR>
Teach Yourself Visual C++ 6 in 21 Days</H1>
<CENTER>
<P><A HREF="../ch08/ch08.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"
ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch10/ch10.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>
<H1 ALIGN="CENTER">- 9 -<BR>
Adding ActiveX Controls to Your Application</H1>
<H1></H1>
<UL>
<LI><A HREF="#Heading1">What Is an ActiveX Control?</A>
<UL>
<LI><A HREF="#Heading2">ActiveX and the IDispatch Interface</A>
<LI><A HREF="#Heading3">ActiveX Containers and Servers</A>
</UL>
<LI><A HREF="#Heading4">Adding an ActiveX Control to Your Project</A>
<UL>
<LI><A HREF="#Heading5">Registering the Control</A>
<LI><A HREF="#Heading6">Adding the Control to Your Dialog</A>
</UL>
<LI><A HREF="#Heading7">Using an ActiveX Control in Your Application</A>
<UL>
<LI><A HREF="#Heading8">Interacting with the Control</A>
<LI><A HREF="#Heading9">Responding to Control Events</A>
</UL>
<LI><A HREF="#Heading10">Summary</A>
<LI><A HREF="#Heading11">Q&A</A>
<LI><A HREF="#Heading12">Workshop</A>
<UL>
<LI><A HREF="#Heading13">Quiz</A>
<LI><A HREF="#Heading14">Exercise</A>
</UL>
</UL>
<P>
<HR SIZE="4">
<BR>
In today's application develop market, there are thousands of prebuilt components
that you can plug into your applications, extending the functionality of your applications
instantaneously. Originally the domain of Visual Basic programmers, now you can use
readily available ActiveX controls with just about any Windows programming language,
including Visual C++. Today you will learn how you can add ActiveX controls to your
Visual C++ applications, taking advantage of their existing functionality. Some of
the topics that you will cover today are</P>
<P>
<UL>
<LI>What ActiveX controls are and how they work.
<P>
<LI>How you can add ActiveX controls to your project workspace.
<P>
<LI>How you can use the ActiveX control in your Visual C++ application.
<P>
<LI>How to call the various methods associated with the ActiveX control.
<P>
<LI>How to handle events that are triggered by the ActiveX control.
</UL>
<H2><A NAME="Heading1"></A>What Is an ActiveX Control?</H2>
<P>An ActiveX control is a software component that can be plugged into many different
programs and used as if it were a native part of the program. It's similar to the
concept of separate stereo components. If you buy a new tape deck, you can just plug
it into the rest of your stereo and it works with everything else you already have.
ActiveX controls bring this same type of interoperability to software applications.</P>
<P>ActiveX used to be called OLE 2.0. OLE 2.0 was Microsoft's technology for combining
two or more applications to make them work as one (or at least to switch between
the various applications within the same application shell). This idea was an expansion
from the original OLE (Object Linking and Embedding) technology, which only enabled
you to combine documents created with different applications into a single document.
When revamping OLE technologies to work in a distributed environment (such as on
the Internet), Microsoft decided to also revamp the name. Thus, ActiveX was born.</P>
<P>
<H3><A NAME="Heading2"></A>ActiveX and the IDispatch Interface</H3>
<P>The ActiveX technology is built on top of Microsoft's COM (Component Object Model)
technology, utilizing its interface and interaction model for making ActiveX control
integration fairly seamless. The COM technology defines how ActiveX objects are constructed
and how their interfaces are designed. The ActiveX technology defines a layer that
is built on top of COM, what interfaces various objects should support, and how different
types of objects should interact.</P>
<BLOCKQUOTE>
<P>
<HR>
<STRONG>NOTE:</STRONG> Microsoft's COM technology defines how applications and components
can interact through the use of interfaces. An interface is like a function call
into an ActiveX component. However, COM specifies how that function call must be
built and called, and what supporting functionality must accom-pany the function
call.<BR>
There are interfaces, like the IUnknown interface, that are required in every COM
object, and which are used to query the component to find out what other interfaces
are supported by the component. Each interface supports a specific set of functionality;
you might have one interface to handle the visual appearance of the control, another
to control how the control appearance interacts with the surrounding application,
another that triggers events in the surrounding application, and so on.
<HR>
</BLOCKQUOTE>
<P>One of the key technologies in ActiveX controls is <I>automation</I>. Automation
enables an application embedded within another application to activate itself and
control its part of the user interface or document, making its changes and then shutting
itself down when the user moves on to another part of the application that isn't
controlled by the embedded application.</P>
<P>This process is what happens when you have an Excel spreadsheet embedded within
a Word document. If you click the spreadsheet, Excel becomes active and you can edit
the spreadsheet using Excel, even though you're still working in Word. Then, once
you finish making your changes to the spreadsheet, Excel closes itself down and you
can continue working in Word.</P>
<P>One of the keys to making automation work is a special interface called the IDispatch
(also known as the dispinterface) interface. The IDispatch interface consists of
a pointer to a table of available methods that can be run in the ActiveX control
or embedded application. These methods have ID numbers, called DISPIDs, which are
also loaded into a table that can be used to look up the ID for a specific method.
Once you know the DISPID for a specific method, you can call that method by calling
the Invoke method of the IDispatch interface, passing the DISPID to identify the
method to be run. Figure 9.1 shows how the IDispatch interface uses the Invoke method
to run methods in the ActiveX object.</P>
<P><A HREF="javascript:popUp('09fig01.gif')"><B>FIGURE 9.1.</B></A><B> </B><I>The
IDispatch ActiveX interface.</I></P>
<P><I></I>
<H3><A NAME="Heading3"></A>ActiveX Containers and Servers</H3>
<P>To embed one ActiveX object within another ActiveX object, you have to implement
the embedded object as an ActiveX <I>server</I>, and the object containing the first
object must be an ActiveX <I>container</I>. Any ActiveX object that can be embedded
within another is an ActiveX server, whether it is an entire application or just
a small ActiveX control. Any ActiveX object that can have other ActiveX objects embedded
within it is an ActiveX container.</P>
<BLOCKQUOTE>
<P>
<HR>
<STRONG>NOTE:</STRONG> Don't confuse the use of the terms <I>container </I>and <I>server
</I>with the term <I>client </I>in the previous figure. The client is the object
calling the other object's IDispatch interface. As you'll learn in a page or so,
both the container and server call the other's IDispatch interfaces, making each
one the client of the other.
<HR>
</BLOCKQUOTE>
<P>These two types of ActiveX objects are not mutually exclusive. An ActiveX server
can also be an ActiveX container. A good example of this concept is Microsoft's Internet
Explorer Web browser. Internet Explorer is implemented as an ActiveX server that
runs within an ActiveX container shell (that can also house Word, Excel, PowerPoint,
or any other ActiveX server application). At the same time that Internet Explorer
is an ActiveX server running within the browser shell, it can contain other ActiveX
controls.</P>
<P>ActiveX controls are a special instance of an ActiveX server. Some ActiveX servers
are also applications that can run on their own. ActiveX controls cannot run on their
own and must be embedded within an ActiveX container. By using ActiveX components
in your Visual C++ application, you automatically make your application an ActiveX
container.</P>
<P>Most of the interaction between the ActiveX container and an ActiveX control takes
place through three IDispatch interfaces. One of these IDispatch interfaces is on
the control, and it is used by the container to make calls to the various methods
that the ActiveX control makes available to the container.</P>
<P>The container provides two IDispatch interfaces to the control. The first of these
IDispatch interfaces is used by the control to trigger events in the container application.
The second interface is used to set properties of the control, as shown in Figure
9.2. Most properties of an ActiveX control are actually provided by the container
but are maintained by the control. When you set a property for the control, the container
calls a method in the control to tell the control to read the properties from the
container. Most of this activity is transparent to you because Visual C++ builds
a series of C++ classes around the ActiveX control's interfaces. You will interact
with the methods exposed by the C++ classes, not directly calling the control's IDispatch
interface.</P>
<P><A HREF="javascript:popUp('09fig02.gif')"><B>FIGURE 9.2.</B></A><B> </B><I>An
ActiveX container and control interact primarily through a few IDispatch interfaces.</I></P>
<P><I></I>
<H2><A NAME="Heading4"></A>Adding an ActiveX Control to Your Project</H2>
<P>Looking into how ActiveX controls work can be deceptive because of how easy it
really is to use them in your applications. Visual C++ makes it easy to add ActiveX
controls to your applications and even easier to use them. Before you begin adding
the ActiveX control to your application, let's create an application shell into which
you will add an ActiveX control:</P>
<P>
<DL>
<DT></DT>
<DD><B>1. </B>Create a new MFC AppWizard project named ActiveX.
<P>
<DT></DT>
<DD><B>2. </B>Use the same defaults on the AppWizard as in previous days, but leave
the check box for ActiveX Controls checked on the second AppWizard step. Give your
application the title ActiveX Controls.
<P>
<DT></DT>
<DD><B>3. </B>Once you generate an application shell, remove all the controls and
add a single command button.
<P>
<DT></DT>
<DD><B>4. </B>Set the button's ID to IDC_EXIT and its caption to E&xit.
<P>
<DT></DT>
<DD><B>5. </B>Using the Class Wizard, add a function to your command button on the
BN_CLICKED event message.
<P>
<DT></DT>
<DD><B>6. </B>Edit the function you just created, calling the OnOK function, as on
earlier days.
<P>
</DL>
<H3><A NAME="Heading5"></A>Registering the Control</H3>
<P>Before you add an ActiveX control to your dialog window, you need to register
the control, both with Windows and with Visual C++. There are two possible ways to
register the ActiveX control with Windows. The first way is to run any installation
routine that came with the ActiveX control. If you do not have an installation routine,
you need to register the control manually. To register the control manually, follow
these steps:</P>
<P>
<DL>
<DT></DT>
<DD><B>1. </B>Open a DOS shell.
<P>
<DT></DT>
<DD><B>2. </B>Change directory to where the ActiveX control is on your system.
<P>
<DT></DT>
<DD><B>3. </B>Run the regsvr32 command, specifying the name of the ActiveX control
as the only command-line argument. For instance, if you were registering a control
named MYCTL.OCX and it was located in your WINDOWS\SYSTEM directory, you would perform
the following:
<P>
</DL>
<PRE>C:\WINDOWS> CD system
</PRE>
<PRE>C:\WINDOWS\SYSTEM> regsvr32 MYCTL.OCX
</PRE>
<BLOCKQUOTE>
<P>
<HR>
<STRONG>CAUTION:</STRONG> It is preferable to run any installation routine that comes with
the control because registering the control manually might not enable the control
for development usage. Controls can be licensed for development or deployment. If
a control is licensed for deployment, you will not be able to use it in your Visual
C++ applications. This is a mechanism that protects control developers by requiring
that developers purchase a development license for controls; they can't just use
the controls they may have installed on their system with another application.
<HR>
<P>
<HR>
<STRONG>NOTE:</STRONG> COM and ActiveX objects store a lot of information in the <BR>
Windows Registry database. Whenever an application uses an ActiveX object, the operating
system refers to the information in the Windows Registry to find the object and to
determine whether the application can use the object in the way that it requested.
Using the regsvr32.exe utility to register an ActiveX control places most of the
required information about the control into the system Registry. However, there may
be additional information about the control that needs to be in the Registry for
the control to function properly.
<HR>
</BLOCKQUOTE>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -