Tuesday, July 17, 2007

* ActiveX Programming using Visual Studio 2005 - Part 1

This post is for people who are new to ActiveX programming and there is nothing 'Managed' about this post.

Pls read ActiveX Technology Overview to get started.

This will be a two part post and it will cover following :

Part 1 :

  • Create a basic ActiveX control using Visual Studio 2005.
  • Structure of the ActiveX control Project
  • Tools to test your ActiveX control
  • How to deploy ActiveX control on Web browser on Remote Machine.

Part 2 :

  • Create a MFC DLL using Visual Studio 2005
  • Create an ActiveX control referencing a MFC DLL.
  • Package and deploy multi file ActiveX Control on Web Browser on Remote Machine.

Lets start with Part1

  • Make sure you have installed Visual C++ as part of your VS 2005 installation which will install VC++ templates and compiler.
  • In Visual C++ -> MFC templates select MFC ActiveX control template.
  • Give name to the Solution/Project say 'MyActiveXControl'.
  • MFC ActiveX Control Wizard will start. Currently go with the default settings and click 'Finish'.
  • VS has already created a ready to use ActiveX control. We can build it and use it right away.
  • Before building read ReadMe.txt generated which explains the various files generated. Two things which are of importance at this moment are: CMyActiveXControlCtrl::OnDraw method in MyActiveXControlCtrl.cpp file which contains code for UI of the control. By default it draws a rectangle and an Ellipse inside it. If you are familiar with MFC you can change it.
  • Next is MyActiveXControl.idl file which defines the interface points.  The UUID above coclass MyActiveXControl is important because that becomes the CLASSID of your ActiveX control by which ActiveX control gets registered on client machine.
  • Build the project in Release Mode as Debug mode MFC runtime is not available on all machines. Upon successful build, VS automatically registers the control on local machine. The main file of the control is MyActiveXControl.ocx.
  • ActiveX Control Test Container is the tool to test controls. If your VS language settings are of VC++ you will see it in Tools menu else you can start it from following location : <Installation_Drive>\Program Files\Microsoft Visual Studio 8\Common7\Tools\tstcon32.exe.
  • Open the tool. In File -> Register controls you should be able to see the 'MyActiveXControl' as it has already been registered by VS.
  • Do Edit -> Insert New control.  Select your control and you should be able to see your control in work area. You can now test this control by changing its properties, events etc.
  • We have successfully tested our control on local machine. But testing the control on remote machine has different kinds of challenges. We are going to host our control in web browser and so we need to create an HTML page with our control embedded in it.
  • To facilitate the creation of HTML page there is another tool called ActiveX Control Pad.
  • Start ActiveX Control Pad. File -> New HTML. Edit-> Insert ActiveX Control. Select your control. Close the child windows opened. You will see tool has inserted Object tag in html page with information about your control like CLASSID. To be able to load control on remote machine you need to add CODEBASE property to OBJECT tag. The value of CODEBASE is URL where the .ocx file is hosted. The html page should look like this :
<HTML>
<HEAD>
<TITLE>New Page</TITLE>
</HEAD>
<BODY>

<OBJECT ID="ActiveX31" WIDTH=100 HEIGHT=51
CLASSID="CLSID:<CLASS ID of your control>"
CODEBASE="http://<MyMachine>/activex/MyActiveXControl.ocx" >
<PARAM NAME="_Version" VALUE="65537">
<PARAM NAME="_ExtentX" VALUE="2646">
<PARAM NAME="_ExtentY" VALUE="1323">
<PARAM NAME="_StockProps" VALUE="0">
</OBJECT>

</BODY>
</HTML>

6 comments:

  1. Superb! just what I was looking for to begin development of ActiveX controls. Written crisply. Thank you very much.

    If time premits please have one with ATL so that we can get an understanding of the benefits of using MFC.

    Anton

    ReplyDelete
  2. Please post the second part of the article.

    Thanks
    Anton

    ReplyDelete
  3. where is the part 2 of the article?????

    ReplyDelete
  4. where is the second part???

    ReplyDelete
  5. Please post the second part

    thank you

    ReplyDelete
  6. Thank you. Just what I was looking for!!

    ReplyDelete