TheChaseMan's Frenetic SoapBox

Always looking for better ways to do things...

HOWTO: Create a Binary DHTML Behavior with Visual Basic 6.0

You can download the source code for this example at http://www.unboxedsolutions.com/sean/files/VBBehavior.zip
  1. Create a new ActiveX Dll project and rename the project to MyVBBehavior
  2. Rename the default Class1 to CBehavior.
  3. Add the following references:

    Microsoft HTML Object Library - C:\WINDOWS\System32\MSHTML.TLB
    Microsoft Internet Controls - C:\WINDOWS\System32\shdocvw.dll
    VB IObjectSafety Library - objsafe.tlb (See KB article Q182598 from MSDN)
     
  4. In the CBehavior class, implement the following interfaces:

    'Interfaces
    Implements IElementBehavior
    Implements IElementBehaviorFactory
    Implements IObjectSafetyTLB.IObjectSafety

     
  5. In the CBehavior Class, declare the following variables:

    Private mobjElementBehaviorSite As MSHTML.IElementBehaviorSite
    Private WithEvents mobjTheElement As MSHTML.HTMLBody
    Private mobjElementBehaviorSite As MSHTML.IElementBehaviorSite
    Private WithEvents mobjTheElement As MSHTML.HTMLBody
    Private WithEvents mobjDivElement As MSHTML.HTMLDivElement
    Private WithEvents mButtonElement As MSHTML.HTMLButtonElement
     
  6. In the CBehavior Class, type the following code:

    Private Sub IElementBehavior_Init(ByVal pBehaviorSite As MSHTML.IElementBehaviorSite)
        On Error Resume Next
        Set mobjElementBehaviorSite = pBehaviorSite
    End Sub

    Private Sub IElementBehavior_Notify(ByVal lEvent As Long, pVar As Variant)
        On Error Resume Next

        If lEvent = 1 Then
            Set mobjTheElement = mobjElementBehaviorSite.GetElement
            Call HookPageElements
        End If
    End Sub

    Public Sub HookPage(obj As Object)
        Set mobjTheElement = obj
        Call HookPageElements
    End Sub

    Private Sub HookPageElements()
        Set mobjDivElement = mobjTheElement.Document.getElementById("MyDivTag")
        Set mButtonElement = mobjTheElement.Document.getElementById("MyButtonElement")
    End Sub

    Private Function mButtonElement_onclick() As Boolean
        mobjDivElement.innerText = "Hello World!"
    End Function

    Private Sub IElementBehavior_Detach()
        Set mobjTheElement = Nothing
    End Sub

    Private Function IElementBehaviorFactory_FindBehavior(ByVal bstrBehavior As String, ByVal bstrBehaviorUrl As String, ByVal pSite As MSHTML.IElementBehaviorSite) As MSHTML.IElementBehavior
        Set IElementBehaviorFactory_FindBehavior = Me
    End Function

    Private Sub IObjectSafety_GetInterfaceSafetyOptions(ByVal riid As Long, pdwSupportedOptions As Long, pdwEnabledOptions As Long)
    End Sub

    Private Sub IObjectSafety_SetInterfaceSafetyOptions(ByVal riid As Long, ByVal dwOptionsSetMask As Long, ByVal dwEnabledOptions As Long)
    End Sub

     
  7. Save the MyVBBehavior project.
  8. From the File menu, click Make MyVBBehavior.dll to build the DLL.
  9. From the Project menu, click MyVBBehavior Properties and click the Component tab. In the Component tab, make sure Binary Compatibility is set. This will allow us to interactively debug and edit-and-continue (something you can't do in VC++).
  10. Save the project.
  11. From the Visual Studio 6 Tools folder, start the OLE View application.
  12. In OLE View, click the View TypeLib option and open MyVBBehavior.dll.


     
  13. At the very bottom of the IDL file, you should see a coclass definition. Copy the UUID value defined above it. In this example, that would be "6C96BAD2-6A57-4556-94E7-488AB5326B45". You will need this GUID for the Web page that instantiates our behavior.

    [
    uuid(6C96BAD2-6A57-4556-94E7-488AB5326B45),
    version(1.0)
    ]
    coclass CBehavior {
    [default] interface _CBehavior;
    interface IElementBehaviorFactory;
    interface IObjectSafety;
    interface IElementBehavior;
    };
     
  14. Create and save the following HTML as my_vb_behavior.htm, however MAKE SURE YOU SUBSTITUTE the CLASSID GUID value with the GUID value you copied from the OLE View application.

    <HTML XMLNS:CUSTOM>
    <HEAD>
    <TITLE>My VB Behavior</title>
    <object ID=objBehavior CLASSID="clsid:6C96BAD2-6A57-4556-94E7-488AB5326B45">
    </object>

    </HEAD>
    <BODY id="theBody">
    <div id="MyDivTag"></div>
    <br>
    <input type="button" id="MyButtonElement" value="Fill DIV">
    <script language='vbscript'>
        objBehavior.HookPage thebody
    </script>
    </BODY>
    </HTML>
     
  15. Save the HTML file and open it in Internet Explorer.
  16. Click the Fill DIV button.


     
  17. Close Internet Explorer.
  18. Set a breakpoint in the VB IDE in the HookPage method.
  19. Start the debugger.
  20. Open the My_vb_behavior.html page in Internet Explorer and you will be ready to leverage the edit-and-continue ability in VB to develop your behavior!



     

Digg!

posted on Monday, March 08, 2004 5:53 PM

Feedback

# re: HOWTO: Create a Binary DHTML Behavior with Visual Basic 6.0 9/3/2004 10:16 AM Boss Hog

hi,
its a good aproach but if i have understood
you use the html object tag with ProgId.
in this case is suppose that the dll is registred on client side ??!

but the HTC file are in server side.

my question is how can do for call binary component
directly from server if it is possible

# re: HOWTO: Create a Binary DHTML Behavior with Visual Basic 6.0 9/3/2004 10:48 AM Sean Chase

Actually it runs client side, so it is not possible. You can put a CAB file on the server and IE will figure out whether or not the component is installed and do so if necessary. That is very simple. HTC is also client-side using javascript. This just gives you the ability to do the same kind of thing, plus get outside the world of IE much like an HTA. Also you get the nice added benefit of being able to debug interactively with VB and leverage its nice edit-and-continue capability.