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

📄 ch15.htm

📁 用VC开发activeX控件的电子书,很不错的
💻 HTM
📖 第 1 页 / 共 4 页
字号:
All properties, and any property pages you defined in your control. General properties
are the base set of properties assigned to all controls on the dialog. All properties
are stock properties you defined in the control. To change a property value, click
the tab containing the name of the property you want to change, and then set the
value for the property. <B><BR>
<BR>
</B><A HREF="Art/15/15fig06.jpg"><B>FIG. 15.6</B></A> <I><BR>
Use the Control Properties dialog to set the properties for the control.</I></P>
<P>The easiest way to access the properties and methods of the control in code is
to create member variables that coincide with the control. To create the member variables,
follow these steps:

<OL>
	<LI>Select <U>V</U>iew, Class<U>W</U>izard to display the MFC ClassWizard dialog.
	<P>
	<LI>Select the Member Variables tab.
	<P>
	<LI>Make sure the Dialog class appears in the Class <U>n</U>ame text box.
	<P>
	<LI>In the Control <U>I</U>Ds list box, select the ID for the control.
	<P>
	<LI>Click the <U>A</U>dd Variable button to add the member variable. The Add Member
	Variable dialog appears.
	<P>
	<LI>In the Member variable <U>n</U>ame text box, enter a name for the member variable.
	A good naming standard is to enter the control name prefaced with <TT>m_</TT><I>.</I>
	<P>
	<LI>Make sure the <U>C</U>ategory combo box still contains &quot;<TT>Control</TT>,&quot;
	and click the OK button. The other choices are for the control's exposed properties.
	<P>
	<LI>Click the OK button on the MFC ClassWizard dialog to accept the member variable.
	ClassWizard defines the member variable in the dialog class and adds a Dialog Data
	Exchange (<TT>DDX_Control</TT>) call to the dialog's implementation of the <TT>DoDataExchange</TT>
	function.
</OL>

<P>Now that the member variable is created, setting and retrieving properties and
executing methods is fairly simple. Manipulating the properties requires using the
functions in the wrapper class designed to do so. These functions are added to the
wrapper class by ClassWizard. In the <TT>MFCControl</TT> control, the functions to
use for setting properties include <TT>SetBackColor</TT>, <TT>SetAlignment</TT>,
and <TT>SetCaptionProp</TT>. The functions for retrieving properties include <TT>GetReadyState</TT>,
<TT>GetBackColor</TT>, <TT>GetAlignment</TT>, and <TT>GetCaptionProp</TT>. To use
these prop- erty functions, you use the following syntax:</P>
<P><FONT COLOR="#0066FF"><TT>Variable = WrapperClassVariable.Function (Parameter1,...ParameterN).</TT></FONT></P>
<P><TT>Variable</TT><I> </I>is used to hold a return value from the function; this
will not be used if there is no return. <TT>WrapperClassVariable</TT><I> </I>is the
wrapper class variable ClassWizard defined in the dialog header file. <TT>Function</TT><I>
</I>is the name of the function being used to retrieve or set the property, and <TT>Parameter1,...ParameterN</TT><I>
</I>represents parameters that need to be passed to the function.</P>
<P>Executing a method is basically the same as calling a property function. To execute
a method, use the following syntax:</P>
<P><FONT COLOR="#0066FF"><TT>Variable = WrapperClassVariable.MethodName(Parameter1,...ParameterN)</TT></FONT></P>
<P><TT>Variable</TT> is used to hold a return value if the method returns a value.
Again, <TT>WrapperClassVariable</TT><I> </I>is the wrapper class variable ClassWizard
defined in the dialog header file. <TT>MethodName</TT> is the name of the method
you want to execute, and <TT>Parameter1,...ParameterN</TT><I> </I>represent parameters
that need to be passed to the method.</P>
<P>Okay, now for coding the events. If you have ever created event handlers for a
button, radio button, or any other standard control in a dialog, you already know
how to set up event handling for your custom control; the setup is the same. You
use the Message Maps tab of the ClassWizard to create an <I>event sink map, </I>which
is basically an outline, or map, of event handlers maintained by ClassWizard. When
your control fires an event, the event handler that is mapped to that event is executed.</P>
<P>To create an event handler for an event, use the following steps:

<OL>
	<LI>Select <U>V</U>iew, Class<U>W</U>izard to open the MFC ClassWizard dialog.
	<P>
	<LI>Select the Message Maps tab if it is not the current tab.
	<P>
	<LI>Make sure the Dialog class appears in the Class <U>n</U>ame text box.
	<P>
	<LI>Select your control from the Object <U>I</U>Ds list box. The Messa_ges list box
	now shows the events for the control (see fig. 15.7).
	<P><A HREF="Art/15/15fig07.jpg"><B>FIG. 15.7</B></A> <BR>
	<I>Messages list box shows the events for the control selected in the Object </I><CITE>I</CITE><I>Ds
	list box.</I></P>
	<LI>In the Messa_ges list box, select the event you want to add code for.
	<P>
	<LI>Click the <U>A</U>dd Function button to bring up the Add Member Function dialog.
	The Member function <U>n</U>ame text box contains a suggested name for the handler
	function. You can change the name if you like. Click the OK button when finished.
	If you look at the Member <U>f</U>unctions list box, you will see the new event handler
	function listed. The uppercase &quot;E&quot; in the gray box signifies that this
	is an event handler.
	<P>
	<LI>To add code to the handler, click the <U>E</U>dit Code button, which will dump
	you into the event handler function for that event. Add your code for the event here.
</OL>

<P>More information about adding controls to Visual C++ applications can be found
in the Visual C++ help files and Microsoft's Knowledge Base (<A HREF="http://www.microsoft.com/kb"><B>http://www.microsoft.com/kb</B></A>).
<H3><A NAME="Heading6"></A>HTML and Web Browsers</H3>
<P>Using your control in an HTML (Hypertext Markup Language) page consists of basically
three actions: placing the control in the HTML, downloading the control to the user,
and installing the control on the user's machine.</P>
<P>If all your Internet users are using the Microsoft Internet Explorer, the task
of inserting an ActiveX control in an HTML document is fairly simple. If your users
are using Netscape Navigator, or both, inserting a control requires a little more
work.</P>
<P>The Microsoft Internet Explorer requires the use of the <TT>&lt;OBJECT&gt;</TT>
HTML tag, which Netscape has not yet adopted. This tag is composed of several attributes,
the most important being <TT>ID</TT>, <TT>CLASSID</TT>, <TT>CODEBASE</TT>, <TT>PARAM
NAME</TT>, and <TT>VALUE</TT>. Listing 15.1 shows the HTML code that might be used
with the <TT>MFCControl</TT> control.
<H3><A NAME="Heading7"></A>Listing 15.1 SAMPLEIE.HTM--Using an ActiveX Control in
HTML Code</H3>
<P><FONT COLOR="#0066FF"><TT>&lt;HTML&gt;<BR>
&lt;HEAD&gt;<BR>
&lt;TITLE&gt;Sample Page&lt;/TITLE&gt;<BR>
&lt;/HEAD&gt;<BR>
&lt;BODY&gt; <BR>
&lt;OBJECT <BR>
ID=&quot;MFCControlWin1&quot; <BR>
WIDTH=100 <BR>
HEIGHT=51<BR>
CLASSID=&quot;CLSID:A1198546-2E75-11D0-BD82-000000000000&quot;<BR>
CODEBASE=&quot;http://www.somesite.com/somedirectory/ MFCControl.ocx&quot;&gt;#Version=1,0,0,1&quot;&gt;<BR>
&lt;PARAM NAME=&quot;Alignment&quot; VALUE=&quot;1&quot;&gt;<BR>
&lt;PARAM NAME=&quot;CaptionProp&quot; VALUE=&quot;Sample&quot;&gt;<BR>
&lt;/OBJECT&gt; <BR>
&lt;/BODY&gt; <BR>
&lt;/HTML&gt;<BR>
</TT></FONT></P>

<P>The <TT>ID</TT> attribute gives the control a name, providing a way for the HTML
code to access the control.</P>

<P>The <TT>CLASSID</TT> attribute, which is the unique UUID assigned to the control,
tells Microsoft Internet Explorer which object to load. The UUID for your control
can be found in the ODL (Object Description Library) file for your ActiveX control.
Be sure to locate the UUID for the specific control you are using by looking for
the class information comment for the control. For example, to locate the UUID for
the <TT>MFCControlWin</TT> control used above, you would look for the following:</P>

<P><FONT COLOR="#0066FF"><TT>// Class information for CMFCControlWinCtrl<BR>
[ uuid(A1198546-2E75-11D0-BD82-000000000000),<BR>
helpstring(&quot;MFCControlWin Control&quot;), control ]</TT></FONT></P>

<P>Open the ODL file with Notepad or something similar, copy the UUID from there,
and paste it in the HTML document. <BR>
<BR>
<IMG SRC="bar.gif" WIDTH="480" HEIGHT="6" ALIGN="BOTTOM" BORDER="0"></P>


<BLOCKQUOTE>
	<P><B>NOTE:</B> Even if you have only one control in your OCX file, this file contains
	many UUIDs. Be sure to locate the proper UUID using the method above. Also, your
	UUID will not be the same as the one in the example.

</BLOCKQUOTE>

<P><IMG SRC="bar.gif" WIDTH="480" HEIGHT="6" ALIGN="BOTTOM" BORDER="0"><BR>
<BR>
If the current version is not on the user's machine, the <TT>CODEBASE</TT> attribute
tells Microsoft Internet Explorer where to find the control to download. Once the
current version is loaded on a user's machine, the HTML document uses the control
from the user's machine, allowing the document to load faster. Keep in mind that
if your control was built with the Microsoft Foundation Classes, the MFC DLLs have
to be loaded on the user's machine. This adds to the size and complexity of the download.
The bigger the download, the longer the user has to wait to load the control. If
your control is built using the BaseCtl, the <TT>CODEBASE</TT> statement refers to
the control only. If you created your control with MFC, you need to use a new technology
from Microsoft called Cabinet files (CAB files for short). These files allow you
to compress a group of files into one file (the CAB file), download the file to the
user's PC, and install the files on the user's PC. The CAB file contains an INF file,
which controls the installation. Check out Microsoft's Web site (<A HREF="http://www.microsoft.com/"><B>http://www.microsoft.com/</B></A>)
and the ActiveX SDK documentation for more information on CAB files. <BR>
<BR>
<IMG SRC="bar.gif" WIDTH="480" HEIGHT="6" ALIGN="BOTTOM" BORDER="0"></P>


<BLOCKQUOTE>
	<P><B>NOTE:</B> Even if you created your control without the Microsoft Foundation
	Classes, you can still use a CAB file to compress your control and save transfer
	time.

</BLOCKQUOTE>

<P><IMG SRC="bar.gif" WIDTH="480" HEIGHT="6" ALIGN="BOTTOM" BORDER="0"><BR>
<BR>
The <TT>&lt;PARAM&gt;</TT> tag is used inside the <TT>&lt;OBJECT&gt;</TT> tag to
set the initial property values of the control. The <TT>&lt;PARAM&gt;</TT> tag has
two attributes: <TT>NAME</TT> and <TT>VALUE</TT>. The <TT>NAME</TT> is the name of
the property, and the <TT>VALUE</TT> is the property value.</P>
<P>Netscape Navigator users will need to purchase a plug-in from NCompass Labs called
ScriptActive. This plug-in allows Navigator to run HTML documents that contain ActiveX
controls. The information to be used by this add-in is contained in an <TT>&lt;EMBED&gt;</TT>
tag because Navigator does not recognize the <TT>&lt;OBJECT&gt;</TT> tag. If your
Web page will be read using Netscape Navigator and Microsoft Internet Explorer, which
is most likely the case, nest the <TT>&lt;EMBED&gt;</TT> tag within the <TT>&lt;OBJECT&gt;</TT>
tag. Microsoft Internet Explorer will fail if the <TT>&lt;EMBED&gt;</TT> tag is outside
the <TT>&lt;OBJECT&gt;</TT> tag. ScriptActive comes with the HTML Conversion Utility,
which creates a Netscape HTML file from a Microsoft Internet Explorer HTML file,
making the developer's life easier. Create your page with ActiveX Control Pad and
use the utility to automatically create a page compatible to both browsers. The HTML
document (see Listing 15.2) is an example of a document converted by the HTML Conversion
Utility. Visit the NCompass Labs Web site (<A HREF="http://www.ncompasslabs.com/"><B>http://www.ncompasslabs.com/</B></A><B>
</B>)for more information.
<H3><A NAME="Heading8"></A>Listing 15.2 SAMPLENN.HTM--Converted HTML Document</H3>
<P><FONT COLOR="#0066FF"><TT>&lt;HTML&gt;<BR>
&lt;HEAD&gt;<BR>
&lt;TITLE&gt;Sample Page&lt;/TITLE&gt;<BR>
&lt;/HEAD&gt;<BR>
&lt;BODY&gt; <BR>
&lt;OBJECT <BR>
ID=&quot;MFCControlWin1&quot; <BR>
WIDTH=100 <BR>
HEIGHT=51<BR>
CLASSID=&quot;CLSID:A1198546-2E75-11D0-BD82-000000000000&quot;<BR>
CODEBASE=&quot;http://www.somesite.com/somedirectory/ <BR>
MFCControl.ocx#Version=1,0,0,1&quot;&gt;<BR>
&lt;PARAM NAME=&quot;Alignment&quot; VALUE=&quot;1&quot;&gt;<BR>
&lt;PARAM NAME=&quot;CaptionProp&quot; VALUE=&quot;Sample&quot;&gt;<BR>
&lt;EMBED <BR>
NAME=&quot;MFCControlWin1&quot; <BR>
WIDTH=100 <BR>
HEIGHT=51<BR>
CLASSID=&quot;CLSID:A1198546-2E75-11D0-BD82-000000000000&quot;<BR>
CODEBASE=&quot;http://www.somesite.com/somedirectory/MFCControl.ocx#Version=1,0,0,1&quot;<BR>
TYPE=&quot;application/oleobject&quot; <BR>
PARAM_Alignment=&quot;1&quot;<BR>
PARAM_CaptionProp=&quot;Sample&quot; <BR>
&gt;&lt;/OBJECT&gt; <BR>
&lt;/BODY&gt; <BR>
&lt;/HTML&gt;<BR>
</TT></FONT></P>

<P>Another powerful attribute is <TT>SCRIPT</TT>. This attribute allows a developer
to add code directly to an HTML document. Currently, two scripting languages are
available: JavaScript and Visual Basic Script (often referred to as VBScript), which
imitate Java and Visual Basic, respectively. Check out Netscape's Web site (<A HREF="http://home.netscape.com/"><B>http://home.netscape.com/</B></A>)
for JavaScript syntax and information and Microsoft's Web site (<A HREF="http://www.microsoft.com/"><B>http://www.microsoft.com/</B></A>)
for VBScript syntax and information. <A HREF="ch16.htm">Chapter 16</A> explains scripting
in a little more detail. <BR>
<BR>
<IMG SRC="bar.gif" WIDTH="480" HEIGHT="6" ALIGN="BOTTOM" BORDER="0"></P>


<BLOCKQUOTE>
	<P><B>NOTE:</B> Unless you have certified your control, make sure that the Safety
	Level is not set to High in Microsoft Internet Explorer. If it is set to High and
	your control is not certified, it will not run your scripts. To check or change this
	setting, select <U>V</U>iew, <U>O</U>ptions from the Explorer menu. Select the Security
	tab, and then click the Safety Level button to display the Safety Level dialog. Most
	users, especially developers, choose Medium because it gives you the option to load
	or not load something. Certification and other security issues are covered in <A
	HREF="ch16.htm">Chapter 16</A>.

</BLOCKQUOTE>

<P><IMG SRC="bar.gif" WIDTH="480" HEIGHT="6" ALIGN="BOTTOM" BORDER="0">
<H3><A NAME="Heading9"></A>ActiveX Control Pad</H3>
<P>The number of Web Authoring tools is growing rapidly; very little manual coding
of HTML is done anymore. The ActiveX SDK includes a handy little tool called the
ActiveX Control Pad, which aids in managing ActiveX controls in Web pages. It allows

⌨️ 快捷键说明

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