使用.net创建activex控件

1. . 创建.net activeX
示例: http://www.codeproject.com/cs/miscctrl/exposingdotnetcontrols.asp
首先要在类定义前面添加:
[ProgId("MyClassName")] COM调用名称
[Guid("MyGUID")] sdf  COM对应的GUID
[ComVisible(true)]   是否为COM组件

activeX自注册方法:
///<summary>
///Register the class as a control and set its CodeBase entry
///</summary>
///<param name="key">The registry key of the control</param>
[ComRegisterFunction()]
public static void RegisterClass ( string key )
{
.

.
}
 
///<summary>
///Called to unregister the control
///</summary>
///<param name="key">Tke registry key</param>
[ComUnregisterFunction()]
public static void UnregisterClass ( string key)
{
.
.
}

然后可以用: regasm /codebase MyAssemblie.dll 注册此activex或 regasm /u MyAssemblie.dll  反注册此DLL
其中activeX中调用的方法需要添加[ComVisible(true)]

最后再建立一个SNK文件: sn –k Kosmala.Michal.ActiveXReport.snk
并添加到assemblyinfo.cs中.
[assembly:AssemblyKeyFile("http://www.cnblogs.com/Kosmala.Michal.ActiveXReport.snk")]

生成并注册activeX之后,就可以在HTML中用JS调用了.

示例:
<body onload="OpenActiveX()">

  <!-- Our activeX object -->
  <OBJECT id="OurActiveX" name="ourActiveX" classid="clsid:121C3E0E-DC6E-45dc-952B-A6617F0FAA32" VIEWASTEXT codebase="OurActiveX.cab"></OBJECT> 

  <!-- Attaching to an ActiveX event-->
<script language="javascript">
           function OurActiveX::OnClose(redirectionUrl)
    {
  alert(redirectionUrl);   <!-- http://otherwebsite.com should be returned-->
                    //window.location = redirectionUrl;
           }
</script>


<script language="javascript">
 //Passing parameters to ActiveX object and starting application
function OpenActiveX()
{
 try
 {
  document.OurActiveX.MyParam = "Hi I am here." //Passing parameter to the ActiveX
  document.OurActiveX.Open(); //Running method from activeX
 }
 catch(Err)
 {
  alert(Err.description);
 }


</script>

  </body>

如果要更友好的使用,则应该建立一个cab文件,放在html文件目录下,这样访问此HTML,而找不到ACTIVEX时,会自动提示安装此CAB.


ref
http://www.codeproject.com/KB/cs/CreateActiveXDotNet.aspx

posted @ 2008-03-10 18:22  margiex  阅读(3161)  评论(0编辑  收藏  举报