?? ch20.htm
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<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 += '<link rel="stylesheet" href="/includes/stylesheets/ebooks.css"></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">
<TITLE>Special Edition Using Visual C++ 6 -- Ch 20 -- Building an Internet ActiveX Control</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF">
<CENTER>
<H1><IMG SRC="../button/que.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM" BORDER="0"><BR>
Special Edition Using Visual C++ 6</H1>
</CENTER>
<CENTER>
<P><A HREF="../ch19/ch19.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"
ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch21/ch21.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>
<CENTER>
<H1>- 20 -</H1>
</CENTER>
<CENTER>
<H1>Building an Internet ActiveX Control</H1>
</CENTER>
<UL>
<LI><A HREF="#Heading1">Embedding an ActiveX Control in a Microsoft Internet Explorer
Web Page</A>
<LI><A HREF="#Heading2">Embedding an ActiveX Control in a Netscape Navigator Web
Page</A>
<LI><A HREF="#Heading3">Registering as Safe for Scripting and Initializing</A>
<LI><A HREF="#Heading4">Choosing Between ActiveX and Java Applets</A>
<LI><A HREF="#Heading5">Using AppWizard to Create Faster ActiveX Controls</A>
<LI><A HREF="#Heading6">Speeding Control Loads with Asynchronous Properties</A>
<UL>
<LI><A HREF="#Heading7">Properties</A>
<LI><A HREF="#Heading8">Using BLOBs</A>
<LI><A HREF="#Heading9">Changing Dieroll</A>
<LI><A HREF="#Heading10">Testing and Debugging Dieroll</A>
</UL>
</UL>
<P>
<HR SIZE="4">
<CENTER>
<H1></H1>
</CENTER>
<H2><A NAME="Heading1"></A>Embedding an ActiveX Control in a Microsoft Internet Explorer
Web Page</H2>
<P>In Chapter 17, "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. However, the ActiveX controls generated by older versions
of Visual C++ were too big and slow to put on a Web page. This chapter shows you
how to place these controls on your Web pages and how to write faster, sleeker controls
that will make your pages a pleasure to use.</P>
<P>It's a remarkably simple matter to put an ActiveX control on a Web page that you
know will be loaded by Microsoft Internet Explorer 3.0 or later. You use the <OBJECT>
tag, a relatively new addition to HTML that describes a wide variety of objects that
you might want to insert in 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 Chapter 17.</P>
<P>
<H4>Listing 20.1  fatdie.html--Using <OBJECT></H4>
<PRE><HEAD>
<TITLE>A Web page with a rolling die</TITLE>
</HEAD>
<BODY>
<OBJECT ID="Dieroll1"
CLASSID="CLSID:46646B43-EA16-11CF-870C-00201801DDD6"
CODEBASE="dieroll.cab#Version=1,0,0,1"
WIDTH="200"
HEIGHT="200">
<PARAM NAME="ForeColor" VALUE="0">
<PARAM NAME="BackColor" VALUE="16777215">
If you see this text, your browser does not support the OBJECT tag.
<BR>
</OBJECT>
<BR>
Here is some text after the die
</BODY>
</PRE>
<PRE></HTML>
</PRE>
<P>The only ugly thing here is the CLSID, and the easiest way to get that, because
you're a software developer, is to cut and paste it from dieroll.odl, the Object
Description Library. Open the dieroll project you built in Chapter 17 and use FileView
to open dieroll.odl quickly. Here's the section in dieroll.odl that includes the
CLSID:</P>
<P>
<PRE> // Class information for CDierollCtrl
[ uuid(46646B43-EA16-11CF-870C-00201801DDD6),
helpstring("Dieroll Control"), control ]
</PRE>
<P>This section is at the end of dieroll.odl--the earlier CLSIDs do not refer to
the whole control, only to portions of it. Copy the uuid from inside the brackets
into your HTML source.</P>
<BLOCKQUOTE>
<P>
<HR>
<STRONG>TIP:</STRONG> Microsoft has a product called the <I>Control</I> <I>Pad</I>
that gets CLSIDs from the Registry for you and makes life easier for Web page builders
who are either intimidated by instructions like "open the ODL file" or
don't have the ODL file because it's not shipped with the control. Because 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="http://www.microsoft.com/workshop/author/cpad/"><B>http://www.microsoft.com/workshop/author/cpad/</B></A>
for more details.
<HR>
</BLOCKQUOTE>
<P>The CODEBASE attribute of the OBJECT tag specifies where the OCX file is kept,
so if the user doesn't have a copy of the ActiveX control, one will be downloaded
automatically. The use of the CLSID means that if this user has already installed
this ActiveX control, there is no download time; the control is used immediately.
You can simply specify an URL to the OCX file, but to automate the DLL downloading,
this CODEBASE attribute points to a CAB file. Putting your control in a CAB file
will cut your download time by nearly half. You can learn more about CAB technology
at <A HREF="http://www.microsoft.com/intdev/cab/"><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>
<HR>
<STRONG>TIP:</STRONG> If you don't have access to a Web server in which to put controls
while you're developing them, use a <B>file://</B> URL in the CODEBASE attribute
that points to the control's location on your hard drive.
<HR>
</BLOCKQUOTE>
<P>The remaining OBJECT tag attributes will be intuitive if you've built a Web page
before: ID is used by other tags on the page to refer to this control; WIDTH and
HEIGHT specify the size, in pixels, of the control's appearance; and HSPACE and VSPACE
are horizontal and vertical blank spaces, in pixels, around the entire control.</P>
<P>Everything after the <OBJECT ...> tag and before the </OBJECT> tag
is ignored by browsers that understand the OBJECT tag. (The <OBJECT...> tag
is usually many lines long and contains all the information to describe the object.)
Browsers that don't understand the OBJECT tag ignore the <OBJECT ...> tag and
the </OBJECT> tag and display the HTML between them (in this case, a line of
text pointing out that this browser doesn't 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 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 OBJECT tag.
<P>
<LI>It can take a long time to download the control.
</UL>
<P><A HREF="javascript:popUp('20uvc01.gif')"><B>FIG. 20.1</B></A><B> </B><I>Microsoft
Internet Explorer can show ActiveX controls.</I></P>
<P>Figure 20.2 shows the same page displayed in Netscape Navigator 3.0. It doesn't
support the OBJECT tag, so it doesn't show the die. Also, Netscape Navigator is used
by more than half 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>
<P><A HREF="javascript:popUp('20uvc02.gif')"><B>FIG. 20.2</B></A><B> </B><I>Netscape
Navigator can't show ActiveX controls.</I></P>
<P>The size issue is a bigger worry. The release version of the Dieroll control,
as built for Chapter 17, is 26KB. Many designers put a 50KB 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 can then 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 doesn't automatically download and install this DLL, though
using a CAB file as discussed earlier can make it possible.</P>
<BLOCKQUOTE>
<P>
<HR>
<STRONG>TIP:</STRONG> For an example of a Web page that includes a CAB file for the
Dieroll control and the MFC DLLs, come to <A HREF="http://www.gregcons.com/dieroll.htm"><B>http://www.gregcons.com/dieroll.htm</B></A>.
<HR>
<BR>
<HR>
<STRONG>NOTE:</STRONG> It might occur to you to try linking the MFC Library statically
into your control. It seems easy enough to do: Choose Project, 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 COleControl and
CPropPage 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 in a control.)
Setting up another library to link in those functions is beyond the scope of this
chapter, especially because all this work would lead to an enormous (more than 1MB)
control that would take far too long to download the first time. 
<HR>
</BLOCKQUOTE>
<H2><A NAME="Heading2"></A>Embedding an ActiveX Control in a Netscape Navigator Web
Page</H2>
<P>NCompass Labs (<A HREF="http://www.ncompasslabs.com"><B>www.ncompasslabs.com</B></A>)
has produced a Netscape plug-in, called ScriptActive, that enables you to embed an
ActiveX control in a page to be read with Netscape Navigator. The HTML for the page
must be changed, as shown in Listing 20.2. (Resist the temptation to get the plug-in
and load this HTML into Netscape yourself until you have registered the control as
safe for initializing and scripting in the next section.)</P>
<BLOCKQUOTE>
<P>
<HR>
<STRONG>TIP:</STRONG> You can download a demonstration version of the plug-in for
a free 30-day trial from the NCompass Labs Web site.
<HR>
</BLOCKQUOTE>
<H4>Listing 20.2  fatdie2.html--Using <OBJECT> and <EMBED></H4>
<PRE><HTML>
<HEAD>
<TITLE>A Web page with a rolling die</TITLE>
</HEAD>
<BODY>
<OBJECT ID="Dieroll1"
CLASSID="CLSID:46646B43-EA16-11CF-870C-00201801DDD6"
CODEBASE="dieroll.cab#Version=1,0,0,1"
WIDTH="200"
HEIGHT="200">
<PARAM NAME="ForeColor" VALUE="0">
<PARAM NAME="BackColor" VALUE="16777215">
<PARAM NAME="Image" VALUE="beans.bmp">
<EMBED LIVECONNECT NAME="Dieroll1"
WIDTH="200"
HEIGHT="200"
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -