您的位置:首页 > 编程语言 > C#

使用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



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: