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

📄 ch19.htm

📁 VC 21天 学习VC 的好东西
💻 HTM
📖 第 1 页 / 共 4 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>

<HEAD>
	<META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1">
<SCRIPT LANGUAGE="JavaScript">

<!--

function popUp(pPage) {
 var fullURL = document.location;
 var textURL = fullURL.toString();
 var URLlen = textURL.length;
 var lenMinusPage = textURL.lastIndexOf("/");
 lenMinusPage += 1;
 var fullPath = textURL.substring(0,lenMinusPage);
 popUpWin = window.open('','popWin','resizable=yes,scrollbars=no,width=525,height=394');
 figDoc= popUpWin.document;
 zhtm= '<HTML><HEAD><TITLE>' + pPage + '</TITLE>';
 zhtm += '</head>';
 zhtm += '<BODY bgcolor="#FFFFFF">';
 zhtm += '<IMG SRC="' + fullPath + pPage + '">';
 zhtm += '<P><B>' + pPage + '</B>';
 zhtm += '</BODY></HTML>';
 window.popUpWin.document.write(zhtm);
 window.popUpWin.document.close();
 // Johnny Jackson 4/28/98
 }

//-->
                                                                
</SCRIPT>

	<META NAME="GENERATOR" Content="Symantec Visual Page Mac 1.1.1">
	<TITLE>Teach Yourself Visual C++ 6 in 21 Days -- Ch 19 -- Building Your Own Widgets--Creating ActiveX Controls</TITLE>
</HEAD>

<BODY TEXT="#000000" BGCOLOR="#FFFFFF">

<H1 ALIGN="CENTER"><IMG SRC="../button/sams.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM"
BORDER="0"><BR>
Teach Yourself Visual C++ 6 in 21 Days</H1>
<CENTER>
<P><A HREF="../ch18/ch18.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"
ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch20/ch20.htm"><IMG
SRC="../button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"
BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../button/contents.gif" WIDTH="128"
HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A> 
<HR>

</CENTER>
<H1 ALIGN="CENTER">- 19 -<BR>
Building Your Own Widgets--Creating ActiveX Controls</H1>
<H1></H1>

<UL>
	<LI><A HREF="#Heading1">What Is an ActiveX Control?</A>
	<UL>
		<LI><A HREF="#Heading2">Properties</A>
		<LI><A HREF="#Heading3">Methods</A>
		<LI><A HREF="#Heading4">Events</A>
	</UL>
	<LI><A HREF="#Heading5">Creating an ActiveX Control</A>
	<UL>
		<LI><A HREF="#Heading6">Building the Control Shell</A>
		<LI><A HREF="#Heading7">Modifying the CModArt Class</A>
		<LI><A HREF="#Heading8">Adding Properties</A>
		<LI><A HREF="#Heading9">Designing and Building the Property Page</A>
		<LI><A HREF="#Heading10">Adding Basic Control Functionality</A>
		<LI><A HREF="#Heading11">Adding Methods</A>
		<LI><A HREF="#Heading12">Testing the Control</A>
	</UL>
	<LI><A HREF="#Heading13">Summary</A>
	<LI><A HREF="#Heading14">Q&amp;A</A>
	<LI><A HREF="#Heading15">Workshop</A>
	<UL>
		<LI><A HREF="#Heading16">Quiz</A>
		<LI><A HREF="#Heading17">Exercises</A>
	</UL>
</UL>

<P>
<HR SIZE="4">
<BR>
The software industry has seen a revolution over the past couple years. How software
is built and packaged has moved from a model where all applications are large, monolithic
pieces of executable code to a model where most applications consist of small building
blocks. These small building blocks, often called components, can be created using
any of several different languages and can take many different forms. One of the
most popular components is the ActiveX control. If you know how to create your own
ActiveX controls, you can build your own components and provide them to other programmers.
Today, you will learn</P>

<UL>
	<LI>How to use the Visual C++ wizards to build ActiveX controls.
	<P>
	<LI>How to add properties and methods to your controls using the Class Wizard.
	<P>
	<LI>How to test your control using the tools provided with Visual C++.
</UL>

<H2><A NAME="Heading1"></A>What Is an ActiveX Control?</H2>
<P>An ActiveX control is a set of functionality packaged in a COM (Component Object
Model) object. This COM object is self-contained, although it does not have the ability
to run by itself. An ActiveX control can only run within a ActiveX container, such
as a Visual C++ or Visual Basic application.</P>
<P>As you learned on Day 9, &quot;Adding ActiveX Controls to Your Application,&quot;
ActiveX controls provide a series of interfaces used by the container application
to trigger the various sets of functionality contained in the control. Many of these
interfaces are used for triggering events in the control or in the containing application.
Others are for specifying the property page of the control or for communicating whether
the control has been activated. All in all, so many interfaces are built into most
ActiveX controls that coding the functionality for each of these interfaces yourself
would take quite some time. Luckily, the Visual C++ App and Class Wizards add much
of this functionality for you, allowing you to focus on the specific functionality
that the control is supposed to have.</P>
<P>Among the aspects of the control you create that you still must plan yourself
are what properties, methods, and events you will expose for your control. You can
add these elements to your control through the Class Wizard, but if any of the properties
or events require special code on your part, then you must add it yourself. As should
be expected with any methods that you add to your controls, you have to supply all
of the code. The Class Wizard will add the surrounding structure and code to allow
the containing application to see and call the method, just as it will add all the
code necessary to call any event handlers for your applications.</P>
<P>
<H3><A NAME="Heading2"></A>Properties</H3>
<P>Properties are attributes of controls that are visible to, and often modifiable
by, the container application. The four basic types of properties are ambient, extended,
stock, and custom. Ambient properties are provided by the container application to
the control--such things as background color or the default font to be used--so that
the control looks like part of the container application. Extended properties are
not actually properties of the control but instead are provided and implemented by
the container application, such as tab order. The control may extend these properties
somewhat; for example, if the control contains two or more standard controls, it
may control the tab order within the overall control, returning the tab order control
to the application once the control has completed its internal tab order. Stock properties
are implemented by the ActiveX control development kit, such as control font or control
background color. The final type of properties, custom properties, are what you are
most concerned with because these properties are specific to your control and are
directly related to the functionality of your control.</P>
<P>You can specify any properties you need in your control using the Automation tab
on the Class Wizard. When you add a new property to your control through the Class
Wizard, you'll specify several aspects of the property.</P>
<P>The first aspect is the external property name, which is the name shown to the
containing application for the property. Another aspect that you can specify is the
internal variable name, which is used in your code, but only if the property is implemented
as a member variable. You also specify the variable type for the property.</P>
<P>If you specify that the property is to be implemented as a member variable (the
property is a member variable of the control class), then you can specify the name
of the notification function, which is called when the property is changed by the
containing application. If the property is not a member variable of the control class,
you need to specify that it is altered and viewed through Get and Set methods, where
the containing application calls a Get method to get the current value of the property
and calls a Set method to change the value of the property. If the property is maintained
through Get and Set methods, then you can specify the names of these two methods.</P>
<P>For all these aspects of a property, the Add Property dialog suggests appropriate
names for everything once you enter the external name for the property. If you want
to accept the default names, the only things you need to specify are the external
name, the type, and whether the property is a member variable or uses Get and Set
methods. If you choose a stock property from the list of available stock properties,
the rest of the elements are automatically specified for you. Once you specify all
of this information, the Class Wizard adds all of the necessary code and variables
to your control project.</P>
<P>
<H3><A NAME="Heading3"></A>Methods</H3>
<P>Methods are functions in the control that can be called by the container application.
These functions are made available to other applications through the IDispatch interface,
which we discussed on Day 9. Because of the way the IDispatch works in calling the
methods in a control, the variables passed to the method have to be packaged in a
structure that is passed to the control. This structure is machine independent so
that it doesn't matter whether your control is running with Windows 95/98 on an Intel
Pentium II or on a Windows NT with a MIPS or Alpha processor; the structure will
look the same. It is the responsibility of each side of the function call to convert
the parameters as necessary to fit them into the structure correctly or to extract
them from the structure. This process of packaging the method parameters is called
marshaling.</P>
<P>When you add a new method to your control through the Class Wizard on the Automation
tab, the Class Wizard adds all of the necessary code to perform the marshaling of
the parameters, as well as all other supporting functionality, including building
the IDispatch interface and table.</P>
<P>When you add a new method to your control through the Class Wizard, you are asked
to provide the external name for the method called by the container application.
Your method will get a default internal name, which you can override by entering
your own internal name. Other aspects of your control methods that you have to specify
are the method's return type and the parameters for the method. Once you finish entering
all this information, the Class Wizard adds all the necessary code to the control.</P>
<P>
<H3><A NAME="Heading4"></A>Events</H3>
<P>Events are notification messages that are sent from the control to the container
application. They are intended to notify the application that a certain event has
happened, and the application can take action on that event if desirable. You can
trigger two types of events from your control, stock or custom events. Stock events
are implemented by the ActiveX control development kit and are available as function
calls within the control. These stock events enable you to trigger events in the
container application for mouse or keyboard events, errors, or state changes.</P>
<P>Along with the stock events, you can add your own custom events to be triggered
in the container application. These events should be related to the specific functionality
of your control. You can specify arguments to be passed with the event to the container
application so that the application can have the data it needs for reacting to the
event message.</P>
<P>When you need to trigger any of these events, all you do is call the internal
event function that fires the event, passing all the necessary parameters to the
function. The Class Wizard will have added all of the necessary code to trigger the
event message from the internal function call.</P>
<P>Events are one of the three elements that you do not add to your controls through
the Automation tab in the Class Wizard. Events are added through the ActiveX Events
tab in the Class Wizard.</P>
<P>
<H2><A NAME="Heading5"></A>Creating an ActiveX Control</H2>
<P>The ActiveX control that you will build as the example today is the squiggle drawing
module that you packaged as a library module and then as DLLs on Day 16, &quot;Creating
Your Own Classes and Modules,&quot; and Day 17, &quot;Sharing Your Functionality
with Other Applications--Creating DLLs.&quot; In converting this module into an ActiveX
control, you'll expose the maximum number of squiggles that the control will draw,
as well as the maximum length of the squiggles, as properties that the container
application can set. Every time the control is clicked, you'll program it to create
a new squiggle drawing. You'll also add a method to load a squiggle drawing into
the control that was created with the previous versions of the squiggle module. Finally,
you'll have the control fire an event to let the container application know that
the control has loaded the drawing.</P>
<P>
<H3><A NAME="Heading6"></A>Building the Control Shell</H3>
<P>You've probably noticed by now that one of the options on the new project dialog
is an MFC ActiveX Control Wizard. This is another project wizard just like the AppWizard
for creating application and DLL projects. You can use it to build a shell for any
ActiveX controls that you want to build. It will create all of the necessary files
and configure the project so that the compiler will build an ActiveX control when
you compile.</P>
<P>When you start the Control Wizard, you are asked some simple questions about your
control project, such as how many controls will be in the project and whether the
controls will have runtime licenses.</P>


<BLOCKQUOTE>
	<P>

⌨️ 快捷键说明

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