Importing and extending ActiveX controls in. net

Source: Internet
Author: User

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

    • Download source files-62.4 KB
Introduction

We often need to reuse and extend an ActiveX control developed by other vendors in real. NET applications. there are articles and user privileges ents 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. NET environment, especially with the C # code generated by the utilityAximp.exe.

In this article, we present step-by-step instructions on how to import and extend an ActiveX control in. 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 "mimize 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 typeAxmsacal. axcalendar. It will also generateAxinterop. msacal. dllAndInterOP. msacal. dllInBin/debug(By default) directory.

To customize the calendar control, you useAxmsacal. axcalendarAs your base class and override certain methods inAxmsacal. axcalendarAnd 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 buildMycalendarInto 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,ToolboxitemAttribute is setFalseIn the generatedAxWrapper.FalseValue 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 classAxcalendarDoes 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 calledAximp.exeThat supports ActiveX importing and source code generation.

Importing ActiveX Control Using aximp.exe

To import the calendar Control UsingAximp.exeWith 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 addAxmsacal. CSInto the project. You need to copyAxmsacal. CS msacal. dllInto the project directory, we don't needAxmsacal. dllSince we will build a customized DLL of our own.

By default, aximp generatesAxmsacal. CSLike 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; // Other stuff ommited }}

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

    1. AddMsacal. dllAndStdole. dllAs references.
    2. Comment out[Assembly: assemblyversion ()]InAxmsacal. CS.
    3. Add attribute[System. componentmodel. toolboxitem (True)]Before the classAxcalendar.
      [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 calledMycalendarExtendingAxcalendar.

    <SPAN class = "CS-keyword"> Public </span> <SPAN class = "CS-keyword"> class </span> mycalendar: axcalendar {}
  2. Modify the base class 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 subclassMycalendarAnd 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.