用C#開發(fā)ActiveX控件,并使用web調(diào)用
入職差不多兩個(gè)月了,由學(xué)生慢慢向職場人做轉(zhuǎn)變,也慢慢的積累知識(shí),不斷的更新自己。最近的一個(gè)項(xiàng)目里邊,涉及到的一些問題,因?yàn)镾DK提供的只是winform才能使用了,但是有需求咱們必須得完成啊,所以涉及到的ActiveX控件開發(fā)并用web來顯示的,正好也總結(jié)一些,之前在學(xué)校一直沒有接觸過,網(wǎng)上是有教程的,但是大多有問題,只有自己親自測試通過了才放心。
一、開發(fā)ActiveX控件
1、新建類庫,命名類庫名稱“user.cs”;
2、在類庫中添加自定義用戶控件“ UserControl1”,實(shí)現(xiàn)各種自定義功能;
3、為了解決瀏覽器安全設(shè)置對控件的影響,必須在組件中加入IObjectSafety接口,所以再添加一個(gè)接口類“IObjectSafety.cs”
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Web.UI.WebControls.WebParts; //必須引用該包
- using System.Security;
- using System.Runtime.InteropServices; //必須引用該包
- namespace user
- {
- [Guid("CB5BDC81-93C1-11CF-8F20-00805F2CD064"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] //GUID這個(gè)不需修改,固定的
- public interface IObjectSafety
- {
- // 方法定義
- void GetInterfacceSafyOptions(System.Int32 riid, out System.Int32 pdwSupportedOptions, out System.Int32 pdwEnabledOptions);
- void SetInterfaceSafetyOptions(System.Int32 riid, System.Int32 dwOptionsSetMask, System.Int32 dwEnabledOptions);
- }
- }
4、繼承接口
- public partial class UserControl1: UserControl,IObjectSafety
- {
- public UserControl1()
- {
- InitializeComponent();
- }
- public void GetInterfacceSafyOptions(System.Int32 riid, out System.Int32 pdwSupportedOptions, out System.Int32 pdwEnabledOptions)
- {
- pdwSupportedOptions = 1; //不要修改該代碼
- pdwEnabledOptions = 2; //不要修改該代碼
- return;
- }
- public void SetInterfaceSafetyOptions(System.Int32 riid, System.Int32 dwOptionsSetMask, System.Int32 dwEnabledOptions)
- {
- return;
- }
- public void YourFunc(){}
- }
5、在UserControl1引入兩個(gè)命名空間
using System.Security;
using System.Runtime.InteropServices;
6、工具——創(chuàng)建GUID——新建GUID——選擇第五項(xiàng)——復(fù)制,就可以關(guān)閉小窗口,然后在命名空間下粘貼,如下
- namespace user
- {
- [Guid("7F29ACED-AD84-4EEE-9E1A-58BE255F9EF7")] //這個(gè)GUID是web引用的時(shí)候用到的
- public partial class UserControl1: UserControl,IObjectSafety
- {
- ……
7、***一步,項(xiàng)目——user屬性(***一項(xiàng)),兩處需要修改
①應(yīng)用程序——程序集信息——√ 使程序集COM可見
②生成——√ 為COM互操作注冊
8、即可右鍵項(xiàng)目user——生成
二、web使用ActiveX控件
在web調(diào)用很簡潔,引用剛剛生成的dll文件,然后在添加
- <body>
- <form id="form1" runat="server">
- <div >
- <object id="VisioDisPlay"
- classid="clsid:7F29ACED-AD84-4EEE-9E1A-58BE255F9EF7"
- width="1250"
- height="600"
- >
- </object>
- </div>
- </form>
即可完成。注意的是:這里的classid里邊的字符串里邊有 “ clsid: ”,別忘啦,后面接的是第六步生成的GUID,必須一致!