[转] 使用C#开发ActiveX控件
2016-09-19 13:56
387 查看
双魂人生 原文 使用C#开发ActiveX控件
ActiveX 是一个开放的集成平台,为开发人员、用户和 Web生产商提供了一个快速而简便的在 Internet 和 Intranet 创建程序集成和内容的方法。 使用 ActiveX, 可轻松方便的在 Web页中插入 多媒体效果、 交互式对象、以及复杂程序,创建用户体验相当的高质量多媒体 CD-ROM 。
简单的说,用activeX和js差不多,但是有些是js无法实现的,这个时候就可以考虑一下activeX,一般要求是在客户端执行的程序,比如对本机的串口操作等,下面来简单介绍下如何用C#自己开发一个activeX组件,并在web中应用
View Code
这样ActiveX的基本设置完成了,这个时候我们就可以利用ActiveX实现自己需要的功能了,我们两个举个例子
一、 在页面中放置一个文本框和一个按钮,当点击按钮的时候,把ActiveX中的值传到页面的文本框中
二、 在ActiveX的控件中放置一个按钮,当触发按钮事件后,把ActiveX的值传到页面的文本框中
上面这两个例子其实就是想实现如何实现js调用ActiveX和ActiveX调用页面的Js的,现在我们写一下代码,先看看第一种情况,在ActiveX中我们写了一个GetStr方法,用来返回一个值,然后把这个值传到页面中
好了,现在我们需要新建一个安装文件
1、 新建一个安装项目
![](https://images.cnblogs.com/cnblogs_com/shuang121/a5.jpg)
2、 在项目上点右键,【添加】->【项目输出】,选择上边的项目
![](https://images.cnblogs.com/cnblogs_com/shuang121/a6.jpg)
如果有多个项目,可以选择需要安装的项目就可以了
点击项目,按下“F4”,设置属性
![](https://images.cnblogs.com/cnblogs_com/shuang121/a7.jpg)
3、 生成项目
会生成两个文件,一个exe文件和一个msi文件
![](https://images.cnblogs.com/cnblogs_com/shuang121/a8.jpg)
然后把这两个文件拷到项目的lib文件夹下面
1、 新建一个页面,添加下面的代码
然后在页面中添加javascript代码,按钮和文本框
这个时候我们就可以打开浏览器浏览页面了
![](https://images.cnblogs.com/cnblogs_com/shuang121/a9.jpg)
运行加载提示的加载项就可以了
![](https://images.cnblogs.com/cnblogs_com/shuang121/a10.jpg)
这个时候我们点击按钮传值就可以把ActiveX中的值传过来了
![](https://images.cnblogs.com/cnblogs_com/shuang121/a11.jpg)
接着看看如何用ActiveX调用Javascript
首先在ActiveX中写一个方法用来将页面的js函数传过来。
页面的js函数
ActiveX中我们需要添加引用
![](https://images.cnblogs.com/cnblogs_com/shuang121/a12.jpg)
上面是activeX的两个简单应用。
ActiveX 是一个开放的集成平台,为开发人员、用户和 Web生产商提供了一个快速而简便的在 Internet 和 Intranet 创建程序集成和内容的方法。 使用 ActiveX, 可轻松方便的在 Web页中插入 多媒体效果、 交互式对象、以及复杂程序,创建用户体验相当的高质量多媒体 CD-ROM 。
简单的说,用activeX和js差不多,但是有些是js无法实现的,这个时候就可以考虑一下activeX,一般要求是在客户端执行的程序,比如对本机的串口操作等,下面来简单介绍下如何用C#自己开发一个activeX组件,并在web中应用
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; namespace ActivexDemo { [ComImport, GuidAttribute("CB5BDC81-93C1-11CF-8F20-00805F2CD064")] [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)] public interface IObjectSafety { [PreserveSig] int GetInterfaceSafetyOptions(ref Guid riid, [MarshalAs(UnmanagedType.U4)] ref int pdwSupportedOptions, [MarshalAs(UnmanagedType.U4)] ref int pdwEnabledOptions); [PreserveSig()] int SetInterfaceSafetyOptions(ref Guid riid, [MarshalAs(UnmanagedType.U4)] int dwOptionSetMask, [MarshalAs(UnmanagedType.U4)] int dwEnabledOptions); } 然后在代码中实现接口即可 using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices;//Guid引用 namespace ActivexDemo { [Guid("0b6ed426-9e67-4cf3-99da-8a346a98e5c6")] public partial class Uc : UserControl,IObjectSafety { public Uc() { InitializeComponent(); } #region IObjectSafety 成员 private const string _IID_IDispatch = "{00020400-0000-0000-C000-000000000046}"; private const string _IID_IDispatchEx = "{a6ef9860-c720-11d0-9337-00a0c90dcaa9}"; private const string _IID_IPersistStorage = "{0000010A-0000-0000-C000-000000000046}"; private const string _IID_IPersistStream = "{00000109-0000-0000-C000-000000000046}"; private const string _IID_IPersistPropertyBag = "{37D84F60-42CB-11CE-8135-00AA004BB851}"; private const int INTERFACESAFE_FOR_UNTRUSTED_CALLER = 0x00000001; private const int INTERFACESAFE_FOR_UNTRUSTED_DATA = 0x00000002; private const int S_OK = 0; private const int E_FAIL = unchecked((int)0x80004005); private const int E_NOINTERFACE = unchecked((int)0x80004002); private bool _fSafeForScripting = true; private bool _fSafeForInitializing = true; public int GetInterfaceSafetyOptions(ref Guid riid, ref int pdwSupportedOptions, ref int pdwEnabledOptions) { int Rslt = E_FAIL; string strGUID = riid.ToString("B"); pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA; switch (strGUID) { case _IID_IDispatch: case _IID_IDispatchEx: Rslt = S_OK; pdwEnabledOptions = 0; if (_fSafeForScripting == true) pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER; break; case _IID_IPersistStorage: case _IID_IPersistStream: case _IID_IPersistPropertyBag: Rslt = S_OK; pdwEnabledOptions = 0; if (_fSafeForInitializing == true) pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA; break; default: Rslt = E_NOINTERFACE; break; } return Rslt; } public int SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int dwEnabledOptions) { int Rslt = E_FAIL; string strGUID = riid.ToString("B"); switch (strGUID) { case _IID_IDispatch: case _IID_IDispatchEx: if (((dwEnabledOptions & dwOptionSetMask) == INTERFACESAFE_FOR_UNTRUSTED_CALLER) && (_fSafeForScripting == true)) Rslt = S_OK; break; case _IID_IPersistStorage: case _IID_IPersistStream: case _IID_IPersistPropertyBag: if (((dwEnabledOptions & dwOptionSetMask) == INTERFACESAFE_FOR_UNTRUSTED_DATA) && (_fSafeForInitializing == true)) Rslt = S_OK; break; default: Rslt = E_NOINTERFACE; break; } return Rslt; } #endregion } }
View Code
这样ActiveX的基本设置完成了,这个时候我们就可以利用ActiveX实现自己需要的功能了,我们两个举个例子
一、 在页面中放置一个文本框和一个按钮,当点击按钮的时候,把ActiveX中的值传到页面的文本框中
二、 在ActiveX的控件中放置一个按钮,当触发按钮事件后,把ActiveX的值传到页面的文本框中
上面这两个例子其实就是想实现如何实现js调用ActiveX和ActiveX调用页面的Js的,现在我们写一下代码,先看看第一种情况,在ActiveX中我们写了一个GetStr方法,用来返回一个值,然后把这个值传到页面中
public string GetStr() { return "这是从ActiveX传过来的值"; }
好了,现在我们需要新建一个安装文件
1、 新建一个安装项目
![](https://images.cnblogs.com/cnblogs_com/shuang121/a5.jpg)
2、 在项目上点右键,【添加】->【项目输出】,选择上边的项目
![](https://images.cnblogs.com/cnblogs_com/shuang121/a6.jpg)
如果有多个项目,可以选择需要安装的项目就可以了
点击项目,按下“F4”,设置属性
![](https://images.cnblogs.com/cnblogs_com/shuang121/a7.jpg)
3、 生成项目
会生成两个文件,一个exe文件和一个msi文件
![](https://images.cnblogs.com/cnblogs_com/shuang121/a8.jpg)
然后把这两个文件拷到项目的lib文件夹下面
1、 新建一个页面,添加下面的代码
<object classid="clsid:0b6ed426-9e67-4cf3-99da-8a346a98e5c6" codebase="lib/setup.exe" width="200" height="40" id="helloBossma"> </object>
然后在页面中添加javascript代码,按钮和文本框
<head runat="server"> <title>无标题页</title> <script type="text/javascript"> function passValue() { document.getElementById("textValue").value=document.getElementById("helloBossma").GetStr(); } </script> </head> <body> <object classid="clsid:0b6ed426-9e67-4cf3-99da-8a346a98e5c6" codebase="lib/setup.exe" width="200" height="40" id="helloBossma"> </object> <form id="form1" runat="server"> <div> <input id="textValue" type="text" /> <input id="Button1" type="button" value="传值" onclick="passValue()"/> </div> </form> </body> </html>
这个时候我们就可以打开浏览器浏览页面了
![](https://images.cnblogs.com/cnblogs_com/shuang121/a9.jpg)
运行加载提示的加载项就可以了
![](https://images.cnblogs.com/cnblogs_com/shuang121/a10.jpg)
这个时候我们点击按钮传值就可以把ActiveX中的值传过来了
![](https://images.cnblogs.com/cnblogs_com/shuang121/a11.jpg)
接着看看如何用ActiveX调用Javascript
首先在ActiveX中写一个方法用来将页面的js函数传过来。
页面的js函数
window.onload=function() { document.getElementById("helloBossma").regJs(window,"show"); } function show(str) { document.getElementById("text1").value=str; }
ActiveX中我们需要添加引用
![](https://images.cnblogs.com/cnblogs_com/shuang121/a12.jpg)
using mshtml; private IHTMLWindow2 temphtml = null; private string functionstr = ""; public void RegJs(object win, string fuc) { temphtml = (IHTMLWindow2)win; if (temphtml != null && !string.IsNullOrEmpty(fuc)) { functionstr = fuc; } else { temphtml = null; functionstr = ""; MessageBox.Show("注册脚本失败!"); } } 在控件中放置一个按钮,触发按钮的onclick事件,在事件中将值传给页面的js,从而完成赋值 private void button1_Click(object sender, EventArgs e) { temphtml.execScript(functionstr + "('从activeX中传值到页面')", "JScript"); }
上面是activeX的两个简单应用。
相关文章推荐
- 使用C#开发ActiveX控件
- 使用C#开发ActiveX控件
- 使用VS2010 C#开发ActiveX控件(上)
- 使用C#开发ActiveX控件
- 使用C#开发ActiveX控件
- 使用C#开发ActiveX控件全攻略(1)
- 使用C#开发ActiveX控件
- 使用VS2010 C#开发ActiveX控件(上)
- 使用VS2010 C#开发ActiveX控件(下),完整代码打包下载
- 使用C#开发ActiveX控件 并制作Cab包
- [转]使用C#开发ActiveX控件全攻略
- 使用C#开发ActiveX控件全攻略(2)
- 使用C#开发ActiveX控件全攻略(1)
- 使用VS2010 C#开发ActiveX控件(下),完整代码下载
- 使用C#开发ActiveX控件(转)
- [转]使用C#开发ActiveX控件全攻略
- 使用C#开发ActiveX控件全攻略
- 使用VS2010 C#开发ActiveX控件(上)
- (转)使用C#开发ActiveX控件
- 使用VS2010 C#开发ActiveX控件(下),完整代码下载