📄 ch06.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<!--last modified on Tue, Apr 15, 1997 10:07 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 6</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF">
<H1>Chapter 6<BR>
Using MFC to Create a Basic ActiveX Control</H1>
<UL>
<LI><A HREF="#Heading1">Using MFC 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 6.1 MFCCONTROLWINCTL.CPP--Default UpdateRegistry
Implementation</A>
</UL>
<LI><A HREF="#Heading5">Creating Methods</A>
<UL>
<LI><A HREF="#Heading6">Listing 6.2 MFCCONTROLWINCTL.H--Alignment Enumeration Include
File and Member Variables Added to Class Definition</A>
<LI><A HREF="#Heading7">Listing 6.3 ALIGNMENTENUMS.H--Alignment Enumeration Include
File</A>
<LI><A HREF="#Heading8">Listing 6.4 MFCCONTROLWINCTL.CPP--Initialize the m_lAlignment
Member Variable in the Class Constructor</A>
<LI><A HREF="#Heading9">Listing 6.5 MFCCONTROLWINCTL.CPP--CaptionMethod Implementation</A>
<LI><A HREF="#Heading10">Listing 6.6 MFCCONTROL.ODL--Keyword [optional] Added to
the CaptionMethod ODL Definition</A>
</UL>
<LI><A HREF="#Heading11">Properties</A>
<UL>
<LI><A HREF="#Heading12">Creating Normal User Defined Properties</A>
<LI><A HREF="#Heading13">Listing 6.7 MFCCONTROLWINCTL.CPP--Alignment Property Get/Set
Method Implementation</A>
<LI><A HREF="#Heading14">Creating Parameterized User Defined Properties</A>
<LI><A HREF="#Heading15">Listing 6.8 MFCCONTROLWINCTL.CPP--GetCaptionProp Implementation</A>
<LI><A HREF="#Heading16">Listing 6.9 MFCCONTROLWINCTL.CPP--SetCaptionProp Implementation</A>
<LI><A HREF="#Heading17">Listing 6.10 MFCCONTROL.ODL--[optional] Keyword Added to
the ODL File</A>
<LI><A HREF="#Heading18">Creating Stock Properties</A>
<LI><A HREF="#Heading19">Using Ambient Properties</A>
<LI><A HREF="#Heading20">Creating Property Sheets</A>
<LI><A HREF="#Heading21">Listing 6.11 MFCCONTROLWINPPG.CPP--New Member Variables
Added to the DoDataExchange Function</A>
<LI><A HREF="#Heading22">Listing 6.12 MFCCONTROLWINPPG.CPP--Updated DoDataExchange
Function; Alignment Enumeration Added</A>
<LI><A HREF="#Heading23">Listing 6.13 MFCCONTROLWINCTL.CPP--BoundPropertyChanged
Function within the SetAlignment Implementation</A>
<LI><A HREF="#Heading24">Listing 6.14 MFCCONTROLWINPPG.CPP--m_AlignmentValue Class
Member Initialized in the Class Constructor</A>
</UL>
<LI><A HREF="#Heading25">Adding Events</A>
<UL>
<LI><A HREF="#Heading26">Listing 6.15 MFCCONTROLWINCTL.H--FireChange Function Prototype
Added to the Class Definition</A>
<LI><A HREF="#Heading27">Listing 6.16 MFCCONTROLWINCTL.CPP--FireChange Implementation
Added to the MFControlWinCtl.cpp Source File</A>
<LI><A HREF="#Heading28">Listing 6.17 MFCCONTROWINCTL.CPP--FireChange Event Added
to the CaptionMethod Implementation</A>
</UL>
<LI><A HREF="#Heading29">Persistence</A>
<UL>
<LI><A HREF="#Heading30">Listing 6.18 MFCCONTROLWINCTL.CPP--Simple DoPropExchange
Implementation</A>
<LI><A HREF="#Heading31">Listing 6.19 MFCCONTROLWINCTL.CPP--DoPropExchange Implementation
Separated into Distinct Operations</A>
<LI><A HREF="#Heading32">Listing 6.20 EXAMPLE--Example Implementation of the Code
Needed to Support Stock Property Persistence</A>
</UL>
<LI><A HREF="#Heading33">Drawing the Control</A>
<UL>
<LI><A HREF="#Heading34">Standard Drawing</A>
<LI><A HREF="#Heading35">Listing 6.21 MFCCONTROLWINCTL.H--CBrush Member Variable
Added to CMFCControlWinCtrl</A>
<LI><A HREF="#Heading36">Listing 6.22 MFCCONTROLWINCTL.CPP--Standard Drawing Added
to the OnDraw Function</A>
</UL>
<LI><A HREF="#Heading37">From Here...</A>
</UL>
</UL>
<P>
<HR SIZE="4">
<H1><A NAME="Heading1"></A>Using MFC to Create a Basic ActiveX Control</H1>
<UL>
<LI><B>Registration</B>
<SPACER TYPE="VERTICAL" SIZE="2">
Registration is probably the most important aspect of creating a control. MFC registration
is robust and easy to understand.
<P>
<LI><B>Adding methods and properties</B>
<SPACER TYPE="VERTICAL" SIZE="2">
The method is the basis for all communication to the control. Like methods, properties
are a way to expose information about a control implementation to the control's user.
The MFC ClassWizard makes adding methods and properties easy.
<P>
<LI><B>Adding events</B>
<SPACER TYPE="VERTICAL" SIZE="2">
The control uses events to communicate information and conditions to the control's
user.
<P>
<LI><B>Persistence</B>
<SPACER TYPE="VERTICAL" SIZE="2">
Persistence is the control's means to remember information about itself across execution
lifetimes. MFC persistence is based on a set of macros.
<P>
<LI><B>Drawing the control</B>
<SPACER TYPE="VERTICAL" SIZE="2">
Drawing the User Interface can make or break a control implementation.
</UL>
<P>Visual C++ and MFC are powerful and flexible tools for creating ActiveX controls.
In terms of rapid development and ease of use, these two win hands down. In this
chapter, you will see just how powerful these tools are. You will create an ActiveX
control with all the basics: methods, properties, events, persistence, and drawing.
And 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 just a few. <BR>
<BR>
<IMG SRC="bar.gif" WIDTH="480" HEIGHT="6" ALIGN="BOTTOM" BORDER="0"></P>
<BLOCKQUOTE>
<P><B>NOTE:</B> MFC controls are linked dynamically to the MFC DLLs for support and
implementation. Linking dynamically can impair the control's load time because the
MFC DLLs are loaded in addition to the control itself. Even though the MFC DLLs may
already be in memory, you still have DLL address fix-ups and memory swapping issues.
When developing an ActiveX control, the developer is prudent to remove as much dependence
on MFC as possible. This practice serves two purposes: First, this practice makes
it much easier to port the control to an alternative framework such as ATL or BaseCtl;
second, it improves the overall performance of the control because you don't have
the overhead of the MFC DLLs to contend with. As a general rule, components that
are performance-sensitive should avoid using DLLs of any kind. The last thing you
want is a component that is disk-bound.
</BLOCKQUOTE>
<P><IMG SRC="bar.gif" WIDTH="480" HEIGHT="6" ALIGN="BOTTOM" BORDER="0">
<H2><A NAME="Heading2"></A>Creating the Basic Control Project</H2>
<P>To create an MFC 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 (see fig. 6.1),
select the Projects tab. 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 MFC ActiveX ControlWizard; enter the Project <U>N</U>ame
<B>MFCControl</B>, and the Lo<U>c</U>ation will be C:\que\ActiveX\MFCControl. Click
the OK button to start the ControlWizard so you can define the properties of your
control. <BR>
<BR>
<A HREF="Art/06/f_fig01.jpg"><B>FIG. 6.1</B></A> <I><BR>
Define the new MFC control project with the New dialog.</I></P>
<P>The first step in the ControlWizard allows you to define how many controls will
exist within your project (see fig. 6.2). Using the up and down arrow buttons, select
3 for the number of controls, and leave the remainder of the properties set to their
default values. Click the <U>N</U>ext button to continue. <B><BR>
<BR>
</B><A HREF="Art/06/f_fig02.jpg"><B>FIG. 6.2</B></A> <BR>
<I>MFC ActiveX ControlWizard -- Step 1 of 2 is where you define the number of controls
in your project.</I></P>
<P><IMG SRC="bar.gif" WIDTH="480" HEIGHT="6" ALIGN="BOTTOM" BORDER="0"></P>
<BLOCKQUOTE>
<P><B>TIP:</B> If you have a requirement to create more than one control, and the
controls are often used together within the same application, we recommend combining
the controls into a single .ocx file--because of .ocx load times. A single .ocx always
loads faster than two .ocxs of comparable size. Code sharing can also be an advantage,
and the end result is a control that's smaller and loads faster than the two individual
controls combined.
</BLOCKQUOTE>
<P><IMG SRC="bar.gif" WIDTH="480" HEIGHT="6" ALIGN="BOTTOM" BORDER="0"><BR>
<BR>
Step 2 of the ControlWizard allows you to change the name of the control, some of
its features, and whether the control will be a subclass of an existing window's
control (see fig. 6.3). <B><BR>
<BR>
</B><A HREF="Art/06/f_fig03.jpg"><B>FIG. 6.3</B></A> <I><BR>
Use the ControlWizard -- Step 2 of 2 to further define your control.</I></P>
<P>Because you have instructed the Control Wizard that you want 3 controls for this
project, you will rename <TT>MFCControl1</TT>, <TT>MFCControl2</TT>, and <TT>MFCControl3</TT>
to <TT>MFCControlWin</TT>, <TT>MFCControlNoWin</TT>, and <TT>MFCControlSubWin</TT>,
respectively. To rename a class, select the name in the drop-down list box and click
the <U>E</U>dit Names button. Enter the new name of the control in the <U>S</U>hort
Name edit box (see fig. 6.4). The names of the other files and classes will change
automatically to conform to the new <U>S</U>hort Name. Click OK to close the Edit
Names dialog. <B><BR>
<BR>
</B><A HREF="Art/06/f_fig04.jpg"><B>FIG. 6.4</B></A> <I><BR>
Edit the names of the control using the Edit Names dialog.</I></P>
<P>The A<U>d</U>vanced button is used to display a dialog for selecting the ActiveX
enhancements that you want for the control (see fig. 6.5). Ensure that the <TT>MFCControlWin</TT>
class is selected, and click the A<U>d</U>vanced button. <BR>
<BR>
<A HREF="Art/06/f_fig05.jpg"><B>FIG. 6.5</B></A> <BR>
<I>Select from the Advanced ActiveX Features dialog for your control.</I></P>
<P>All of the advanced features in the dialog are the result of the OC 96 specification
referred to in <A HREF="ch01.htm">Chapter 1</A>. Now take a minute to look at the
features in detail, as shown in Table 6.1.
<TABLE BORDER="1" WIDTH="100%">
<CAPTION><B>Table 6.1</B><SPACER TYPE="HORIZONTAL" SIZE="10"><B> Advanced ActiveX Features</B></CAPTION>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><B>ActiveX Feature</B></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><B>Description</B></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><U>W</U>indowless activation</TD>
<TD ALIGN="LEFT" VALIGN="TOP">Allows the control to be instantiated without having to create a window for itself--relies
on the container to do so.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><U>U</U>nclipped device context</TD>
<TD ALIGN="LEFT" VALIGN="TOP">Used for controls that guarantee they will not draw outside their client area and
results in disabling MFC clipping tests for the control. Cannot be used with a windowless
control.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><U>F</U>licker-free activation</TD>
<TD ALIGN="LEFT" VALIGN="TOP">Used for controls that appear the same to the user whether the control is in an active
or inactive state. Selecting this feature eliminates some of the flicker associated
with changing states between the active and inactive states of a control. Cannot
be used with a windowless control.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -