[译]使用C#的用户控件创建ActiveX

文章:Create an ActiveX using a Csharp Usercontrol
来源:http://www.codeguru.com/csharp/.net/net_general/comcom/article.php/c16257
原著作者:Andreas Verhamme
翻译:Dezai

这篇文章主要是介绍如何在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









上一篇: 我深深感觉到未来的压力
下一篇: Oracle EBS:Form Builder 中 LOV的建立
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: 创建ActiveX
相关日志:
评论: 0 | 引用: 0 | 查看次数: 7418
发表评论
昵 称:
密 码: 游客发言不需要密码.
邮 箱: 邮件地址支持Gravatar头像,邮箱地址不会公开.
网 址: 输入网址便于回访.
内 容:
验证码:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 30 字 | UBB代码 关闭 | [img]标签 关闭