软件组件技术实验3.docx

上传人:b****5 文档编号:7688890 上传时间:2023-01-25 格式:DOCX 页数:19 大小:384.31KB
下载 相关 举报
软件组件技术实验3.docx_第1页
第1页 / 共19页
软件组件技术实验3.docx_第2页
第2页 / 共19页
软件组件技术实验3.docx_第3页
第3页 / 共19页
软件组件技术实验3.docx_第4页
第4页 / 共19页
软件组件技术实验3.docx_第5页
第5页 / 共19页
点击查看更多>>
下载资源
资源描述

软件组件技术实验3.docx

《软件组件技术实验3.docx》由会员分享,可在线阅读,更多相关《软件组件技术实验3.docx(19页珍藏版)》请在冰豆网上搜索。

软件组件技术实验3.docx

软件组件技术实验3

和兵兵

软件组件技术

实验指导书

 

河南科技大学国际教育学院

 

实验3控件的开发

实验目的:

1.进一步熟练掌握软件组件技术。

2.掌握ActiveX控件的编写、注册和使用等过程。

3.了解控件的开发过程和运行原理

实验环境:

Windows系统平台和C++Builder开发环境。

实验学时:

4学时

实验内容:

开发一个实现Beep功能的组件。

要求根据此说明,编写一个实现特定功能的组件,进行组件工程的创建、保存和组件的创建保存,并在上次的调用程序下调用测试。

要求记录操作的步骤和遇到的问题及解决方法,能够熟练编写简单的组件。

并掌握BorlandC++Builder环境下不同类型组件的编写和调用。

实验步骤:

实验前首先根据要求设计一个COM对象和其接口,并熟悉C++Builder中组件编制的环境。

创建控件:

步骤1:

创建控件

从VCL类生成控件:

允许用户选择一个VCL类为基类

步骤1.1:

ChooseFile|New|OthertoopentheNewItemsdialogbox.

步骤1.2:

选中ActiveX属性页.

步骤1.3:

双击ActiveXControl图标或选中它后单击”OK”按钮

基于VCL类生成控件:

它以TActiveForm为基类

步骤1.1:

选择File|New|Other打开“NewItems”对话框,选中”ActiveX”属性页.

步骤1.2:

双击ActiveXForm图标或选中后单击”OK”按钮

•*XControl.*:

控件工程名,包含了启动控件所需的代码,一般不需要改变。

•_TLB.*:

类型库文件,定义了控件的CoClass类、面向客户的接口以及它们所需的类型的定义。

•*Impl*.*:

ActiveX控件实现文件,定义并实现控件。

以ActiveTemplateLibrary(ATL)的VCLCONTROL_IMPL宏为基础.

•*_ATL.cpp(.h):

一个ATLunit,前面省略的是工程名,包含了对所需类的引用和声明.

•AnAboutboxformandunitifyourequestedthem.

•A.LICfileifyouenabledlicensing.

步骤2:

创建组件

使用COM对象向导

步骤2.1:

选择File|New|Other打开“NewItems”对话框,选中”ActiveX”属性页.

步骤2.2:

双击ComObject图标或选中后单击”OK”按钮

步骤2.3:

设置弹出的”NewCOMObject”对话框,设置内容如下:

•CoClassname:

组件对象名.

•ImplementedInterface:

实现的接口,缺省名字为:

“I”+组件对象名,也可以单击List在弹出的对话框中选择所需接口,显示为所选接口名。

•ThreadingModel:

组件的线程模型,一般选默认值即可。

•Eventsupport:

表示是否自动生成部分组件支持事件的代码。

•Oleautomation:

表示生成组件是否与自动化对象兼容,选中可自动生成部分代码,自定义接口时有效。

•ImplementAncestorInterfaces:

选中要实现接口时有效,选中后可自动生成接口的实现。

但不生成IUnknown,IDispatch和IAppServer的实现.

步骤2.4:

设置完后,单击OK,弹出如下对话框。

使用自动化对象向导

步骤2.1:

选择File|New|Other打开“NewItems”对话框,选中”ActiveX”属性页.显示同COM对象。

步骤2.2:

双击AutomationObject图标或选中后单击”OK”按钮,弹出如下对话框。

步骤2.3:

设置CoClassname、ThreadingModel、Eventsupport,其含义同COM对象。

步骤3:

设置组件选项

单击“Project|Option”,选中Option,设置ATL,可选默认设置,各项含义如下:

Instancing:

确定COM对象的实例数,对进程内组件服务无效。

-SingleUse:

客户实例化对象后,COM将其从视图中删除,其它客户可以创建他们自身的对象实例。

-Multipleuse:

每个客户可以创建自己的实例,且都在同一进程空间。

OLEInitializationCOINIT_XXXflag:

确定COM怎么初始化

-apartment-threaded:

对象在创建的线程中调用

-multithreaded:

对象可以在多个线程调用

Debuggingflags:

设置调试方式.

步骤4:

定义组件

步骤4.1:

在弹出的类型库编辑器中,在左侧窗口中,单击鼠标右键,在弹出的菜单中,单击New.

步骤4.2:

选中需要增加的项,单击鼠标或直接按红色方框圈中的按钮。

添加方法:

步骤4.2.1:

在new弹出的菜单中,选中Method,则在左侧窗口中增加”Method1”,可更改名字.

步骤4.2.2:

可在方法名中或者在“Attributes”属性页下Name文本框内,输入新的名字,在“Parameters”属性页下,输入属性参数。

步骤4.2.3:

单击刷新按钮,生成方法的框架代码。

步骤4.2.4:

在图中蓝色区域所在的括号内属于方法的相关代码。

添加属性:

步骤4.2.1:

在new弹出的菜单中,选中Progerty,则在左侧窗口中增加两个”Progerty1”,分别表示属性的读和写.

步骤4.2.2:

可在属性名中或者在“Attributes”属性页下Name文本框内,输入新的名字.

步骤4.2.3:

单击刷新按钮,生成方法的框架代码。

步骤4.2.4:

在图中蓝色区域所在的括号内属于方法的相关代码。

CustomizingtheActiveXcontrol’sinterface

TheActiveXControlandActiveFormwizardsgenerateadefaultinterfaceforthe

ActiveXwrapperclass.Thisdefaultinterfacesimplyexposestheproperties,

methods,andeventsoftheoriginalVCLcontrolorform,withthefollowing

exceptions:

•Data-awarepropertiesdonotappear.BecauseActiveXcontrolshaveadifferent

mechanismformakingcontrolsdata-awarethanVCLcontrols,thewizardsdonot

convertpropertiesrelatedtodata.See“Enablingsimpledatabindingwiththe

typelibrary”onpage43-11forinformationonhowtomakeyourActiveXcontrol

data-aware.

•Anyproperty,method,oreventthattypethatisnotAutomation-compatibledoes

notappear.YoumaywanttoaddthesetotheActiveXcontrol’sinterfaceafterthe

wizardhasfinished.

Youcanadd,edit,andremovetheproperties,methods,andeventsinanActiveX

controlbyeditingthetypelibraryusingtheTypeLibraryeditorasdescribedin

Chapter39,“Workingwithtypelibraries.”

NoteYoucanaddunpublishedpropertiestoyourActiveXcontrol’sinterface.Such

propertiescanbesetatruntimeandwillappearinadevelopmentenvironment,but

changesmadetothemwillnotpersist.Thatis,whentheuserofthecontrolchanges

thevalueofapropertyatdesigntime,thechangesarenotreflectedwhenthecontrol

isrun.ThisisbecauseActiveXcontrolsusetheVCLstreamingsystemratherthanthe

ATLstreamingsystem.IfthesourceisaVCLobjectandthepropertyisnotalready

published,youcanmakepropertiespersistentbycreatingadescendantoftheVCL

objectandpublishingthepropertyinthedescendant.

YoumayalsochoosenottoexposealloftheVCLcontrol’sproperties,methods,and

eventstohostapplications.YoucanusetheTypeLibraryeditortoremovethesefrom

theinterfacesthatthewizardgenerated.Whenyouremovepropertiesandmethods

fromaninterfaceusingtheTypeLibraryeditor,theTypeLibraryeditordoesnot

removethemfromthecorrespondingimplementationclass.EdittheActiveX

wrapperclassintheimplementationunittoremovetheseafteryouhavechangedthe

interfaceintheTypeLibraryeditor.

WarningAnychangesyoumaketothetypelibrarywillbelostifyouregeneratetheActiveX

controlfromtheoriginalVCLcontrolorform.

TipItisagoodideatocheckthemethodsthatthewizardaddstoyourActiveXwrapper

class.Notonlydoesthisgiveyouachancetonotewherethewizardomittedany

data-awarepropertiesormethodsthatwerenotAutomation-compatible,italsolets

youdetectmethodsforwhichthewizardcouldnotgenerateanimplementation.

Suchmethodsappearwithacommentintheimplementationthatindicatesthe

problem.

CreatinganActiveXcontrol43-9

CustomizingtheActiveXcontrol’sinterface

Addingadditionalproperties,methods,andevents

AddingpropertiesandmethodstoyourActiveXcontrol’sinterfaceworksthesame

wayasaddingproperties,methods,andeventstoanyCOMinterface.Addingevents

isthesameasaddingmethods(eventhandlers),exceptthatyouaddthemtothe

Eventsinterfaceratherthantheobject’sdefaultinterface.Usingthetypelibraryto

addpropertiesandeventsisdescribedin“DefiningaCOMobject’sinterface”on

page41-9.

WhenyouaddtoanActiveXcontrol’sinterface,youareoftenjustsurfacinga

property,method,oreventintheunderlyingVCLcontrol.Thefollowingtopics

describehowtodothis.

Addingpropertiesandmethods

TheActiveXwrapperclassimplementspropertiesinitsinterfaceusingreadand

writeaccessmethods.Thatis,thewrapperclasshasCOMproperties,whichappear

onaninterfaceasgetterand/orsettermethods.UnlikeVCLproperties,youdonot

seea“property”declarationontheinterfaceforCOMproperties.Rather,yousee

methodsthatareflaggedaspropertyaccessmethods.Whenyouaddapropertyto

theActiveXcontrol’sdefaultinterface,thewrapperclassdefinition(whichappearsin

the_TLBunitthatisupdatedbytheTypeLibraryeditor)gainsoneortwonew

methods(agetterand/orsetter)thatyoumustimplement,justaswhenyouadda

methodtotheinterface,thewrapperclassgainsacorrespondingmethodforyouto

implement.Thus,addingpropertiestothewrapperclass’sinterfaceisessentiallythe

sameasaddingmethods:

thewrapperclassdefinitiongainsnewskeletalmethod

implementationsforyoutocomplete.

NoteFordetailsonwhatappearsinthegenerated_TLBunit,see“Codegeneratedwhen

youimporttypelibraryinformation”onpage40-5.

Forexample,consideraCaptionproperty,oftypeAnsiStringintheunderlyingVCL

object.WhenyouaddthispropertyintheTypeLibraryeditor,C++Builderaddsthe

followingdeclarationstothewrapperclass:

STDMETHOD(get_Caption(BSTR*Value));

STDMETHOD(set_Caption(BSTRValue));

Inaddition,itaddsskeletalmethodimplementationsforyoutocomplete:

STDMETHODIMPTButtonXImpl:

:

get_Caption(BSTR*Value)

{

try

{

}

catch(Exception&e)

{

returnError(e.Message.c_str(),IID_IButtonX);

}

returnS_OK;

};

43-10Developer’sGuide

CustomizingtheActiveXcontrol’sinterface

STDMETHODIMPTButtonXImpl:

:

set_Caption(BSTRValue)

{

try

{

}

catch(Exception&e)

{

returnError(e.Message.c_str(),IID_IButtonX);

}

returnS_OK;

};

Typically,youcanimplementthesemethodsbysimplydelegatingtotheassociated

VCLcontrol,whichcanbeaccessedusingthem_VclCtlmemberofthewrapperclass:

STDMETHODIMPTButtonXImpl:

:

get_Caption(BSTR*Value)

{

try

{

*Value=WideString(m_VclCtl->Caption).Copy();

}

catch(Exception&e)

{

returnError(e.Message.c_str(),IID_IButtonX);

}

returnS_OK;

};

STDMETHODIMPTButtonXImpl:

:

set_Caption(BSTRValue)

{

try

{

m_VclCtl->Caption=AnsiString(Value);

}

catch(Exception&e)

{

returnError(e.Message.c_str(),IID_IButtonX);

}

returnS_OK;

};

Insomecases,youmayneedtoaddcodetoconverttheCOMdatatypestonative

C++types.Theprecedingexamplemanagesthiswithtypecasting.

Addingevents

TheActiveXcontrolcanfireeventstoitscontainerinthesamewaythatan

automationobjectfireseventstoclients.Thismechanismisdescribedin“Exposing

eventstoclients”onpage41-10.

IftheVCLcontrolyouareusingasthebasisofyourActiveXcontrolhasany

publishedevents,thewizardsautomaticallyaddthenecessarysupportformanaging

alistofclienteventsinkstoyourActiveXwrapperclassanddefinetheoutgoing

dispinterfacethatclientsmustimplementtorespondtoevents.

CreatinganActiveXcontrol43-11

CustomizingtheActiveXcontrol’sinterface

Inordertofireeventstothecontainer,yourActiveXwrapperclassmustimplement

aneventhandlerfortheeventontheVCLobject.Thiseventhandlercallsthe

Fire_EventNamemethodthatisimplementedbytheTEvents_CoClassNameclass

definedinthe_TLBunit:

void__fastcallTButtonXImpl:

:

KeyPressEvent(TObject*Sender,char&Key)

{

shortTempKey;

TempKey=(short)Key;

Fire_OnKeyPress(&TempKey);

Key=(short)TempKey;

};

YoumustthenassignthiseventhandlertotheVCLcontrolsothatitiscalledwhen

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 农林牧渔 > 林学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1