dephi7开发ocx

Delphi7 开发ocx

分享
2009-09-20 12:59

关于delphi7 ActiveX的学习备忘

Author :5caidm ,2008-06-12

步骤:

1、先创建ActiveX工程文件;

 2、注册ActiveX文件(ocx文件) ;

3、编写相应调用ActiveX文件(.ocx文件)的html文件.

具体操作流程如下:

步骤1:

a、选择File->New->Other ,ActiveX页面,选择ActiveX Form ;

b、填写New ActiveX Name:,其他的不用做变动(不好意思,我也不知道其他的什么意思);

c、成功创建ActiveX Form,保存工程,三个文件 (AAImpl1.pas、AAProj1.dpr加入ActiveX Name为AA);

d、AAProj1_TLB、AAProj1、AAImpl1;

e、增加接口(两种方法):

1、选中AAImpl1单元,即选中ActiveX Form,在delphi的Edit菜单中选择Add to Interface...;  

1> 在Add To Interface 窗口中加入接口Func:Function Func(x1,x2 :Integer) : Integer ; 

 2>最后OK就可以,delphi会自动帮你检查语法,如果语法或者参数类型或者返回值类型不对,delphi开发环境会自动给出提示。  

3>注意:该接口函数有两个参数 x1、x2,为Integer型 ,返回值也为Integer;  

4>参数和返回值类型说明:integer --long;String为BStr或者WideString ;

2、选中AAImpl1单元,即选中ActiveX Form, 在delphi的View菜单中选择Type Library;  

1> 在AAProj1.tlb 窗口中,在左侧列表中选择IAA,即选中IAA接口,右键->New->Method或者在在工具栏中选择New Method   ,接着给方法命名test;  

2>选中test,在选择窗口右边的Parameters页面,对该方法进行参数和返回值的进行增加,Return Type不需要改变;  

3>比如test的返回值为String,有一个String型的参数,则在Parameters中增加两个变量,具体如下   

   中文说明     Name       Type        Modifier     参数:      sVar       BSTR        [in]  //[in]表示输入参数     返回值:    sRet       BSTR*       [out,retvar] //表示输出结果,Type一定要为指针类型,否则出错,即一定要带*

     则AAImpl1中产生:     function TAA.test(const sVar: WideString): WideString;     begin      //编写函数体      result := sVar ; //此句为自己编写     end; 

 4>参数和返回值类型说明:integer --long;String为BStr或者WideString ;

f、增加属性Property:同样选中AAImpl1单元,即选中ActiveX Form, 在delphi的View菜单中选择Type Library;    在左侧列表中选择IAA,即选中IAA接口,右键->New->Property或者在在工具栏中选择New Property  ,接着给属性命名为Mystr,    在Parameters中修改Type为BSTR;  

 function TAA.Get_Mystr: WideString;  

 begin   //编写函数体    

end;

  procedure TAA.Set_Mystr(Value: WideString); 

 begin  //编写过程体 

 end; 

       以上为搭建框架,接下来在ActiveX Form上增加一个TButton(btn_AX)和一个TEdit(edt_AX)控件

g、回到AAImpl1页面代码窗口中,  

 function TAA.Get_Mystr: WideString;//得到 Mystr  

 begin   //编写函数体 

  Result := Self.edt_AX.Text ; 

  end;

  procedure TAA.Set_Mystr(Value: WideString);//设置Mystr  

begin  //编写过程体 

 Self.edt_AX.Text := Value ;  

end;  

 procedure TAA.btn_AXClick(Sender: TObject); 

begin 

 Self.ClickEvent(Self);

 end; 

ActiveX Form 编辑完毕,Project->Builder All Projects. //编译工程,生成AAProj1.Ocx文件。

步骤2:注册ActiveX文件(ocx文件) 两种方法注册ActiveX文件:

1、在delphi开发环境中,Run-> Register ActiveX Server //注册Ocx控件。

 2、开始->运行->regsvr32  "Ocx文件所在目录/AAProj1.ocx".

步骤3:编写相应调用ActiveX文件(.ocx文件)的html文件. 注:如果在delphi 7中可能会麻烦一点,至少我学习时是麻烦啦;方法:

1>Project->Web Deployment Options(Web部署选项)->Project ;

2>Target dir(Full path of the deployed OCX):选择或填写OCX的完整路径目录;   Target URL(Virtual path of the deployed OCX):填写 ./;   HTML dir(Full path of the deployed HTML file):选择或填写HTML文件的完整路径目录;   其他选项默认。   注:如果Web Deployment Options为不可用状态,则在File->New->Other->ActiveX->ActiveX Form接着删除掉这个ActiveX Form,此时Web Deployment Options为可用状态啦,他娘的,这种Bug 也有人能解决,真人才;

3>Project->Web Deploy ;

4>调试方法:Run->Parameters,在Local页面中  Host Application :C:/Program Files/Internet Explorer/IEXPLORE.EXE //你的IE在哪里就填写那里啦;  Parameters:文件路径/AAProj1.htm 调试运行即可以;

至此结束;

以下内容为ActiveX和网页Html交互内容脚本:

<HTML> <H1> Delphi 7 ActiveX Test Page </H1><p> You should see your Delphi 7 forms or controls embedded in the form below. <HR><center><P>

<OBJECT

  id=TestActiveProj1   

classid="clsid:A215EF7E-1486-4C7A-8A5F-16C640D73C65"  

 codebase="./AAProj1.ocx#version=1,0,0,0"   

width=350  

 height=300  

 align=center   

hspace=0   

vspace=0 >

 </OBJECT>

<body>

  <head> <!--<title>无标题文档</title> -->

<script language="javascript">

<!--

function onloadtime() {

document.myform.txtDate.value=AAProj1.Func(1,8) ;

document.all.AAProj1.Mystr = document.all["txtTest"].value ;

 }

function ontestclick() {

document.myform.txtDate.value=AAProj1.Func(52,8) ;

document.myform.txtTest.value = AAProj1.Mystr ;

}

</script>

<script language="javascript" for=AAProj1 event=onclick>   

  document.myform.txtTest.value = AAProj1.Mystr ;

</script>

 </head>

<body οnlοad="onloadtime()">

<form name=myform><div align="center">  

<input name="txtTest" type="text" value="" size="20">

 <input name="txtDate" type="text" value="" size="20"> 

<input name="btn" type="Button"  value="test" οnclick="onloadtime()" size="40">  

 <input name="btn1" type="Button"  value="Click" οnclick="ontestclick()"  size="40">

 </div>

 </form>

</body> </HTML>

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/cai5/archive/2008/06/13/2543761.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值