Exposing Windows Forms controls as ActiveX controls

Source: Internet
Author: User
ArticleDirectory
    • Inserting the control
    • Testing Methods
Introduction
    • Download Demo project-15 KB

This article will describe how to utilise Windows Forms controls outside. net. in a recent msdn magazine article on. net InterOP available here, various ways of exposing. net objects to 'legacy 'environments are discussed, including the exposure of Windows Forms controls as ActiveX controls.

The problem is that the goalposts have moved since the article was written as beta 2 is now available, and unfortunately this support has been removed-see this posting on the. NET list at http://discuss.develop.com.

The following image shows a control, written purely within. net, hosted within an ActiveX control container-in this instanceTstcon32.exe.

As beta1 supported this facility, and being somewhat inquisitive, I decided to see if I cocould find a way to expose controls anyway. the attached project creates the 'prisoner' control, which won't set the world on fire but does show the main things you need to do in order to get. net Control up & running within VB6.

Caveat:As this support has been dropped from beta2 of. net, don't blame me if it fries your PC or toasts the cat.

Now that's out of the way, how's it done ?.

Writing the control
  1. Create a new control project from within Visual Studio-my examples are all in C # But VB. NET cocould also be used.

     

  2. Add controls etc to the form, put in the code etc.

     

  3. Add in the following using Clauses...

      using  system. runtime. interopservices;  using  system. text;  using  system. reflection;  using  Microsoft. win32; 
  4. attribute your class so that it gets a progid. This isn't strictly necessary as one will be generated, but it's almost always best to be explicit.
     [progid (" prisoner. prisonercontrol ")] [classinterface (classinterfacetype. autodual)] 

    This assigns the progid, and also defines that the interface exposed shoshould be 'autodual'-This Crufts up a default Interface for you from all public, non-static members of the class. if this isn' t what you want, use one of the other options.

  5. Update the project properties so that your assembly is registered for com InterOP.

     

    If you're using VB. net, you also need a strong named assembly. curiously in C # You don't-and it seems to be a feature of the environment rather than a feature of the compiler or CLR.

  6. Add the following two methods into your class.

     

    Collapse
    [Comregisterfunction ()] Public   Static   Void Registerclass ( String Key ){ // Strip off hkey_classes_root \ from the passed key as I don't need it Stringbuilder sb =New Stringbuilder (key); sb. Replace (@ "Hkey_classes_root \" , "" ); // Open the CLSID \ {guid} key for write access Registrykey K = registry. classesroot. opensubkey (sb. tostring (), True ); // And create the 'control' key-This allows it to show up in      // The ActiveX Control container Registrykey CTRL = K. createsubkey ( "Control" ); Ctrl. Close (); // Next create the codebase entry-needed if not string named and gacced. Registrykey inprocserver32 = K. opensubkey ( "Inprocserver32" , True ); Inprocserver32.setvalue ( "Codebase" , Assembly. getexecutingassembly (). codebase); inprocserver32.close (); // Finally close the main key K. Close ();}

    The registerclass function is attributed with comregisterfunction-This static method will be called when the Assembly is registered for com InterOP. All I do here is add'Control'Keyword to the Registry, plus add in the codebase entry.

    codebase is interesting-not only. net controls. it defines a URL path to where the code can be found, which cocould be an assembly on disk as in this instance, or a remote assembly on a Web server somewhere. when the runtime attempts to create the control, it will probe this URL and download the control as necessary. this is very useful when testing. NET components, as the usual caveat of residing in the same directory (ETC) as. EXE does not apply.

    [Comunregisterfunction ()] <SPAN class = "CS-keyword"> Public </span> <SPAN class = "CS-keyword"> static </span> <SPAN class = "CS-keyword"> void </span> unregisterclass (<SPAN class = "CS-keyword"> string </span> key) {stringbuilder sb = <SPAN class = "CS-keyword"> New </span> stringbuilder (key); sb. replace (@ <SPAN class = "CS-string"> "hkey_classes_root \" </span>, <SPAN class = "CS-string"> "" </span> ); <SPAN class = "CS-comment"> // open hkcr \ CLSID \ {guid} For write access </span> registrykey K = registry. classesroot. opensubkey (sb. tostring (), <SPAN class = "CS-keyword"> true </span>); <SPAN class = "CS-comment"> // Delete the 'control' key, but don't throw an exception if it does not exist </span> K. deletesubkey (<SPAN class = "CS-string"> "control" </span>, <SPAN class = "CS-keyword"> false </span> ); <SPAN class = "CS-comment"> // next open up inprocserver32 </span> registrykey inprocserver32 = K. opensubkey (<SPAN class = "CS-string"> "inprocserver32" </span>, <SPAN class = "CS-keyword"> true </span> ); <SPAN class = "CS-comment"> // and delete the codebase key, again not throwing if missing </span> K. deletesubkey (<SPAN class = "CS-string"> "codebase" </span>, <SPAN class = "CS-keyword"> false </span> ); <SPAN class = "CS-comment"> // finally close the main key </span> K. close ();}

    The second function will remove the registry entries added when (IF) the class is unregistered-it's always a good suggestion to tidy up as you go.

Now you are ready to compile & test your control.

Testing the control

For this example I have chosen tstcon32.exe, which is available with the installation of. net. The main reason I 've used this rather than VB6 is that I don't have VB6 anymore.

Inserting the control

First up you need to insert your control, so to do that chooseEdit-> Insert new control, And choose your control from the dropdown...

This will result in a display as shown at the top of the article, if you're following along with my example code.

Testing Methods

The example control only has des one method, 'Question '. To test this within tstcon32, chooseControl-> invokemethodsFrom the menu, and select the method you want to call. note that because I defined the interface as autodual, I get gazillions of methods. if you implement an interface and expose this as the default Interface then the list of methods will be more manageable.

Click on the 'invoke' button will execute the method, which in this instance displays the obligatory message box.

Wrap up

Dropping support for creating ActiveX controls from Windows Forms controls is a pain, and one demo-i wish Microsoft had not made.

This article presents one way of exposing. net controls as ActiveX controls, and seems to work OK. having said that, I 've not exhaustively tested this and who knows what bugs might be lurking in there. I haven't delved into events yet, nor property change configurations, so there's some fun to be had there if you like that sort of thing.

The. net Framework truly is the best thing since sliced bread, but the lack of support for creating ActiveX controls from Windows Forms controls is inconvenient. there are applications out there (ours defined DED) which can be extended with ActiveX controls. it wocould be nice given the rest of the support in the framework to be able to expose Windows Forms controls to ActiveX containers, and maybe someday the support will be available.


About morgan Skinner

now working in Microsoft developer Services, based in the UK.

I loved. net so much I had to join the company !.

click here to view Morgan Skinner's online profile.

Related Article

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.