Show VB6 forms when click a link in html page of webbrowser

前端 未结 3 1712
终归单人心
终归单人心 2021-01-21 08:56

I am working with VB6 WebBrowser, Here i need to open a vb6 form when user click any particular link of WebBrowser\'s link like

In HTML

3条回答
  •  生来不讨喜
    2021-01-21 09:45

    Instead of putting the form name inside the href attribute, I believe a better method would be to set a your own data attribute and use that, it seems to me a much cleaner way to do such a task.

    In my example, inside the href tag i'm using the classic void(0) to prevent the link navigation, otherwise your external link to VB forms could break the browser history with unexpected results.

    To use the WebBrowser control, You should have already added in your VB project a reference to the Microsoft Internet Controls, what you need next is to add a reference to the Microsoft HTML Library, the type library contained inside the mshtml.tlb file.

    Assuming your WebBrowser control is called "WebBrowser1", and you have three additional forms called "Form1", "Form2" and "Form3", in the form where you placed the WebBrowser control put this piece of code:

    Private HTMLdoc As MSHTML.HTMLDocument
    
    ' Create a Web Page  to test the navigation '
    ' You can skip this phase after your test are successfully executed '
    Private Sub Form_Load()
        Dim HTML As String
        WebBrowser1.Navigate "about:blank"
        HTML = ""
        HTML = HTML & "Open a VB Form from a Link"
        HTML = HTML & ""
        HTML = HTML & "Click To show Form1"
        HTML = HTML & "
    " HTML = HTML & "Click To show Form2" HTML = HTML & "
    " HTML = HTML & "Click To show Form3" HTML = HTML & "
    " HTML = HTML & "" HTML = HTML & "" WebBrowser1.Document.Write HTML End Sub ' This will load and show the form specified in the data-vb attribute of the link ' Private Sub WebBrowser1_BeforeNavigate2(ByVal pDisp As Object, URL As Variant, Flags As Variant, TargetFrameName As Variant, PostData As Variant, Headers As Variant, Cancel As Boolean) Dim frm As Form, FormName as String If Not (WebBrowser1.Document Is Nothing) Then Set HTMLdoc = WebBrowser1.Document FormName = vbNullString & HTMLdoc.activeElement.getAttribute("data-vb") If Not FormName = vbNullString Then Set frm = Forms.Add(FormName) frm.Show End If End If End Sub

    An additional note:

    You can get the content of the clicked link in following way:

    HTMLdoc.activeElement.toString
    

    Obviously, for all links in my test page, the result will be:

    javascript:void(0) which is the same as the URL parameter of the BeforeNavigate event.

    Another useful information which you can get from the HTMLDocument and wouldn't be available in the BeforeNavigate event is, for example:

    HTMLdoc.activeElement.outerHTML
    

    the result will be:

    Click To show Form2
    

提交回复
热议问题