📄 ch10.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<!--last modified on Tue, Apr 15, 1997 11:19 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 10</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF">
<H1>Chapter 10<BR>
Using BaseCtl to Create a Basic ActiveX Control</H1>
<UL>
<LI><A HREF="#Heading1">Using BaseCtl to Create a Basic ActiveX Control</A>
<UL>
<LI><A HREF="#Heading3">Creating a Basic Control Project</A>
<UL>
<LI><A HREF="#Heading4">Listing 10.1 BCFCONTROL.CPP--Include New Control Header Files
and Control Declarations</A>
<LI><A HREF="#Heading5">Listing 10.2 GUIDS.H--Combined Guids.h</A>
<LI><A HREF="#Heading6">Listing 10.3 BCFCONTROL.ODL--Combined Control ODL Files</A>
<LI><A HREF="#Heading7">Listing 10.4 RESOURCE.H--Combined Resource.h Files</A>
<LI><A HREF="#Heading8">Listing 10.5 LOCALOBJ.H--Combined LocalObj.h File</A>
<LI><A HREF="#Heading9">Listing 10.6 BCFCONTROLCTL.H--Update the Bitmap Source</A>
</UL>
<LI><A HREF="#Heading10">Control Registration</A>
<UL>
<LI><A HREF="#Heading11">Listing 10.7 BCFCONTROLCTL.H--DEFINE_CONTROLOBJECT Structure</A>
</UL>
<LI><A HREF="#Heading12">Creating Methods</A>
<UL>
<LI><A HREF="#Heading13">Listing 10.8 DISPIDS.H--Dispid for CaptionMethod</A>
<LI><A HREF="#Heading14">Listing 10.9 BCFCONTROLINTERFACES.H--Interface File Created
from the ODL File</A>
<LI><A HREF="#Heading15">Listing 10.10 BCFCONTROLCTL.H--Updated Control Class Header
File</A>
<LI><A HREF="#Heading16">Listing 10.11 ALIGNMENTENUMS.H--Alignment Styles Enumeration</A>
<LI><A HREF="#Heading17">Listing 10.12 BCFCONTROLCTL.CPP--Member Variable Initialization</A>
<LI><A HREF="#Heading18">Listing 10.13 BCFCONTROLCTL.CPP--CaptionMethod Implementation</A>
</UL>
<LI><A HREF="#Heading19">Creating Properties</A>
<UL>
<LI><A HREF="#Heading20">Creating Normal User Defined Properties</A>
<LI><A HREF="#Heading21">Listing 10.14 BCFCONTROL.ODL--Normal User Defined Property
ODL Declaration</A>
<LI><A HREF="#Heading22">Listing 10.15 BCFCONTROLCTL.H--Property Function Prototypes</A>
<LI><A HREF="#Heading23">Listing 10.16 BCFCONTROLCTL.CPP--Property Function Implementation</A>
<LI><A HREF="#Heading24">Creating Parameterized User Defined Properties</A>
<LI><A HREF="#Heading25">Listing 10.17 DISPIDS.H--Dispid for CaptionProp Property</A>
<LI><A HREF="#Heading26">Listing 10.18 BCFCONTROL.ODL--ODL Entry for Caption Property</A>
<LI><A HREF="#Heading27">Listing 10.19 BCFCONTROLCTL.H--CaptionProp Function Prototypes</A>
<LI><A HREF="#Heading28">Listing 10.20 BCFCONTROLCTL.CPP--CaptionProp Property Implementation</A>
<LI><A HREF="#Heading29">Creating Stock Properties</A>
<LI><A HREF="#Heading30">Listing 10.21 BCFCONTROL.ODL--BackColor Stock Property Support</A>
<LI><A HREF="#Heading31">Listing 10.22 BCFCONTROLCTL.H--BackColor Function Prototype</A>
<LI><A HREF="#Heading32">Listing 10.23 BCFCONTROLCTL.H--BackColor Member Variable</A>
<LI><A HREF="#Heading33">Listing 10.24 BCFCONTROLCTL.CPP--Member Initialization</A>
<LI><A HREF="#Heading34">Listing 10.25 BCFCONTROLCTL.CPP--BackColor Property Implementation</A>
<LI><A HREF="#Heading35">Using Ambient Properties</A>
<LI><A HREF="#Heading36">Creating Property Sheets</A>
<LI><A HREF="#Heading37">Listing 10.26 BCFCONTROLPPG.CPP--Property Page Implementation</A>
</UL>
<LI><A HREF="#Heading38">Adding Events</A>
<UL>
<LI><A HREF="#Heading39">Listing 10.27 DISPIDS.H--Change Event Dispid</A>
<LI><A HREF="#Heading40">Listing 10.28 BCFCONTROL.ODL--Event ODL Entry</A>
<LI><A HREF="#Heading41">Listing 10.29 BCFCONTROLCTL.H--Event Handling Structures--Header</A><A
HREF="#Heading42">File</A>
<LI><A HREF="#Heading43">Listing 10.30 BCFCONTROLCTL.CPP--Event Structures Implementation--Source
File</A>
<LI><A HREF="#Heading44">Listing 10.31 BCFCONTROLCTL.H--FireChange Event--Header
File</A>
<LI><A HREF="#Heading45">Listing 10.32 BCFCONTROLCTL.CPP--FireChange Event Implementation</A>
</UL>
<LI><A HREF="#Heading46">Persistence</A>
<UL>
<LI><A HREF="#Heading47">Text Persistence</A>
<LI><A HREF="#Heading48">Listing 10.33 BCFCONTROLCTL.CPP--LoadTextState Implementation</A>
<LI><A HREF="#Heading49">Listing 10.34 BCFCONTROLCTL.CPP--SaveTextState Implementation</A>
<LI><A HREF="#Heading50">Binary Persistence</A>
<LI><A HREF="#Heading51">Listing 10.35 BCFCONTROLCTL.CPP--SaveBinaryState Implementation</A>
<LI><A HREF="#Heading52">Listing 10.36 BCFCONTROLCTL.CPP--LoadBinaryState Implementation</A>
</UL>
<LI><A HREF="#Heading53">Drawing the Control</A>
<UL>
<LI><A HREF="#Heading54">Standard Drawing</A>
<LI><A HREF="#Heading55">Listing 10.37 BCFCONTROLCTL.H--Drawing Implementation Member
Variables and Functions</A>
<LI><A HREF="#Heading56">Listing 10.38 BCFCONTROLCTL.CPP--Member Initialization</A>
<LI><A HREF="#Heading57">Listing 10.39 BCFCONTROLCTL.CPP--Drawing Helper Functions</A>
<LI><A HREF="#Heading58">Listing 10.40 BCFCONTROLCTL.CPP--OnDraw Implementation</A>
</UL>
<LI><A HREF="#Heading59">From Here...</A>
</UL>
</UL>
<P>
<HR SIZE="4">
<H1><A NAME="Heading1"></A>Using BaseCtl to Create a Basic ActiveX Control</H1>
<UL>
<LI><B>Registration</B>
<SPACER TYPE="VERTICAL" SIZE="2">
The BaseCtl framework supports control registration and unregistra-tion through
a set of constants and structures that define the basic control framework.
<P>
<LI><B>Adding methods and properties</B>
<SPACER TYPE="VERTICAL" SIZE="2">
Like ATL, BaseCtl methods are first implemented in the type library and then in
the actual control. Like methods, properties are a way of exposing information about
a control implementation to the control's user.
<P>
<LI><B>Adding events</B>
<SPACER TYPE="VERTICAL" SIZE="2">
Implementing events in BaseCtl is far easier than in ATL and just as easy to maintain
as MFC.
<P>
<LI><B>Persistence</B>
<SPACER TYPE="VERTICAL" SIZE="2">
Persistence of data in BaseCtl is at its most basic and differs significantly from
that of both MFC and ATL.
<P>
<LI><B>Drawing the control</B>
<SPACER TYPE="VERTICAL" SIZE="2">
Drawing the User Interface can make or break a control implementation. Your implementation
relies on the same basic drawing routines as the MFC and ATL implementations and
requires almost no modification.
</UL>
<P>The BaseCtl framework was originally created by Marc Wandschneider, a member of
the Visual Basic (VB) team, to address the need for small, fast OCXs that could be
used within VB without adversely affecting VB's performance. The original implementation
was referred to lovingly as the MarcWan framework in honor of its primary author.</P>
<P>Along with the desire for a compact control framework that could be used to create
ActiveX controls came the mandate to remove the framework's dependence on MFC, which
in our mind is both the BaseCtl's strength and weakness. Removing the BaseCtl's dependence
on MFC solved several key problems: code overhead and control load time performance.
Since the BaseCtl framework is lean, the controls tend to execute faster, and because
the MFC DLLs are not required, the amount of time it takes to load the control into
memory is effectively reduced. However, writing a control from scratch using the
BaseCtl takes significantly more time since you do not have AppWizards and ClassWizards
at your disposal to speed up your implementation. The lack of general class support,
for example drawing classes and storage classes, can also increase your development
effort. With the coming of ActiveX, the need for small, fast OCXs suddenly became
an industry concern. Microsoft's answer to that demand was to publish the BaseCtl
as an alternative method (as opposed to MFC) for developing controls. Several versions
of the BaseCtl framework are floating around. The basic version, which ships with
the ActiveX SDK, consists of a number of source files and several samples. A more
thorough version consisting of more samples and even an AppWizard written in VB has
been available to the members of the Visual Basic 5 (VB 5) beta testing group.</P>
<P>The BaseCtl Framework is intended merely as a sample application and does not
have the same support and backing of Microsoft as do its other development products.
It was an immediate solution to an immediate problem. The BaseCtl framework has one
very distinct disadvantage in that Microsoft considers it a sample application only
and does not support it directly as a product. A growing number of developers on
the Internet are using BaseCtl, so you should be able to find support easily if you
run into a problem.</P>
<P>This chapter focuses on creating ActiveX controls using the BaseCtl sample that
ships with the ActiveX SDK. Be warned, though, that this kind of control development
is not for the timid. Be prepared to roll your sleeves up and get dirty.
<H2><A NAME="Heading3"></A>Creating a Basic Control Project</H2>
<P>The version of the BaseCtl sample that ships with the ActiveX SDK does not have
an AppWizard for creating a basic control like its MFC counterpart. Also, the documentation
is minimal and not much help. To speed up your development, we've included a sample
project called BCFBasicControl that is based on the original BaseCtl sample files.
Using this sample control, you can create a new project by copying the files and
changing the names of the files and classes.</P>
<P>First in Table 10.1 examine the files that are needed for creating a basic control
with the BaseCtl framework.
<TABLE BORDER="1" WIDTH="100%">
<CAPTION><B>Table 10.1</B><SPACER TYPE="HORIZONTAL" SIZE="10"><B> BaseCtl Basic Project Files</B></CAPTION>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><B>File name</B></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><B>Description</B></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP">BCFBasicControl.dsw</TD>
<TD ALIGN="LEFT" VALIGN="TOP">VC++ build file.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP">BCFBasicControl.dsp</TD>
<TD ALIGN="LEFT" VALIGN="TOP">VC++ build file.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP">BCFBasicControl.opt</TD>
<TD ALIGN="LEFT" VALIGN="TOP">VC++ build file.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP">BCFBasicControl.cpp</TD>
<TD ALIGN="LEFT" VALIGN="TOP">Main control application file.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP">BCFBasicControl.def</TD>
<TD ALIGN="LEFT" VALIGN="TOP">Application definition file.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP">BCFBasicControl.odl</TD>
<TD ALIGN="LEFT" VALIGN="TOP">Object Definition Language file for describing the and interfaces contained in the
control.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP">BCFBasicControl.rc</TD>
<TD ALIGN="LEFT" VALIGN="TOP">Application resource file.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP">BCFBasicControlCtl.bmp</TD>
<TD ALIGN="LEFT" VALIGN="TOP">Basic control bitmap that appears in the tool browser.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP">BCFBasicControlCtl.cpp</TD>
<TD ALIGN="LEFT" VALIGN="TOP">Primary control source file.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP">BCFBasicControlCtl.h</TD>
<TD ALIGN="LEFT" VALIGN="TOP">Primary control header file.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP">BCFBasicControlPPG.cpp</TD>
<TD ALIGN="LEFT" VALIGN="TOP">Property page source file.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP">BCFBasicControlPPG.h</TD>
<TD ALIGN="LEFT" VALIGN="TOP">Property page header file.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP">Dispids.h</TD>
<TD ALIGN="LEFT" VALIGN="TOP">Header file that contains all of the method, property, event dispids. Add all dispids
to this file.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP">Dwinvers.h</TD>
<TD ALIGN="LEFT" VALIGN="TOP">Version information header file. Change this file to the version information specific
to the control.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP">Guids.cpp</TD>
<TD ALIGN="LEFT" VALIGN="TOP">Source file for the GUIDs defined in the application.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP">Guids.h</TD>
<TD ALIGN="LEFT" VALIGN="TOP">Header file for the GUID defined in the application.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP">LocalObj.h</TD>
<TD ALIGN="LEFT" VALIGN="TOP">Header file for the OBJECT_TYPE constants that are used in the g_ObjectInfo table
to identify the controls and property pages contained in the application.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP">Resource.h</TD>
<TD ALIGN="LEFT" VALIGN="TOP">Header file that contains all of the resource ID constants.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP">project_nameInterfaces.H</TD>
<TD ALIGN="LEFT" VALIGN="TOP">This file is not created until the project is compiled. The mktylib (or midl) compiler
automatically generates this file from the ODL file. This file contains the C++style
declarations for the interfaces of the ActiveX component. Do not modify this file
directly; it will be recreated every time the ODL file is compiled.</TD>
</TR>
</TABLE>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -