📄 ch20.htm
字号:
<HTML>
<HEAD>
<TITLE>Special Edition Using Visual C++ 5 - Chapter 20</TITLE>
<LINK REL="Next" HREF="ch21.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/ch21.htm">
<LINK REL="Previous" HREF="ch19.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/ch19.htm"></HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H2><A ID="I1" NAME="I1"><B>Chapter 20</B></A></H2>
<H2><A ID="I2" NAME="I2"></A><B>Building an Internet ActiveX Control</B></H2>
<hr>
<P>In <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," you learned how to build your own controls and include them in forms-based applications written in Visual Basic, Visual C++, and the VBA macro language.
There's one other place those controls can go—on a web page. But the ActiveX controls generated by older versions of Visual C++ were too big and slow to put on a web page. This chapter shows how to get these controls onto your web pages, and how to
write faster, sleeker controls that will make your pages a pleasure to use.</P>
<ul>
<li> <B>How Microsoft Explorer loads ActiveX controls</B></P>
<P> When you see a web page with an ActiveX control in it, your jaw will drop!</P>
<li> <B>How Netscape Navigator loads ActiveX controls</B></P>
<P> Yes, it's true, Netscape Navigator can display an ActiveX control. This section points you to the plug-in your users will need.</P>
<li> <B>ActiveX controls versus Java applets</B></P>
<P> There's more than one way to allow a user to interact with a web page. We compare ActiveX controls to Java applets.</P>
<li> <B>AppWizard's Advanced Control options</B></P>
<P> AppWizard's ActiveX ControlWizard, can add some nice optimizations to your controls.</P>
<li> <B>Asynchronous properties</B></P>
<P> The last thing you want your control to do is slow down your web page. The second to last thing you want is for your control to be boring.</P>
</ul>
<H3><A ID="I3" NAME="I3"><B>Embedding an ActiveX Control into a Microsoft Explorer Web Page</B></A></H3>
<P>It's a remarkably simple matter to put an ActiveX control onto a web page that you know will be loaded by Microsoft Explorer 3.0. You use the <font color="#008000"><OBJECT></font> tag, a relatively new addition to HTML that describes a wide
variety of objects that you might want to insert into a web page: a moving video clip, a sound, a Java applet, an ActiveX control, and many more kinds of information and ways of interacting with a user. Listing 20.1 shows the HTML source for a page that
displays the Dieroll control from <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."</P>
<P><I>Listing 20.1—fatdie.html—Using </I><OBJECT></P>
<pre><font color="#008000"><HEAD></font></pre>
<pre><font color="#008000"><TITLE>A Web page with a rolling die</TITLE></font></pre>
<pre><font color="#008000"></HEAD></font></pre>
<pre><font color="#008000"><BODY></font></pre>
<pre><font color="#008000"><OBJECT ID="Dieroll1"</font></pre>
<pre><font color="#008000">CLASSID="CLSID:46646B43-EA16-11CF-870C-00201801DDD6"</font></pre>
<pre><font color="#008000">CODEBASE="dieroll.cab#Version=1,0,0,1"</font></pre>
<pre><font color="#008000">WIDTH="200"</font></pre>
<pre><font color="#008000">HEIGHT="200"></font></pre>
<pre><font color="#008000"><PARAM NAME="ForeColor" VALUE="0"></font></pre>
<pre><font color="#008000"><PARAM NAME="BackColor" VALUE="16777215"></font></pre>
<pre><font color="#008000">If you see this text, your browser does not support the OBJECT tag.</font></pre>
<pre><font color="#008000"><BR></font></pre>
<pre><font color="#008000"></OBJECT></font></pre>
<pre><font color="#008000"><BR></font></pre>
<pre><font color="#008000">Here is some text after the die</font></pre>
<pre><font color="#008000"></BODY></font></pre>
<pre><font color="#008000"></HTML></font></pre>
<P>The only ugly thing here is the <font color="#008000">CLSID</font>, and the easiest way to get that, since you're a software developer, is to cut and paste it from dieroll.odl, the Object Description Library. Use <font color="#008000">FileView</font>
to open dieroll.odl quickly. Here's the section in dieroll.odl that includes the <font color="#008000">CLSID</font>:</P>
<pre><font color="#008000"> // Class information for CDierollCtrl</font></pre>
<pre><font color="#008000"> [ uuid(46646B43-EA16-11CF-870C-00201801DDD6),</font></pre>
<pre><font color="#008000"> helpstring("Dieroll Control"), control ]</font></pre>
<P>This section is at the end of dieroll.odl—the earlier <font color="#008000">CLSID</font>s do not refer to the whole control, only to portions of it. Copy the <font color="#008000">uuid</font> from inside the brackets into your HTML source.</P>
<blockquote><p><img src="tip.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/tip.gif">
<P>Microsoft has a product called the Control Pad that gets <font color="#008000">CLSID</font>s from the Registry for you and makes life easier for web page builders who are intimidated by instructions like "open the ODL file" or who don't have
the ODL file, since it's not shipped with the control. Since you're building this control, and know how to open files in Developer Studio, this chapter will not describe the Control Pad tool. If you're curious, see Microsoft's Control Pad web page at <A
HREF="tppmsgs/msgs0.htm#20" tppabs="http://www.microsoft.com/workshop/author/cpad/" target="_top"><B>http://www.microsoft.com/workshop/author/cpad/</B></A> for more details.</P>
<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>
<P>The <font color="#008000">CODEBASE</font> attribute of the <font color="#008000">OBJECT</font> tag specifies where the OCX file is kept, so that if the user does not have a copy of the ActiveX control, one will be downloaded automatically. The use of
the <font color="#008000">CLSID</font> means that if this user has already installed this ActiveX control, there is no download time, the control is simply used right away. You can simply specify a URL to the OCX file, but in order to automate the DLL
downloading, this <font color="#008000">CODEBASE</font> attribute points to a CAB file. Putting your control into a CAB file will cut your download time to as little as half what it otherwise would be. You can learn more about CAB technology at <A
HREF="tppmsgs/msgs0.htm#21" tppabs="http://www.microsoft.com/intdev/cab/" target="_top"><B>http://www.microsoft.com/intdev/cab/</B></A>. That page is written for Java developers, but the technology works just as well to cut the download time for ActiveX controls.</P>
<blockquote><p><img src="tip.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/tip.gif">
<P>If you don't have access to a web server in which you can put controls while you're developing them, use a <font color="#008000">file://</font> URL in the <font color="#008000">CODEBASE</font> attribute that points to the location of the control on
your hard drive.</P>
<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>
<P>The remainder of the attributes of the <font color="#008000">OBJECT</font> tag should be fairly intuitive if you've built a web page before: <font color="#008000">ID</font> is used by other tags on the page to refer to this control, <font
color="#008000">WIDTH</font> and <font color="#008000">HEIGHT</font> specify the size, in pixels, that the control should appear, and <font color="#008000">HSPACE</font> and <font color="#008000">VSPACE</font> are horizontal and vertical blank spaces, in
pixels, around the entire control.</P>
<P>Everything after the <font color="#008000"><OBJECT ...></font> tag and before the <font color="#008000"></OBJECT></font> tags is ignored by browsers that understand the <font color="#008000">OBJECT</font> tag. (The <font
color="#008000"><OBJECT...></font> tag is usually many lines long and contains all the information to describe the object.) Browsers that do not understand the <font color="#008000">OBJECT</font> tag ignore the <font color="#008000"><OBJECT
...></font> tag and the <font color="#008000"></OBJECT></font> tag and display the HTML between them (in this case, a line of text pointing out that this browser does not support the tag). This is part of the specification for a web browser: It
should ignore tags it doesn't understand.</P>
<P>Figure 20.1 shows this page displayed in Microsoft Explorer 3.0. Clicking on the die rolls it, and everything works beautifully. Things certainly look simple and amazing, but two flaws appear immediately:</P>
<ul>
<li> Not all browsers support the <font color="#008000">OBJECT</font> tag.</P>
<li> It can take a long time to download the control.</P>
</ul>
<A HREF="Vfigs01.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch20/Vfigs01.gif"><b>Fig. 20.1</b></A>
<P><I>Microsoft Internet Explorer can show ActiveX controls.</I></P>
<P>Figure 20.2 shows the same page displayed in Netscape Navigator 2.0. It doesn't support the <font color="#008000">OBJECT</font> tag, so it doesn't show the die. And Netscape Navigator is used by well over half of the people who browse the web! Does
that mean it's not worth writing ActiveX controls for web pages? Not at all. As you'll see in the very next section, there's a way that Navigator users can use the same controls as Explorer users.</P>
<A HREF="Vfigs02.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch20/Vfigs02.gif"><b>Fig. 20.2</b></A>
<P><I>Netscape Navigator cannot show ActiveX controls.</I></P>
<P>The size issue is a bigger worry. The release version of the Dieroll control, as built for <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," is 26 K. Many designers put a 50K limit per web page for graphics
and other material to be downloaded, and this simple control uses half that limit. A more powerful control would easily exceed it. The majority of this chapter deals with ways to reduce that size or otherwise minimize the download time for ActiveX
controls. Web page designers will then be able to tap the controls’ full power without worrying that users will label their pages as "slow," one of the worst knocks against any web site.</P>
<P>There's a third flaw that you won't notice because you have Visual C++ installed on your computer. The control requires the MFC DLL. The user must download it and install it before the controls can run. The mechanism that automatically downloads and
installs controls does not automatically download and install this DLL, though using a CAB file as discussed earlier can make it possible.</P>
<blockquote><p><img src="tip.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/tip.gif">
<P>For an example of a web page that includes a cab file for the Dieroll control and the MFC DLLs, come to <A HREF="tppmsgs/msgs0.htm#22" tppabs="http://www.gregcons.com/dieroll.htm" target="_top"><B>http://www.gregcons.com/dieroll.htm</B></A>.</P>
<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>
<blockquote><p><img src="note.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/note.gif">
<P>It might occur to you to try linking the MFC Library statically into your control. It seems easy enough to do: Choose Build Settings, and on the General tab there is a drop-down list box inviting you to choose static linking. If you do that and build,
you'll get hundreds of linker errors: the <font color="#008000">COleControl</font> and <font color="#008000">CPropPage</font> functions are not in the DLL that is linked statically.(That's because Microsoft felt it would be foolish to link the MFC
functions statically into a control.) Setting up another library to link in those functions is beyond the scope of this chapter, especially since all this work would lead to an enormous (over 1M) control that would take far too long to download the first
time.</P>
<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>
<H3><A ID="I4" NAME="I4"><B>Embedding an ActiveX Control into a Netscape Navigator Web Page</B></A></H3>
<P>NCompass Labs (<A HREF="tppmsgs/msgs0.htm#23" tppabs="http://www.ncompasslabs.com/" target="_top"><B>www.ncompasslabs.com</B></A>) has produced a Netscape plug-in, called ControlActive, that allows you to embed an ActiveX control into a page that will be read with Netscape
Navigator. Just look at Figure 20.3, which shows the Dieroll in Netscape. The HTML for this page is shown in Listing 20.2. (Resist the temptation to load this HTML into Netscape yourself until you have registered the control as safe for initializing and
scripting in the next section.)</P>
<A HREF="Vfigs03.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch20/Vfigs03.gif"><b>Fig. 20.3</b></A>
<P><I>ActiveX controls can be displayed in Netscape Navigator with the Ncompass Labs plug-in.</I></P>
<p><img src="cd_rom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/cd_rom.gif" hspace=10>
<P><I>Listing 20.2—fatdie2.html—Using </I><OBJECT><I> and </I><EMBED></P>
<pre><font color="#008000"><HTML></font></pre>
<pre><font color="#008000"><HEAD></font></pre>
<pre><font color="#008000"><TITLE>A Web page with a rolling die</TITLE></font></pre>
<pre><font color="#008000"></HEAD></font></pre>
<pre><font color="#008000"><BODY></font></pre>
<pre><font color="#008000"><OBJECT ID="Dieroll1"</font></pre>
<pre><font color="#008000">CLASSID="CLSID:46646B43-EA16-11CF-870C-00201801DDD6"</font></pre>
<pre><font color="#008000">CODEBASE="dieroll.cab#Version=1,0,0,1"</font></pre>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -