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. axcalendar
As your base class and override certain methods inAxmsacal. 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 buildMycalendar
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,Toolboxitem
Attribute is setFalse
In the generatedAx
Wrapper.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 classAxcalendar
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 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:
- AddMsacal. dllAndStdole. dllAs references.
- Comment out
[Assembly: assemblyversion ()]
InAxmsacal. CS.
- 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 {} |
- 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:
- Create a new class called
Mycalendar
ExtendingAxcalendar
.
<SPAN class = "CS-keyword"> Public </span> <SPAN class = "CS-keyword"> class </span> mycalendar: axcalendar {} |
- 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>} |
-
- 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