现在的位置: 首页 > 综合 > 正文

Importing and Extending ActiveX Controls in .NET

2012年09月12日 ⁄ 综合 ⁄ 共 4508字 ⁄ 字号 评论关闭

转: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:

<span class="cs-keyword">public</span> <span class="cs-keyword">class</span> MyCalendar : AXMSACAL.AxCalendar
{
<span class="cs-comment">//customize it here</span>
}

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(<span class="cs-keyword">true</span>)]
        <span class="cs-keyword">public</span> <span class="cs-keyword">class</span> 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.

          <span class="cs-keyword">public</span> <span class="cs-keyword">class</span> 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:
    <span class="cs-keyword">public</span> <span class="cs-keyword">class</span> AxCalendar : System.Windows.Forms.AxHost
        {
        <span class="cs-keyword">protected</span> MSACAL.ICalendar ocx;
        <span class="cs-keyword">protected</span> AxCalendarEventMulticaster eventMulticaster;
        <span class="cs-keyword">protected</span> System.Windows.Forms.AxHost.ConnectionPointCookie cookie;
        <span class="cs-comment">// other stuff</span>
        }
  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

抱歉!评论已关闭.