Create an ActiveX using a Csharp Usercontrol

http://www.codeguru.com/csharp/.net/net_general/comcom/article.php/c16257
Create an ActiveX using a Csharp Usercontrol
Rating: none

Andreas Verhamme (view profile)
July 20, 2009

Environment:  Visual Studio 2005 C#

This article is about creating ActiveX controls using a DotNet Usercontrol in Csharp. You can design all ActiveX features like: properties, methods and events.


(continued)
Environment:  Visual Studio(2005) Csharp

How to Do This

1. Create a Usercontrol using Visual Studio (C#):


(

Full Size Image)

2. Configure the project properties:


(Full Size Image)

3. Modify the usercontrol interface:

Note : Make sur you added the EVENTS class:   [ClassInterface(ClassInterfaceType.AutoDual), 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. Add the register/unregister section in the source code

// 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. Add an ActiveX property:

// ActiveX properties (Get/Set) //

private int ptextVal;
public int TextVal
{
       get
       {
                  ptextVal = (int)(numericUpDown1.Value);
                  return ptextVal;
        }
        set
       {

            ptextVal = value;
                numericUpDown1.Value = ptextVal;
         }
}

 

 

 

6. Add an ActiveX method:

// 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. Add an ActiveX event:

// 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. Register the ActiveX on your PC:

Register the new ActiveX on your computer using the command:  RegAsm.exe CsharpWindowsActiveX.dll

9. Test your ActiveX:

Use the TSTCon32.exe tool from Visul Studio to test the ActiveX:


(Full Size Image)


(Full Size Image)

 

Downloads

CsharpWindowsActiveX_demo.zip - demo project
CsharpWindowsActiveX_src.zip - source code

转载于:https://www.cnblogs.com/jannock/archive/2009/07/29/1534220.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值