全局搜索结果(2)
-
- 经验分享c#编写activeX控件详细教程
- 2021/4/21 14:27:50
- vs2012
- 1、新建windows窗体控件库,命名为 MyActiveXDemo,打开Properties/AssemblyInfo.cs,修改为如下:2、打开工具箱,在自动生成的UserControl1.cs设计视图中添加一个button,如下图:3、双击button1,为按钮添加方法,这里演示只添加了一个弹出消息,如下private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("ActiveX Message.");
}4、创建接口IObjectSafety.csusing System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace MyActiveXDemo
{
[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);
}
}
5、修改UserControl1.cs,使其继承接口IObjectSafety,完整的UserControl1.cs如下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;
namespace MyActiveXDemo
{
[Guid("73D3BE6A-E3B3-46F7-AFFE-8673390DA3F6"), ProgId("ActiveXDemo.UserControl1"), ComVisible(true)]
public partial class UserControl1 : UserControl, IObjectSafety
{
public UserControl1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("ActiveX Message.");
}
#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
}
}
6、右键项目MyActiveXDemo属性, a、应用程序=》点击程序集信息=》勾选“使程序集COM可见” b、生成=》勾选“为COM互操作注册”7、解决方案右键=》添加=》其它项目类型=》安装和部署,选中“InstallShield Limited Edition Project”,命名为“ActiveXSetup”,确定8、点击application files=》My Product Name=》右下角点击add Product Outputs,在弹出框内勾选“主输出”,确定,如下图9、右键ActiveXSetup,生成,成功后在ActiveXSetup\Express\DVD-5\DiskImages\DISK1下可以看到ActiveXSetup.msi安装程序,到这里就成功了一大半了。10、网上下载一个cabarc.exe,放在与ActiveXSetup.msi安装文件相同目录,下载地址:https://sqdownd.rbread05.cn/down/42010_20161130174135.rar,在相同目录创建一个install.inf文件,文件内容如下:[version]
signature="$CHICAGO$"
AdvancedINF=2.0
[Setup Hooks]
hook1=hook1
[hook1]
run=msiexec.exe /i "%EXTRACT_DIR%\ActiveXSetup.msi" /qn11、同时在同目录创建一个build.bat,内容如下:"cabarc.exe" n test.cab ActiveXSetup.msi install.inf 双击build.bat,执行完毕后将会生成一个test.cab文件,到此ActiceX控件已经打包好了。如何在浏览器上加载ActiveX控件?请看下一篇:asp.net 如何在网页上加载ActiveX控件
- 评论:0 / 阅读:1079
-
- 经验分享asp.net 如何在网页上加载ActiveX控件
- 2021/4/21 15:49:02
- ...45e64ae04228b5f" rel="nofollow">c#编写activeX控件详细教程3、在body中插入以下内容,<object id="mytt" classid="clsid:73D3BE6A-E3B3-46F7-AFFE-8673390DA3F6" codebase="/test.cab"></object>如下图(注意classid一定要是前面UserControl1.cs定义的guid):4、运行(注意要用IE浏览器),弹出的插件提示,点允许即可,出现如下界面表示加载ok了。点击按钮button1,自然也会弹出控件对应的消息提示框了,如下至此,就是完整的.net编写ActiveX控件以及web加载ActiveX的教程了。
- 评论:0 / 阅读:1206