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

📄 ch08.htm

📁 用VC开发activeX控件的电子书,很不错的
💻 HTM
📖 第 1 页 / 共 5 页
字号:
the New <U>A</U>TL Object menu item. Within the ATL Object Wizard, select the Controls
item in the left panel, and select the Full Control icon (refer to fig. 8.4). Click
the <U>N</U>ext button to continue.</P>
<P>On the Names tab within the ATL Object Wizard Properties dialog, add the <U>S</U>hort
Name ATLControlNoWin, and on the Attributes tab, check the Support <U>I</U>SupportErrorInfo
and Support Connection <U>P</U>oints check boxes to add OLE rich error support and
events to the control.</P>
<P>On the Miscellaneous tab, do not check the <U>W</U>indowed Only check box--so
that the control will create a window for itself only if the container cannot. Leave
the Stock Properties tab at its default settings. Click OK to add the control object
to the project.</P>
<P>For the last control implementation, you create a control that subclasses another
window's control. Again, from the <U>I</U>nsert menu, select the New <U>A</U>TL Object
menu item. Within the ATL Object Wizard (refer again to fig. 8.4), select the Controls
item in the left panel, and select the Full Control icon. Click the <U>N</U>ext button
to continue.</P>
<P>On the Names tab, within the ATL Object Wizard Properties dialog, add the <U>S</U>hort
Name ATLControlSubWin, and on the Attributes tab, check the Support <U>I</U>SupportErrorInfo
and Support Connection <U>P</U>oints check boxes to add OLE rich error support and
events to the control.</P>
<P>On the Miscellaneous tab, select the Button control from the <U>A</U>dd control
based on list box, and check the <U>W</U>indowed Only check box to ensure that a
window is always created for the control whether the container supports windowless
controls or not. Leave the Stock Properties tabs at its default settings. Click OK
to add the control object to the project.</P>
<P>At this point in the MFC sample (see <A HREF="ch06.htm">Chapter 6</A>), you are
also able to add other ActiveX features as part of the AppWizard implementation.
The ATL AppWizard and Object Wizard do not allow for defining any other ActiveX features
at this point, but they are defined in <A HREF="ch09.htm">Chapter 9</A>.</P>
<P>All of the basic source files and control objects are now added to the control
project. The next step in any control project is to ensure that the project contains
registration support. Without registration, the control cannot be used by any application.
<H2><A NAME="Heading3"></A>Control Registration</H2>
<P>Control registration and unregistration support is provided for you by ATL. You
are not required to make any code changes or additions to support it. Unlike MFC,
which uses a set of constants, ATL relies on resource information in the form of
a registry script file to define the information that is added to the registry database.
The registry script file is added automatically to the project when the control object
is added; one script file is added for each control object.</P>
<P>The registry script file or files are compiled into the control project as resources
and can be viewed in binary form in the resource editor. The files, which have the
extension .rgs, are normal text files that can be edited within the IDE. For more
information about the use of registry script files and their particular syntax, see
the VC++ books online subject &quot;Registry Scripting Examples--Active Template
Library, Articles.&quot; Listing 8.1 shows the registry script file for the <TT>CATLControlWin</TT>
control object that you added.
<H3><A NAME="Heading4"></A>Listing 8.1 ATLCONTROLWIN.RGS--Sample Registry Script
File for the CATLControlWin Control Class</H3>
<P><FONT COLOR="#0066FF"><TT><BR>
<BR>
HKCR <BR>
{ <BR>
ATLControlWin.ATLControlWin.1 = s `ATLControlWin Class' <BR>
{ <BR>
CLSID = s `{A19F6964-7884-11D0-BEF3-00400538977D}' <BR>
} <BR>
ATLControlWin.ATLControlWin = s `ATLControlWin Class' <BR>
{ <BR>
CurVer = s `ATLControlWin.ATLControlWin.1' <BR>
} <BR>
NoRemove CLSID <BR>
{ <BR>
ForceRemove {A19F6964-7884-11D0-BEF3-00400538977D} = s `ATLControlWin Class' <BR>
{ <BR>
ProgID = s `ATLControlWin.ATLControlWin.1' <BR>
VersionIndependentProgID = s `ATLControlWin.ATLControlWin' <BR>
ForceRemove `Programmable' <BR>
InprocServer32 = s `%MODULE%' <BR>
{ <BR>
val ThreadingModel = s `Apartment' <BR>
} <BR>
ForceRemove `Control' <BR>
ForceRemove `Programmable' <BR>
ForceRemove `Insertable' <BR>
ForceRemove `ToolboxBitmap32' = s `%MODULE%, 1' <BR>
`MiscStatus' = s `0' <BR>
{ <BR>
`1' = s `131473' <BR>
} <BR>
`TypeLib' = s `{A19F6957-7884-11D0-BEF3-00400538977D}' <BR>
`Version' = s `1.0' <BR>
} <BR>
} <BR>
} </TT></FONT></P>
<P>You can now compile and register the control you've created, but it won't be of
much use because it doesn't contain any methods, properties, or events.
<H2><A NAME="Heading5"></A>Creating Methods</H2>
<H2>Creating Methods</H2>
<P>Now that you have successfully created your basic ActiveX control project, you
can add a <I>method</I>, which is one of the basic aspects of component development.</P>
<P>For the purposes of the sample control, you are going to add a method called <TT>CaptionMethod</TT>.
The method will accept two parameters, the second being optional. The first parameter
is a string that the control will display within its client area, and the second,
optional parameter is the alignment of the caption within the client area, either
left, right, or center. Adding methods to an ATL control differs from MFC in that
MFC relies on the familiar ClassWizard, and ATL does not. From the ClassView tab
in the Project Workspace window, select the <TT>IATLControlWin</TT> interface, click
the right mouse button, and select the Add <U>M</U>ethod menu item (see fig. 8.9).</P>
<P>In the Add Method to Interface dialog, add the Method <U>N</U>ame, <TT>CaptionMethod,</TT>
and the <TT>P</TT>arameters, <TT>[in] BSTR bstrCaption, [in, optional] VARIANT varAlignment,
[out, retval] long * lRetVal</TT> (see fig. 8.10). The <U>A</U>ttributes button displays
a dialog for adding Interface Definition Language (IDL) attributes for the entire
function declaration. <B><BR>
<BR>
</B><A HREF="Art/08/gfigs09.jpg"><B>FIG. 8.9</B></A> <I><BR>
Add a new method to the control project.</I></P>
<P><A HREF="Art/08/gfigs10.jpg"><B>FIG. 8.10</B></A> <I><BR>
Define the <TT>CaptionMethod</TT> method.</I></P>
<P><IMG SRC="bar.gif" WIDTH="480" HEIGHT="6" ALIGN="BOTTOM" BORDER="0"></P>


<BLOCKQUOTE>
	<P><B>NOTE:</B> All optional parameters must be of type <TT>VARIANT</TT>, and they
	must fall at the end of the parameter list. Optional parameters are not managed in
	any way by OLE. It is the Server application's responsibility to determine whether
	the <TT>VARIANT</TT> parameter passed to the method contains data and whether to

	<UL>
	<LI>either use the data passed to the method or convert the data to a useful type,
	if possible, or <BR>
	<BR>
	
	<LI>ignore the parameter if invalid data was passed and use the default value if
	appropri- ate, or <BR>
	<BR>
	
	<LI>inform the user of an error condition if one of the above conditions was not
	met.
	</UL>


</BLOCKQUOTE>

<P><IMG SRC="bar.gif" WIDTH="480" HEIGHT="6" ALIGN="BOTTOM" BORDER="0"><BR>
<BR>
You've also added the direction that the parameters flow in the form of <TT>[in]</TT>
and <TT>[out]</TT> parameter attributes. See Table 8.1 for a complete description
of the possible attributes that can be used.</P>
<P>Parameter attributes are used to aid development tools in determining how parameters
are used within a function call. A tool like Visual Basic will hide the details of
how parameters are handled--such as creating and destroying memory--based on these
and other attributes in the type library. This is why the type library is so important
to ActiveX component development.</P>
<P>Note that the IDL parameter attributes are added directly to the parameter list.
Click OK to add the method to the control. 
<TABLE BORDER="1" WIDTH="100%">
	<CAPTION><B>Table 8.1</B><SPACER TYPE="HORIZONTAL" SIZE="10"><B> Parameter Flow Attributes</B></CAPTION>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><B>Direction</B></TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><B>Description</B></TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>in</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP">Parameter is passed from caller to callee.</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>out</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP">Parameter is returned from callee to caller.</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>in, out</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP">Parameter is passed from caller to callee, and the callee returns a parameter.</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>out, retval</TT></TD>
		<TD ALIGN="LEFT" VALIGN="TOP">Parameter is the return value of the method and is returned from the callee to the
			caller.</TD>
	</TR>
</TABLE>
<BR>
<BR>
To aid your <TT>CaptionMethod</TT> implementation, you need to add an enumeration
for all the valid alignment settings and two member variables to your class definition
(see Listing 8.2). The enumeration is included in the header file Alignmentenums.h
(see Listing 8.3). The two member variables, <TT>m_lptstrCaption</TT> and <TT>m_lAlignment</TT>,
are used to store the caption string and the alignment setting while the control
is being used.</P>
<P>Note the data type used for the <TT>m_lAlignment</TT> member variable. The variable
is declared as type <TT>long</TT> and not as the enumeration type because of the
data type restrictions imposed upon you by ActiveX Automation. Remember that only
data types that can be passed in a <TT>VARIANT</TT> can be used in methods and properties.
By declaring the <TT>m_lAlignment</TT> member as <TT>long</TT>, you do not have to
explicitly convert the value by casting to the enumerated type when it is retrieved
from the <TT>VARIANT</TT> parameter in the <TT>caption</TT> method. On the other
hand, casting the value to the enumerated type is a trivial issue, and its implementation
is based completely on your preference.
<H3><A NAME="Heading6"></A>Listing 8.2 ATLCONTROLWIN.H--Alignment Enumeration Include
File and Member Variables Added to Class Definition</H3>
<P><FONT COLOR="#0066FF"><TT><BR>
<BR>
// ATLControlWin.h : Declaration of the CATLControlWin <BR>
#ifndef __ATLCONTROLWIN_H_ <BR>
#define __ATLCONTROLWIN_H_ <BR>
#include &quot;resource.h&quot; // main symbols <BR>
#include &quot;alignmentenums.h&quot; <BR>
///////////////////////////////////////////////////////////////////////////// <BR>
// CATLControlWin <BR>
class ATL_NO_VTABLE CATLControlWin : <BR>
. . . <BR>
protected: <BR>
// storage variable for the caption <BR>
LPTSTR m_lptstrCaption; <BR>
// storage variable for the alignment <BR>
long m_lAlignment; <BR>
}; </TT></FONT></P>
<P>The enumeration is added as an include file. By adding the enumeration to an include
file, you are able to use the enumeration in other files simply by including the
file reference, which will be necessary as you proceed through the chapter (see Listing
8.3).
<H3><A NAME="Heading7"></A>Listing 8.3 ALIGNMENTENUMS.H--Alignment Enumeration Include
File</H3>
<P><FONT COLOR="#0066FF"><TT><BR>
<BR>
#if !defined _ALIGNMENTENUMS_H <BR>
#define _ALIGNMENTENUMS_H <BR>
// caption alignment enumeration <BR>
typedef enum tagAlignmentEnum <BR>
{ <BR>
EALIGN_LEFT = 0, <BR>
EALIGN_CENTER = 1, <BR>
EALIGN_RIGHT = 2, <BR>
}EALIGNMENT; <BR>
#define EALIGN_LEFT_TEXT &quot;Left&quot; <BR>
#define EALIGN_CENTER_TEXT &quot;Center&quot; <BR>
#define EALIGN_RIGHT_TEXT &quot;Right&quot; <BR>
#endif // #if !defined _ALIGNMENTENUMS_H </TT></FONT></P>
<P>You initialize your member variables in the constructor of your control (see Listing
8.4).
<H3><A NAME="Heading8"></A>Listing 8.4 ATLCONTROLWIN.H--Member Variable Initialization</H3>
<P><FONT COLOR="#0066FF"><TT><BR>
<BR>
. . . <BR>
public IConnectionPointContainerImpl&lt;CATLControlWin&gt;, <BR>
public ISpecifyPropertyPagesImpl&lt;CATLControlWin&gt; <BR>
{ <BR>
public: <BR>
CATLControlWin() <BR>

⌨️ 快捷键说明

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