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

📄 ch08.htm

📁 用VC开发activeX控件的电子书,很不错的
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<!--last modified on Tue, Apr 15, 1997 10:22 AM-->
<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 8</TITLE>
</HEAD>

<BODY BACKGROUND="../images/r2harch.gif" TEXT="#000000" BGCOLOR="#FFFFFF">

<H1>Chapter 8<BR>
Using ATL to Create a Basic ActiveX Control</H1>

<UL>
	<LI><A HREF="#Heading1">Using ATL to Create a Basic ActiveX Control</A>
	<UL>
		<LI><A HREF="#Heading2">Creating the Basic Control Project</A>
		<LI><A HREF="#Heading3">Control Registration</A>
		<UL>
			<LI><A HREF="#Heading4">Listing 8.1 ATLCONTROLWIN.RGS--Sample Registry Script File
			for the CATLControlWin Control Class</A>
		</UL>
		<LI><A HREF="#Heading5">Creating Methods</A>
		<UL>
			<LI><A HREF="#Heading6">Listing 8.2 ATLCONTROLWIN.H--Alignment Enumeration Include
			File and Member Variables Added to Class Definition</A>
			<LI><A HREF="#Heading7">Listing 8.3 ALIGNMENTENUMS.H--Alignment Enumeration Include
			File</A>
			<LI><A HREF="#Heading8">Listing 8.4 ATLCONTROLWIN.H--Member Variable Initialization</A>
			<LI><A HREF="#Heading9">Listing 8.5 ATLCONTROLWIN.CPP--CaptionMethod Implementation</A>
		</UL>
		<LI><A HREF="#Heading10">Properties</A>
		<UL>
			<LI><A HREF="#Heading11">Creating Normal User Defined Properties</A>
			<LI><A HREF="#Heading12">Listing 8.6 ATLCONTROL.IDL--Dispid Enumeration Added to
			the IDL File to Aid in the Support of Properties in the Control</A>
			<LI><A HREF="#Heading13">Listing 8.7 ATLCONTROLWIN.CPP--Alignment Property Get/Put
			Method Implementation</A>
			<LI><A HREF="#Heading14">Creating Parameterized User Defined Properties</A>
			<LI><A HREF="#Heading15">Listing 8.8 ATLCONTROL.IDL--Update the IDL File to Support
			the Parameterized Property</A>
			<LI><A HREF="#Heading16">Listing 8.9 ATLCONTROLWIN.CPP--get_CaptionProp Implementation</A>
			<LI><A HREF="#Heading17">Listing 8.10 ATLCONTROLWIN.CPP--SetCaptionProp Implementation</A>
			<LI><A HREF="#Heading18">Creating Stock Properties</A>
			<LI><A HREF="#Heading19">Listing 8.11 ATLCONTROL.IDL--Add the Constant DISPID_BACKCOLOR
			to the IDL to Support the BackColor Stock Property</A>
			<LI><A HREF="#Heading20">Listing 8.12 ATLCONTROLWIN.H--m_BackColor Member Variable
			Added to the Class Declaration</A>
			<LI><A HREF="#Heading21">Listing 8.13 ATLCONTROLWIN.CPP--BackColor Property Source
			File Implementation</A>
			<LI><A HREF="#Heading22">Listing 8.14 ATLCONTROLWIN.H--Initialize the BackColor Property
			to an Initial Value</A>
			<LI><A HREF="#Heading23">Using Ambient Properties</A>
			<LI><A HREF="#Heading24">Creating Property Sheets</A>
			<LI><A HREF="#Heading25">Listing 8.15 ATLCONTROLWINPPG.H--Add the Necessary Include
			Files to the Property Page Header File</A>
			<LI><A HREF="#Heading26">Listing 8.16 ATLCONTROLWINPPG.H--Add the OnInitDialog Message
			Handler so the Property Page Can Be Initialized</A>
			<LI><A HREF="#Heading27">Listing 8.17 ATLCONTROLWINPPG.H--Modify the Apply Function
			to Update All of the Properties in the Control When the Property Page Exits</A>
			<LI><A HREF="#Heading28">Listing 8.18 ATLCONTROLWIN.H--Add the Property Page Reference
			to the </A><A HREF="#Heading29">Control Class Declaration to Complete the Property
			Page Implementation</A>
		</UL>
		<LI><A HREF="#Heading30">Adding Events</A>
		<UL>
			<LI><A HREF="#Heading31">Listing 8.19 ATLCONTROL.IDL--Add the Event Interface and
			the Change Method to the ATLControl.IDL File</A>
			<LI><A HREF="#Heading32">Listing 8.20 ATLCONTROLWIN.CPP--FireChange Helper Function
			Added to the Control</A>
			<LI><A HREF="#Heading33">Listing 8.21 ATLCONTROLWIN.CPP--FireChange Event Added to
			the CaptionMethod Implementation</A>
		</UL>
		<LI><A HREF="#Heading34">Persistence</A>
		<UL>
			<LI><A HREF="#Heading35">Listing 8.22 ATLCONTROLWIN.H--BackColor Property Added to
			the Property Map for Persistence</A>
		</UL>
		<LI><A HREF="#Heading36">Drawing the Control</A>
		<UL>
			<LI><A HREF="#Heading37">Standard Drawing</A>
			<LI><A HREF="#Heading38">Listing 8.23 ATLCONTROLWIN.H--Drawing Implementation Member
			Variables and Functions</A>
			<LI><A HREF="#Heading39">Listing 8.24 ATLCONTROLWIN.H--Initialize the New Member
			Variables in the Constructor</A>
			<LI><A HREF="#Heading40">Listing 8.25 ATLCONTROLWIN.CPP--Drawing Helper Functions</A>
			<LI><A HREF="#Heading41">Listing 8.26 ATLCONTROLWIN.CPP--Standard Drawing Added to
			the OnDraw Function</A>
		</UL>
		<LI><A HREF="#Heading42">From Here...</A>
	</UL>
</UL>

<P>
<HR SIZE="4">

<H1><A NAME="Heading1"></A>Using ATL to Create a Basic ActiveX Control</H1>

<UL>
	<LI><B>Registration</B> 
<SPACER TYPE="VERTICAL" SIZE="2">
 ATL's support of registration scripts makes registration support even easier to
	implement and use.
	<P>
	<LI><B>Adding methods</B> 
<SPACER TYPE="VERTICAL" SIZE="2">
 The ATL Object Wizards, while a little rudimentary, provide the necessary IDE integration
	that makes ATL appealing for rapid development.
	<P>
	<LI><B>Adding properties</B> 
<SPACER TYPE="VERTICAL" SIZE="2">
 The ATL Object Wizard makes adding properties a snap.
	<P>
	<LI><B>Adding events</B> 
<SPACER TYPE="VERTICAL" SIZE="2">
 Unfortunately, event support is not as easy to implement as that of MFC, but this
	chapter sheds some light on the subject that should make it as easy as is possible.
	<P>
	<LI><B>Persistence</B> 
<SPACER TYPE="VERTICAL" SIZE="2">
 ATL persistence support makes up for events in terms of ease of implementation.
	<P>
	<LI><B>Drawing the control</B> 
<SPACER TYPE="VERTICAL" SIZE="2">
 ATL deviates very little from the norm when it comes to drawing the control and
	requires no special knowledge to implement.
</UL>

<P>With the coming of Visual C++ 5.0, the ActiveX Template Library (ATL) has matured
to the level necessary for a complete ActiveX development framework. The two releases
of ATL, versions 1.0 and 1.1, that came before VC++ 5.0 introduced ATL to the growing
ActiveX development community as an alternative to MFC. However, the first two versions
of ATL allowed only for the creation of ActiveX COM Objects and ActiveX Automation
Servers (no small feats in their own right). ATL version 2.1, the version that ships
with VC++ 5.0, now supports the creation of ActiveX Controls.</P>
<P>Versions 1.0 and 1.1 included an AppWizard for creating a basic ATL project. Version
2.1 includes a number of AppWizards that can be used to create various ActiveX components,
thus furthering ATL's capability to compete with MFC as a rapid development tool.</P>
<P>In this chapter, you will create an ActiveX control with all the basics: methods,
properties, events, persistence, and drawing. Also, in this chapter and <A HREF="ch09.htm">Chapter
9</A>, you will explore some of the more advanced features and lesser known aspects
of control development, such as methods with optional parameters, asynchronous properties,
Clipboard support, and optimized drawing, to name a few.
<H2><A NAME="Heading2"></A>Creating the Basic Control Project</H2>
<P>To create an ATL ActiveX control, you want to take advantage of the AppWizard
provided by Visual C++. Run the Visual C++ development environment, and from the
<U>F</U>ile menu, select <U>N</U>ew. When the New dialog displays, select the Projects
tab (see fig 8.1). The Projects tab allows you the opportunity to define several
aspects of how the application will be created, for example, the type of application
to create, the name of the application, and the location where you want the project
created. For the type, select ATL COM AppWizard; enter the Project <U>N</U>ame ATLControl,
and the Lo<U>c</U>ation will be C:\que\ActiveX\ATLControl. Click the OK button to
start the ATL COM AppWizard so you can further define the properties of your control.
<B><BR>
<BR>
</B><A HREF="Art/08/gfigs01.jpg"><B>FIG. 8.1</B></A> <I><BR>
Define the new ATL control project with the New dialog.</I></P>
<P>The next step in the AppWizard is defining the basic architecture of your ATL
project (see fig. 8.2). Since you are creating an ActiveX Control, you choose the
<U>D</U>ynamic Link Library (DLL) radio button. <B><BR>
<BR>
</B><A HREF="Art/08/gfigs02.jpg"><B>FIG. 8.2</B></A> <I><BR>
Define the basic architecture of the ATL COM Object with the ATL COM AppWizard.An
OCX is in reality nothing more than a DLL. The extension OCX was carried over from
the early days of control development. You have the option of changing the extension
from DLL to OCX.</I></P>
<P>Since the sample implementation will be a control, it is not necessary to support
merging of the proxy/stub marshaling code, nor will the implementation require the
use of MFC. Click the <U>F</U>inish button to continue.</P>
<P>The New Project Information dialog is used to confirm the settings that were selected
for the project prior to the creation of the actual source files (see fig. 8.3).
This step is the last one in the ATL COM AppWizard. <B><BR>
<BR>
</B><A HREF="Art/08/gfigs03.jpg"><B>FIG. 8.3</B></A> <BR>
<I>Confirm the new project settings with the New Project Information dialog.</I></P>
<P>&quot;But wait,&quot; you say, &quot;I haven't defined any of my control properties.&quot;
The ATL COM AppWizard takes a slightly different approach from that of MFC. Only
the basic source files are created with the AppWizard. The remainder of the project
is defined by the Object Wizard, which allows much better control of the project
implementation versus MFC since the developer can add any number of ActiveX Controls,
Servers, or plain COM Objects after the basic project is created. After you confirm
your project settings, click the OK button to close the ATL COM AppWizard and create
the <TT>ATLControl</TT> project.</P>
<P>The next step is to add your control implementations to the project. From the
<U>I</U>nsert menu, select the New <U>A</U>TL Object menu item. Within the ATL Object
Wizard, select the Controls item in the left panel to display the types of control
components that can be added (see fig. 8.4). Your implementation will be a Full Control,
so select the Full Control icon. The other types of components that can be created
are a Microsoft Internet Explorer control that supports all of the necessary interfaces
to be hosted by the Internet Explorer Web browser and a Property Page component,
which you need if your control requires property page support. The Internet Explorer
control simply supports fewer interfaces than the Full Control. The Full Control
will also work within a Web browser. For more information, see the ATL documentation.
Click the <U>N</U>ext button to continue. <B><BR>
<BR>
</B><A HREF="Art/08/gfigs04.jpg"><B>FIG. 8.4</B></A> <BR>
<I>Select the type of ATL object to add to your project.</I></P>
<P>The next dialog is the ATL Object Wizard Properties dialog, which is used to define
the specific properties of the new object that will be added to your project. Select
the Names tab, and in the <U>S</U>hort Name edit field, type ATLControlWin (see fig.
8.5). The remainder of the edit fields will automatically update, reflecting the
short name that you added. The other fields can be changed, but in this case, you
use the default values. <B><BR>
<BR>
</B><A HREF="Art/08/gfigs05.jpg"><B>FIG. 8.5</B></A><BR>
<I>Define the name of the new control object.</I></P>
<P>Select the Attributes tab so that you can define the attributes of the control
project (see fig. 8.6). Check the Support ISupportErrorInfo and Support Connection
Points check boxes to add OLE rich error support and events to the control. See <A
HREF="ch04.htm">Chapter 4</A> and the ATL documentation for more information regarding
the options available to you. Leave the remainder of the settings at their default
values. <B><BR>
<BR>
</B><A HREF="Art/08/gfigs06.jpg"><B>FIG. 8.6</B></A> <I><BR>
Define the attributes of the new control object.</I></P>
<P>You use the Miscellaneous tab to define how the control will draw and act while
contained and whether your control implementation subclasses a built-in Windows control
(see fig. 8.7). For your implementation, you want the control to always create a
window whether or not the container is capable of supporting windowless controls,
so check the <U>W</U>indowed Only check box. Leave the remainder of the controls
at their default settings. <B><BR>
<BR>
</B><A HREF="Art/08/gfigs07.jpg"><B>FIG. 8.7</B></A> <BR>
<I>The Miscellaneous tab is used to define some of the basic control behaviors.</I></P>
<P>The Stock Properties tab is used to define any number of the basic stock properties
that the control project will support (see fig. 8.8). For now, leave the Stock Properties
tab as is, and click the OK button to create the new control object. <B><BR>
<BR>
</B><A HREF="Art/08/gfigs08.jpg"><B>FIG. 8.8</B></A> <I><BR>
The Stock Properties tab is used to define the stock properties that the control
object will be created with.</I></P>
<P>As with the MFC and later the BaseCtl implementation, you need to define two more
controls to complete the sample implementation. From the <U>I</U>nsert menu, select

⌨️ 快捷键说明

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