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

📄 ch15.htm

📁 用VC开发activeX控件的电子书,很不错的
💻 HTM
📖 第 1 页 / 共 4 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<!--last modified on Tue, Apr 15, 1997 1:52 PM-->
<HTML>

<HEAD>
<!-- This document was created from RTF source by rtftohtml version 3.0.1 -->

	<META NAME="GENERATOR" Content="Symantec Visual Page 1.0">
	<META NAME="Author" Content="Steph Mineart">
	<META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1">
	<TITLE>Chapter 15</TITLE>
</HEAD>

<BODY TEXT="#000000" BGCOLOR="#FFFFFF">

<H1>Chapter 15<BR>
Testing and Using Your Components</H1>

<UL>
	<LI><A HREF="#Heading1">Testing and Using Your Components</A>
	<UL>
		<LI><A HREF="#Heading3">ActiveX Containers and Controllers</A>
		<UL>
			<LI><A HREF="#Heading4">Using Visual Basic as a Container</A>
			<LI><A HREF="#Heading5">Using Microsoft Visual C++ as a Container</A>
			<LI><A HREF="#Heading6">HTML and Web Browsers</A>
			<LI><A HREF="#Heading7">Listing 15.1 SAMPLEIE.HTM--Using an ActiveX Control in HTML
			Code</A>
			<LI><A HREF="#Heading8">Listing 15.2 SAMPLENN.HTM--Converted HTML Document</A>
			<LI><A HREF="#Heading9">ActiveX Control Pad</A>
			<LI><A HREF="#Heading10">Using the Microsoft Access, Word, and Excel Applications
			as ActiveX Control Containers</A>
		</UL>
		<LI><A HREF="#Heading11">Tools for Testing Your Component</A>
		<UL>
			<LI><A HREF="#Heading12">Visual C++ ActiveX Control Test Container</A>
			<LI><A HREF="#Heading13">Users</A>
			<LI><A HREF="#Heading14">Automated Tools</A>
		</UL>
		<LI><A HREF="#Heading15">From Here...</A>
	</UL>
</UL>

<P>
<HR SIZE="4">

<H1><A NAME="Heading1"></A>Testing and Using Your Components</H1>

<UL>
	<LI><B>Using Visual Basic as a container</B> 
<SPACER TYPE="VERTICAL" SIZE="2">
 Microsoft Visual Basic provides a rapid development environment for creating containers.
	Check out how to use an ActiveX control in Visual Basic.
	<P>
	<LI><B>Using Visual C++ as a container</B> 
<SPACER TYPE="VERTICAL" SIZE="2">
 Microsoft Visual C++ is one of the more difficult tools to use as a container, but
	using the Foundation Classes makes this easier.
	<P>
	<LI><B>ActiveX controls in Web pages</B> 
<SPACER TYPE="VERTICAL" SIZE="2">
 Using ActiveX controls in HTML greatly enhances Web pages. Instead of just containing
	information, Web pages can now be pieces of a system. Check out how to use ActiveX
	controls in Web pages.
	<P>
	<LI><B>Using Microsoft Access, Microsoft Excel, and Microsoft Word as containers</B>
	
<SPACER TYPE="VERTICAL" SIZE="2">
 Microsoft Office 97 has greatly improved its ActiveX capabilities. Find out how
	to use ActiveX controls in Microsoft Access, Microsoft Excel, and Microsoft Word.
	<P>
	<LI><B>Visual C++ OLE Control Test Container</B> 
<SPACER TYPE="VERTICAL" SIZE="2">
 Microsoft Visual C++ provides the OLE Control Test Container to test your control
	without ever leaving Visual C++.
</UL>

<P>Once you have created your ActiveX component, the next step is to use and test
your component. You will not typically wait to try out your component until after
it is completely implemented. More than likely, the first thing you will want to
do is compile your code and try to use your component before you've added any significant
functionality. You need to be aware of the types of container and testing applications
that are available to you as a developer.</P>
<P>Even more critical than the actual testing of the component is testing it as soon
as possible. Even with all of the banners being raised about reuse and compatibility,
there are still issues to be aware of, which depend upon the tools you use. Some
containers or tools may not support the features that your component supports, and
even worse, in some cases, container implementations may differ, resulting in slightly
different behavior. What may work in VB may result in a crash in VC++. A word of
advice: Test your components often and with all of the tools that you plan to use
your components with.</P>
<P>This chapter will focus on the types and numbers of tools available that can host
ActiveX Controls, as well as the tools that you can use to test your controls. Even
though the primary focus of the chapter is on ActiveX Controls, almost all of the
applications mentioned can launch and use ActiveX Automation Servers and even ActiveX
COM objects. Because there are so many and they change so rapidly, it is beyond the
scope of this book to address all of the ActiveX capabilities of all the tools available
to you. You will need to research the specific tool to see to what extent it supports
ActiveX and COM.
<H2><A NAME="Heading3"></A>ActiveX Containers and Controllers</H2>
<P>Applications that use ActiveX controls are called <I>container applications, </I>or
just containers. In this chapter, you will discover how to use your control in Microsoft
Visual Basic, Microsoft Visual C++, Web browsers, Microsoft ActiveX Control Pad,
and the Microsoft Office tools: Access, Word, and Excel. Visual Basic is probably
the easiest tool to use, and Visual C++ is probably the hardest. This chapter will
explain how to insert an ActiveX control into these containers, how to change the
control's properties, and how to code the events contained in the control. <BR>
<BR>
<IMG SRC="bar.gif" WIDTH="480" HEIGHT="6" ALIGN="BOTTOM" BORDER="0"></P>


<BLOCKQUOTE>
	<P><B>NOTE:</B> If a container application is to be distributed, all controls added
	to a container must be distributed with the container.

</BLOCKQUOTE>

<P><IMG SRC="bar.gif" WIDTH="480" HEIGHT="6" ALIGN="BOTTOM" BORDER="0">
<H3><A NAME="Heading4"></A>Using Visual Basic as a Container</H3>
<P>To place a control in a Visual Basic application, you can open an existing project
or create a new one. To create a new project, select <U>F</U>ile, <U>N</U>ew Project
from the Visual Basic menu, select Standard EXE from the New Project dialog, and
click the OK button. To open an existing project, select <U>F</U>ile, <U>O</U>pen
Project from the Visual Basic menu. If you want to open a recent project, select
the Recent tab, select the desired project from the File list box, and click the
<U>O</U>pen button. If the project you are looking for is not listed on the Recent
tab, select the Existing tab, and then select your project from the proper directory.
After you have selected the project, click the <U>O</U>pen button to open the project.</P>
<P>Next you need to create a form or open an existing one. To create a form, select
<U>P</U>roject, Add <U>F</U>orm from the Visual Basic menu. If the Add form dialog
appears, click Form on the New tab, and then click the form in the project window.
To open an existing form, click the form in the project window, and then click the
View Form button.</P>
<P>Now you need to add the control to the project. Select <U>P</U>roject, C<U>o</U>mponents
from the main menu, which brings up the Components dialog (see fig 15.1). Make sure
that the <U>S</U>elected Items Only check box, on the lower-right side of the Components
dialog, is not selected. The <U>S</U>elected Items Only option will limit the display
to the items previously selected. Search for the name of the control you want to
add on the Controls tab, and click the check box next to the control. To see the
path of a particular control, click the control name, and the path will display in
the frame at the bottom of the dialog. <B><BR>
<BR>
</B><A HREF="Art/15/15fig01.jpg"><B>FIG. 15.1</B></A> <I><BR>
Select </I><CITE>P</CITE><I>roject, C</I><CITE>o</CITE><I>mponents to display the
Components dialog.</I></P>
<P>If the control is not registered, it will not appear on the Controls tab. To select
an unregistered control, click the <U>B</U>rowse button, select your control from
the proper directory, and click the Open button to register your control.</P>
<P>After the control is checked, click the <U>A</U>pply button to add the control
to the Toolbox, and then click the OK button to close the Components dialog. If the
control file added contains more than one control, you will see more than one tool
added to the Toolbox. If the <TT>MFCControl</TT> control was added, you will see
three new tools, labeled with a purple OCX. If each control had a different icon,
you would see three different icons on the Toolbox. If you drag the mouse over the
icons on the Toolbox, tool tips appear to tell you which control you are selecting.
To place the control on the form, double-click the new tool button or click the new
tool button, and then click and drag the form. If the toolbox is not displayed, select
<U>V</U>iew, Toolbo<U>x</U> from the Visual Basic menu.</P>
<P>To set the properties at design time, you need to use the Properties grid located
on the right side of the screen, which lists all the changeable properties and their
current values. If the Properties grid is not displayed, click the control on the
form, and press F4 to display the Properties grid. The left column contains the property
names, and the right contains the current property values. Click the Alphabetic tab
on the Properties grid to list the properties alphabetically, or select the Categorized
tab to view the properties by property category. To change a value, click the cell
containing the name of the property you want to change, and then click property value
cell in the same row and edit the value.</P>
<P>Setting the properties in code is almost as easy. To set a control in code use
following syntax:</P>
<P><FONT COLOR="#0066FF"><TT>ControlName.PropertyName = Value</TT></FONT></P>
<P><TT>ControlName</TT> is the name of the control, <TT>PropertyName</TT> is the
name of the property, and <TT>Value</TT> is the new property value or a variable
containing the value.</P>
<P>Retrieving a property value is the reverse of the preceding:</P>
<P><TT>Variable = ControlName.PropertyNameVariable</TT> in this case is used to hold
the value of the property.</P>
<P>To execute the methods of the control, use the following syntax:</P>
<P><FONT COLOR="#0066FF"><TT>Variable = ControlName.Method Parameter1,...ParameterN</TT></FONT></P>
<P><TT>Variable</TT> is the name of the variable that will hold the return value
of the method. If the method has no return value, <TT>Variable</TT> and the <TT>=</TT>
will be eliminated. <TT>ControlName</TT> is the name of the control, <TT>Method</TT>
is the name of the method, and <TT>Parameter1,...ParameterN</TT><I> </I>represents
any parameters to be passed to the method.</P>
<P>The final step in the design process is coding the events. To code an event, follow
these steps:

<OL>
	<LI>If the Project Explorer window is not visible, Select <U>V</U>iew, <U>P</U>roject
	Explorer. The Project Explorer window appears.
	<P>
	<LI>Select the form that contains the control, and then select <U>V</U>iew, <U>C</U>ode
	to display the Code window (see fig. 15.2).
	<P><A HREF="Art/15/15fig02.jpg"><B>FIG. 15.2</B></A><B> </B><I><BR>
	Select </I><CITE>V</CITE><I>iew, </I><CITE>C</CITE><I>ode from the Visual Basic menu
	to display the Code window.</I></P>
	<LI>Select the control you want to add the event code for from the Object combo box,
	which is the combo box on the left.
	<P>
	<LI>Select the event you want to code from the Procedure combo box, which is the
	combo box on the right.
	<P>
	<LI>Enter the code between the <TT>Private Sub</TT> and <TT>End Sub</TT> statements.
</OL>

<P>Repeat Steps 4 and 5 to code any of the other events contained in the control.</P>
<P>If you change your control, don't forget to recompile your Visual Basic executable
to prevent errors.</P>
<P>You can find more information on using ActiveX controls in Visual Basic in the
Visual Basic help files or the Knowledge Base articles on Microsoft's Web site (<A
HREF="http://www.microsoft.com/kb/"><B>http://www.microsoft.com/kb/</B></A><B>)</B>.
<H3><A NAME="Heading5"></A>Using Microsoft Visual C++ as a Container</H3>
<P>The easiest way to use your control in a Microsoft Visual C++ application is to
use the Microsoft Foundation Classes (MFC) with a dialog. When you first create an
application that will be using ActiveX controls, be sure to check the ActiveX Cont<U>r</U>ols
option in Step 3 of the MFC AppWizard (see fig. 15.3). If you do not select this
option, you will have to add a call to <TT>AfxEnableControlContainer</TT> in the
application's <TT>InitInstance</TT> member function (see fig. 15.4). <B><BR>
<BR>
</B><A HREF="Art/15/15fig03.jpg"><B>FIG. 15.3</B></A> <I><BR>
Here is where you check the ActiveX Cont</I><CITE>r</CITE><I>ols option.</I></P>
<P><A HREF="Art/15/15fig04.jpg"><B>FIG. 15.4</B></A> <I><BR>
You add a call to <TT>AfxEnableControl Container</TT> in the application's <TT>InitInstance</TT>
member function.</I></P>
<P>To add your control to a dialog, follow these steps:

<OL>
	<LI>Select the ResourceView tab of the Project Workspace window, click the &quot;+&quot;
	next to the project's resources folder, click the &quot;+&quot; next to the Dialog
	folder, and then double-click the ID for the dialog. These steps will display the
	dialog in the Dialog Editor window.
	<P>
	<LI>From the <U>P</U>roject menu, select Add To Com_ponents and Controls to display
	the Components and Controls Gallery dialog (see fig. 15.5).
	<P><A HREF="Art/15/15fig05.jpg"><B>FIG. 15.5</B></A> <I><BR>
	Select </I><CITE>P</CITE><I>roject, </I><CITE>A</CITE><I>dd To Project, Com</I><CITE>p</CITE><I>onents
	and Controls to display the Components and Controls Gallery dialog.</I></P>
	<LI>Double-click the Registered ActiveX Controls folder to display a list of ActiveX
	controls.
	<P>
	<LI>Scroll through the list of controls to find the icon for your control. Select
	the icon and click the In<U>s</U>ert button to add the control to your dialog, which
	will bring up a message box asking &quot;Insert this component?&quot; Click the OK
	button. This will bring up the Confirm Classes dialog.
	<P>
	<LI>Click the OK button to generate the wrapper class and to add the control to the
	Controls Toolbar; the Confirm Classes dialog will close automatically. The wrapper
	class serves as an interface between your application and the control.
	<P>
	<LI>Click the <U>C</U>lose button on the Components and Controls Gallery dialog.
	<P>
	<LI>Click and drag the control from the Controls Toolbar to the dialog to place the
	control on the dialog.
</OL>

<P>To set the properties of the control at design time, click the control on the
dialog, and select <U>V</U>iew, P<U>r</U>operties from the Developer Studio menu.
The Control Properties dialog will appear (see fig. 15.6). Click the Keep Visible
Pin to keep the dialog from closing. A property tab is available for General properties,

⌨️ 快捷键说明

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