Using an ActiveX Control from a Web Page

 < Free Open Study > 



As you can see, creating a full control with ATL demands quite a bit of knowledge. You need to be very comfortable with not only COM itself, but the ATL framework and the Win32 API as well. The reason ATL is popular, despite its complexity, is the end result is a very lightweight binary, just the right thing for low bandwidth downloading over the Internet. When you insert a control with the ATL Object Wizard, one of the generated files is a simple HTML file that loads your control into the current browser on your development machine. This file, which is located in your project's subfolder, simply uses the <OBJECT> tag to specify the CLSID of the control you wish to load, as well as give it an ID to refer to during the scope of the page:

<HTML> <HEAD> <TITLE>ATL 3.0 test page for object ShapesControl</TITLE> </HEAD> <BODY> <OBJECT       CLASSID="CLSID:03FBA320-71D1-11D3-B92D-0020781238D4"> </OBJECT> </BODY> </HTML>

If you open this file in Visual Studio, you may right-click anywhere on the file and select the Preview option to see how your control will appear on a web page (see Figure 14-39).

click to expand
Figure 14-39: Hosting ShapesControl in IE.

While it is inspiring to see our control hosted in a web page, we have no code to respond to our [default, source] interface, or allow the end user to manipulate our properties. The act of manipulating our [default] interface is straightforward; create a VBScript block which makes use of them. To respond to our custom event requires an additional bit of script.

Manipulating ShapesControl on the Web

During the course of developing ShapesControl, one addition we made to the default generated code was to add support for property bag persistence. Web browsers make exclusive use of this approach, which may be manipulated with the <PARAM> tag. Let's initialize our control with a shape of SQUARE (i.e., 1) and the text "We Love COM":

<HTML> <HEAD> <TITLE>ATL 3.0 test page for object ShapesControl</TITLE> </HEAD> <BODY> <OBJECT  CLASS>      <PARAM NAME = "ShapeType" VALUE ="1">      <PARAM NAME = "Caption" VALUE = "We Love COM"> </OBJECT> </BODY> </HTML>


Figure 14-40: Using our property bag support.

Next, we should provide a way to capture our custom ClickedControl() event. In VBScript, we define an event sink as so:

<HTML> <HEAD> <TITLE>ATL 3.0 test page for object ShapesControl</TITLE> </HEAD> <BODY> <OBJECT  CLASS>      <PARAM NAME = "ShapeType" VALUE ="1">      <PARAM NAME = "Caption" VALUE = "We Love COM"> </OBJECT> <script LANGUAGE = "VBScript" FOR = "ShapesControl"           EVENT = "ClickedControl(xPos, yPos)">           MsgBox "X= " & xPos & " Y= " & yPos </script>     </BODY> </HTML> 

The VBScript FOR EVENT syntax allows you to write event sinks for any methods defined in the [default, source] interface of your control. Because VBScript is very loosely typed, we do not specify the type of parameter being received in the ClickedControl() event (everything is assumed to be a Variant) and simply pass them into the MsgBox method for a simple display (Figure 14-41).


Figure 14-41: Capturing our event in VBScript.



 < Free Open Study > 



Developer's Workshop to COM and ATL 3.0
Developers Workshop to COM and ATL 3.0
ISBN: 1556227043
EAN: 2147483647
Year: 2000
Pages: 171

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net