Saturday 5 May 2012

WHY USE AN ACTIVEX CONTROL?:



When you create an ActiveX control (OCX file), you are creating a TOOL.  This tool can be a custom control to do something that no standard control can do, or it can be a control that works exactly like a standard control works, but with slightly changed functionality, properties, events, and/or methods.  It can be whatever you want.
Let's say for example that you would like to create a PictureBox that loads and saves BMP, JPEG, and GIF pictures, as well as adds special effects to them when they are loaded.  No standard Visual Basic control can do this custom work.  You can get this functionality by putting in a HUGE amount of custom coding into your application, or by using my Advanced Bitmap Processing module (along with the JPEG module posted on this site), or something like that.  But why bother if the thing you're displaying the picture on (most often a PictureBox) can do that kind of work for you?  Creating a custom PictureBox that does this kind of custom functionality would take that kind of work off of your main application, and allow your code in your main application to be cleaner and simpler.
Another possible use for this tool would be to create a control that does things for you in order to take processing and code off your main application.  For example, you could create an ActiveX control that contains several smaller controls like a TreeView on the left, and a ListBox on the right which resizes them dynamically when the ActiveX control is resized.  It could also contain data validation for information being passed in to be displayed in the TreeView or ListBox.  This takes a LARGE amount of code off of our main application so that your main application's code is cleaner and simpler.
Another advantage to using ActiveX controls over just coding the functionality into your main application is the ActiveX control runs in its own thread.  If you are handling some intense processing, graphics work, database connectivity, data transferring, or sub-classing within your ActiveX Control and something goes wrong, the ActiveX will freeze instead of your entire application freezing up.  The ActiveX control DOES get loaded into the same memory space as your main application though, so it is possible for something to go wrong with your ActiveX control that takes down the rest of your program, but that is VERY unlikely.
Also, because ActiveX controls run in their own threads, you can create a "multi-threaded" application by embedding functionality into an ActiveX control and having them do your application's work.  For example, you could create a "Download Control" that simply goes to the FTP site you specify and downloads the file you specify to a destination file on your hard drive that you specify.  All you'd have to do to download multiple files simultaneously is create a control array (or several instances of that ActiveX control) on a form, set each one to download a different file to a different destination, and cut 'em all loose!  BOOM!  Multi-threading, baby!  Multiple downloads that are independent of each other.
But ActiveX controls are not just for use within Visual Basic, C++, Delphi, and other "COM aware" programming languages.  You can also use ActiveX controls over the internet!  It is possible to embed an ActiveX control right into a web page and then view it with Microsoft Internet Explorer 4.x or better.  
"But how do I embed an ActiveX control into my HTML or ASP web page?"
First of all, I need to be clear that ActiveX is a Microsoft technology and Netscape does not natively support it.  There are Netscape Plug-Ins that will allow you to use them within Netscape, but they obviously don't work as well as Microsoft Internet Explorer (which is designed to natively support them).
Now, let's take the "CustomButton" (CustmBtn.ocx) ActiveX control that I posted on this web site.  If you wanted to embed that into a web page, the HTML code would look something like this:
 
<
HTML>
<
HEAD>
<
TITLE>TestOCX</TITLE>

<
SCRIPT LANGUAGE="JavaScript">
  
// This function changes the caption to "Hello" and displays a MessageBox via JavaScript
  function 
ChangeCaption() {
    TestForm.CustomBtn.Caption = "Hello";
    alert ("Click OK when ready to change the caption again");
  }

</
SCRIPT>

<
SCRIPT LANGUAGE="VbScript">
  
' This function changes the caption to "Whasssup!" and displays a MessageBox via VbScript
  Sub 
ChangeBackColor
    TestForm.CustomBtn.Caption = "Whasssup!"
    
MsgBox "Caption was changed",vbOKOnly Or vbExclamation, " "
  End Sub
</
SCRIPT>

<
SCRIPT LANGUAGE="JavaScript" FOR="CustomBtn" EVENT="Click">
  
// This event fires when the ActiveX control is clicked and displays the About dialog
  
TestForm.CustomBtn.About();
</
SCRIPT>

</
HEAD>
<
BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<!-- The ActiveX control has to be within a form in order for it to be called via code //-->
<
FORM ID="TestForm">
  <
OBJECT ID="CustomBtn" CLASSID="CLSID:BB7BA40E-784E-11D4-AF87-0008C74B19A1">
    <
PARAM NAME="_ExtentX"   VALUE="2249">
    <
PARAM NAME="_ExtentY"   VALUE="714">
    <
PARAM NAME="Appearance" VALUE="0">
    <
PARAM NAME="BackColor"  VALUE="12632256">
    <
PARAM NAME="Caption"    VALUE="Testing">
    <
PARAM NAME="Enabled"    VALUE="-1">
    <
PARAM NAME="ForeColor"  VALUE="12632319">
    <
PARAM NAME="Style"      VALUE="0">
  </
OBJECT>
</
FORM>

<
SCRIPT LANGUAGE="JavaScript">
  
// Change the caption the first time via JavaScript
  
ChangeCaption();
</
SCRIPT>

<
SCRIPT LANGUAGE="VbScript">
  
' Change the caption the second time via VbScript
  Call 
ChangeBackColor
</
SCRIPT>

</
BODY>
</
HTML>
  
You'll notice a few things about this code.  First is that in order to use the ActiveX control within your page, it has to be embedded as an object.  Not only does it have to be embedded as an object, but it has to be embedded with an "ID" and within a form that also has an "ID".  The form it's embedded within doesn't have to do anything at all, but it has to be there, and it has to have an ID.  The reason for this is the embedded object can't stand alone in HTML just as a PictureBox control can't stand alone within Visual Basic without a Form, MDI Form, PropertyPage, UserControl, or UserDocument.  It has to be a part of a form within VB or HTML.  That is how the object is referenced in both cases...  by <FORM>.<CONTROL> where in this case, the form is "TestForm" and the control is "CustomBtn".  You'll notice that it is referenced in VbScript and JavaScript as such.  Again, VbScript is a Microsoft technology and isn't natively supported by Netscape (though I wish it was).
The second thing you may notice is that the ActiveX control's properties are initially set using the object's "PARAM" values, and later at run-time by referencing the <FORM>.<CONTROL>.<PROPERTY>.  That's easy enough, but what about Methods and Events?  Well, methods are called like functions from VbScript or JavaScript similar to properties... <FORM>.<CONTROL>.<METHOD>.  As far as events, those are a little bit more complex, but still fairly simple.  All you do is declare a JavaScript <SCRIPT> block and tack on "FOR" pointing to the name of the object that the event is for, and "EVENT" with the name of the event that the script block will be fired for represents.  In this case, the CLICK event is the one handled, and it calls the "About" method which shows the control's about message.
The third thing you may notice is that the ActiveX control is referenced not by location, but by "CLSID" (Class Identifier).  The CLSID is also referred to as the "GUID" (Globally Unique Identifier) or "UUID" (Universally Unique Identifier).
"But how will I know what the CLSID of my ActiveX control is?"
You'll be able to find out by looking in your Windows Registry via a tool called "Registry Editor" (REGEDIT.EXE).  Go to START > RUN and type "REGEDIT" and hit ENTER.  First locate the name of the interface you wish to reference by going to theHKEY_CLASSES_ROOT section and looking for the name of the project the class module interface you wish to reference is located.  This <PROJECT>.<CLASS> combination is refered to as the "ProgID" or "Program Identifier" and is used in calls toServer.CreateObject() in ASP, and CreateObject() in VB.  In this case, it's Custom_Button.CustomButton.  Under this registry key, you'll notice a registry key called "CLSID".  The default value for this key is the "CLSID" that you use to reference your ActiveX control as I did in the above code.
If you want to take this a step further and find out the location of the actual OCX or DLL file that is referenced by the CLSID, you can do that by going to the "CLSID" section under the HKEY_CLASSES_ROOT section and finding your CLSID... in this case {BB7BA40E-784E-11D4-AF87-0008C74B19A1}.  Once you find it, you'll notice there are several entries under it.  One of them is "InprocServer32".  The default value of this registry key will be the physical location of the file that is your ActiveX control.  In this case, CustmBtn.ocx.

No comments:

Post a Comment