Importing and Extending ActiveX Controls in .NET

转:http://www.codeproject.com/csharp/importactivex.asp

Introduction

We often need to reuse and extend an ActiveX control developed by other vendors in real .NET applications. There are articles and user documents on how to import an ActiveX control into Visual Studio .NET, but we don’t see much on how to customize an imported ActiveX control in the .NET environment, especially with the C# code generated by the utility AxImp.exe.

In this article, we present step-by-step instructions on how to import and extend an ActiveX control in the .NET environment. We use Microsoft Calendar Control as our example. The sample code shows a customized Calendar Control that allows user to create daily note for each day; when a day is selected, a note will show in a box like a tooltip.

Importing ActiveX Control in Visual Studio .NET

Importing an ActiveX control inside Visual Studio .NET is easy, you can just simply place your mouse cursor inside Toolbox window, right click the mouse, then you see the “Customize Toolbox” window, in this window, select the “COM components” tab, then select the ActiveX control you want to import. Here is the window with a selection of Calendar Control:

For the imported Calendar Control, Studio will create a .NET “wrapper” with a type of AXMSACAL.AxCalendar. It will also generate AxInterop.MSACAL.dll and Interop.MSACAL.dll in bin/Debug (by default) directory.

To customize the Calendar Control, you use AXMSACAL.AxCalendar as your base class and override certain methods in AXMSACAL.AxCalendar and add your own methods:

public class MyCalendar : AXMSACAL.AxCalendar
{
//customize it here
}

You build MyCalendar into a WinForm control, add it into Visual Studio .NET toolbox, you are confident it will work, but you end up with the following error message:

This problem is due to: “by default, the ToolboxItem attribute is set to false in the generated Ax wrapper. A false value for this attribute prevents the control or wrapper from being added to the Toolbox.” (See this link.)

When we try to customize the Calendar Control in this way, we run into lots of problems mainly due to the fact that the .NET generated class AxCalendar does not expose its members in a level that its subclass can access. The reason is probably that Visual Studio .NET expects you to import and use an existing ActiveX control, but not extend an ActiveX control. Fortunately, Visual Studio .NET 2003 provides a utility called AxImp.exe that supports ActiveX importing and source code generation.

Importing ActiveX Control using AxImp.exe

To import the Calendar Control using AxImp.exe with source code generation, you can use a command like:

AxImp /source activex_control_path_name

The following screen shows the importing of Calendar Control in command line and its output:

As you can see from the above screen, two DLLs and a C# source code are generated.

The next step is to create a Windows Control Library project with Visual Studio .NET and add AxMSACAL.cs into the project. You need to copy the AxMSACAL.cs MSACAL.dll into the project directory, we don’t need the AxMSACAL.dll since we will build a customized DLL of our own.

By default, AxImp generates AxMSACAL.cs like this:

Collapse
[assembly: System.Reflection.AssemblyVersion("7.0.0.0")]
[assembly:
System.Windows.Forms.AxHost.TypeLibraryTimeStamp("6/26/1998 12:00:00 AM")]
namespace AxMSACAL {
[System.Windows.Forms.AxHost.ClsidAttribute(
"{8e27c92b-1264-101c-8a2f-040224009c02}")]
[System.ComponentModel.DesignTimeVisibleAttribute(true)]
[System.ComponentModel.DefaultEvent("AfterUpdate")]
[System.ComponentModel.DefaultProperty("_Value")]
public class AxCalendar : System.Windows.Forms.AxHost {
private MSACAL.ICalendar ocx;
private AxCalendarEventMulticaster eventMulticaster;
private System.Windows.Forms.AxHost.ConnectionPointCookie cookie;
// other stuff ommited
}
}

In order to build this control into a DLL, you need to do the following:

  1. Add MSACAL.dll and Stdole.dll as references.
  2. Comment out the [assembly: AssemblyVersion()] in AxMSACAL.cs.
  3. Add attribute [System.ComponentModel.ToolboxItem(true)] before the class AxCalendar.
    [System.ComponentModel.ToolboxItem(true)]
        public class AxCalendar : System.Windows.Forms.AxHost
        {
        }
  4. Change the default namespace “AxMSACAL” into whatever namespace your project defines.

Extending ActiveX Control

To customize the Calendar Control, you need to do the following:

  1. Create a new class called MyCalendar extending AxCalendar.
          public class MyCalendar : AxCalendar
        {
        }
  2. Modify the base class of AxCalendar, if necessary, to make subclass MyCalendar easier to implement. For the Calendar Control, we have to at least change certain members’ visibility modifier in AxCalender from private to protected so subclass can access them:
    public class AxCalendar : System.Windows.Forms.AxHost
        {
        protected MSACAL.ICalendar ocx;
        protected AxCalendarEventMulticaster eventMulticaster;
        protected System.Windows.Forms.AxHost.ConnectionPointCookie cookie;
        // other stuff
        }
  3. Implement subclass MyCalendar and add your own stuff.

Summary

In this article, we showed how to use AxImp to import an ActiveX control with C# code generation, then customize the control with the generated code and extend it.

Colin Peng

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值