使用C#的用户控件创建ActiveX
2009-10-09 19:01
597 查看
文章:Create an ActiveX using a Csharp Usercontrol
来源:http://www.codeguru.com/csharp/.net/net_general/comcom/article.php/c16257
原著作者:Andreas Verhamme
翻译:Dezai
http://www.dezai.cn/blog/article.asp?id=269
这篇文章主要是介绍如何在C#中如何使用DotNet的用户控件来创建ActiveX控件.你可以设计ActiveX的相关属性,方法和事件.
开发环境:Visual Studio2005
1.使用Visual Studio创建一个用户控件
2.设置程序集Assebmly的相关属性
3.修改用户控件的接口设计
注意:务必确认增加下面这个事件类
程序代码
ComSourceInterfaces(typeof(UserControlEvents))]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
// Add references
using System.Runtime.InteropServices;
using System.Reflection;
using Microsoft.Win32;
namespace CsharpWindowsActiveX
{
[ProgId("CsharpWindowsActiveX.ActiveXUserControl")]
[ClassInterface(ClassInterfaceType.AutoDual), ComSourceInterfaces(typeof(UserControlEvents))]
public partial class ActiveXUserControl : UserControl
{
public ActiveXUserControl()
{
InitializeComponent();
}
.....
4..在源码中增加注册/注销功能部分的代码
程序代码
// register COM ActiveX object
[ComRegisterFunction()]
public static void RegisterClass(string key)
{
StringBuilder skey = new StringBuilder(key);
skey.Replace(@"HKEY_CLASSES_ROOT/", "");
RegistryKey regKey = Registry.ClassesRoot.OpenSubKey(skey.ToString(), true);
RegistryKey ctrl = regKey.CreateSubKey("Control");
ctrl.Close();
RegistryKey inprocServer32 = regKey.OpenSubKey("InprocServer32", true);
inprocServer32.SetValue("CodeBase", Assembly.GetExecutingAssembly().CodeBase);
inprocServer32.Close();
regKey.Close();
}
[ComUnregisterFunction()]
public static void UnregisterClass(string key)
{
StringBuilder skey = new StringBuilder(key);
skey.Replace(@"HKEY_CLASSES_ROOT/", "");
RegistryKey regKey = Registry.ClassesRoot.OpenSubKey(skey.ToString(), true);
regKey.DeleteSubKey("Control", false);
RegistryKey inprocServer32 = regKey.OpenSubKey("InprocServer32", true);
regKey.DeleteSubKey("CodeBase", false);
regKey.Close();
}
5.增加一个ActiveX属性
程序代码
/ ActiveX properties (Get/Set) //////////////////////////////////////////////////////////////////
private int ptextVal;
public int TextVal
{
get
{
ptextVal = (int)(numericUpDown1.Value);
return ptextVal;
}
set
{
ptextVal = value;
numericUpDown1.Value = ptextVal;
}
}
6.增加一个ActiveX方法
程序代码
/ ActiveX methods/functions //////////////////////////////////////////////////////////////////
public interface ICOMCallable
{
int GetTextBoxValue();
}
public int GetTextBoxValue()
{
int i = (int)(numericUpDown1.Value);
MessageBox.Show("ActiveX method: GetTextBoxValue " + i.ToString());
return (i);
}
7.增加一个ActiveX事件
程序代码
// Eventhandler interface //////////////////////////////////////////////////////////////////
public delegate void ControlEventHandler(int NumVal);
[Guid("0A415E38-372F-45fb-813B-D9558C787EA0")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface UserControlEvents
{
[DispId(0x60020001)]
void OnButtonClick(int NumVal);
}
public event ControlEventHandler OnButtonClick;
private void buttonOK_Click(object sender, EventArgs e)
{
int NumVal;
if (OnButtonClick != null)
{
NumVal = (int)(numericUpDown1.Value);
OnButtonClick(NumVal);
}
}
8.在PC机上注册这个ActiveX
使用RegAsm.exe CsharpWindowsActiveX.dll命令在你的电脑上注册这个新的ActiveX控件
9.测试你的ActiveX
使用TSTCon32.exe tool在Visul Studio 中测试ActiveX
来源:http://www.codeguru.com/csharp/.net/net_general/comcom/article.php/c16257
原著作者:Andreas Verhamme
翻译:Dezai
http://www.dezai.cn/blog/article.asp?id=269
这篇文章主要是介绍如何在C#中如何使用DotNet的用户控件来创建ActiveX控件.你可以设计ActiveX的相关属性,方法和事件.
开发环境:Visual Studio2005
1.使用Visual Studio创建一个用户控件
2.设置程序集Assebmly的相关属性
3.修改用户控件的接口设计
注意:务必确认增加下面这个事件类
程序代码
ComSourceInterfaces(typeof(UserControlEvents))]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
// Add references
using System.Runtime.InteropServices;
using System.Reflection;
using Microsoft.Win32;
namespace CsharpWindowsActiveX
{
[ProgId("CsharpWindowsActiveX.ActiveXUserControl")]
[ClassInterface(ClassInterfaceType.AutoDual), ComSourceInterfaces(typeof(UserControlEvents))]
public partial class ActiveXUserControl : UserControl
{
public ActiveXUserControl()
{
InitializeComponent();
}
.....
4..在源码中增加注册/注销功能部分的代码
程序代码
// register COM ActiveX object
[ComRegisterFunction()]
public static void RegisterClass(string key)
{
StringBuilder skey = new StringBuilder(key);
skey.Replace(@"HKEY_CLASSES_ROOT/", "");
RegistryKey regKey = Registry.ClassesRoot.OpenSubKey(skey.ToString(), true);
RegistryKey ctrl = regKey.CreateSubKey("Control");
ctrl.Close();
RegistryKey inprocServer32 = regKey.OpenSubKey("InprocServer32", true);
inprocServer32.SetValue("CodeBase", Assembly.GetExecutingAssembly().CodeBase);
inprocServer32.Close();
regKey.Close();
}
[ComUnregisterFunction()]
public static void UnregisterClass(string key)
{
StringBuilder skey = new StringBuilder(key);
skey.Replace(@"HKEY_CLASSES_ROOT/", "");
RegistryKey regKey = Registry.ClassesRoot.OpenSubKey(skey.ToString(), true);
regKey.DeleteSubKey("Control", false);
RegistryKey inprocServer32 = regKey.OpenSubKey("InprocServer32", true);
regKey.DeleteSubKey("CodeBase", false);
regKey.Close();
}
5.增加一个ActiveX属性
程序代码
/ ActiveX properties (Get/Set) //////////////////////////////////////////////////////////////////
private int ptextVal;
public int TextVal
{
get
{
ptextVal = (int)(numericUpDown1.Value);
return ptextVal;
}
set
{
ptextVal = value;
numericUpDown1.Value = ptextVal;
}
}
6.增加一个ActiveX方法
程序代码
/ ActiveX methods/functions //////////////////////////////////////////////////////////////////
public interface ICOMCallable
{
int GetTextBoxValue();
}
public int GetTextBoxValue()
{
int i = (int)(numericUpDown1.Value);
MessageBox.Show("ActiveX method: GetTextBoxValue " + i.ToString());
return (i);
}
7.增加一个ActiveX事件
程序代码
// Eventhandler interface //////////////////////////////////////////////////////////////////
public delegate void ControlEventHandler(int NumVal);
[Guid("0A415E38-372F-45fb-813B-D9558C787EA0")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface UserControlEvents
{
[DispId(0x60020001)]
void OnButtonClick(int NumVal);
}
public event ControlEventHandler OnButtonClick;
private void buttonOK_Click(object sender, EventArgs e)
{
int NumVal;
if (OnButtonClick != null)
{
NumVal = (int)(numericUpDown1.Value);
OnButtonClick(NumVal);
}
}
8.在PC机上注册这个ActiveX
使用RegAsm.exe CsharpWindowsActiveX.dll命令在你的电脑上注册这个新的ActiveX控件
9.测试你的ActiveX
使用TSTCon32.exe tool在Visul Studio 中测试ActiveX
相关文章推荐
- [译]使用C#的用户控件创建ActiveX
- 技巧/诀窍: 创建和使用Silverlight和WPF 用户控件
- ASP.NET 用户控件的创建和使用
- C# 用户自定义UserControl控件使用
- 使用C#反射实现用户控件调用父页面方法
- c#创建透明背景用户控件
- VS2003 C#:重写键盘响应事件、动态创建控件、创建线程、在线程中使用委托在界面显示数据
- 使用VS2010开发ActiveX(MFC)控件(1)——创建
- C#用户控件的创建
- 使用VS2010开发ActiveX(MFC)控件(1)——创建
- 关于VS2008中,C#创建ASP.NET Web应用程序无法添加和使用web控件
- 技巧/诀窍: 创建和使用Silverlight和WPF 用户控件
- C# 中用户控件中添加事件和使用
- C#用户控件的创建
- 英语阅读推荐:创建Silverlight用户控件 & A*路径搜索中使用二叉堆
- 使用C#反射实现用户控件调用父页面方法
- 使用c#创建windows本地用户帐号
- Visual Studio C# 中使用用户控件
- C#的Winform中使用用户控件
- C#下使用Winform用户控件,Host vtkRenderWindow,来实现vtk与winform集成