单击webbrowser的html页面中的链接时显示VB6表单

时间:2017-08-03 05:32:31

标签: javascript html vb6 webbrowser-control

我正在使用VB6 WebBrowser,我需要在用户点击WebBrowser链接的任何特定链接时打开vb6表单

在HTML中

<html>
<head>
<body>
<a href="--show vb6 form--">Click To show VB6 Form2</a>
</body>
</html>

我不知道该怎么做。我想有时它可以完成第三个文本文件,比如点击链接时会在文本文件中写出002这样的鳕鱼。

并且在vb形式的Timer中将每秒检查一次文件,当计时器检测到文件包含002时,它将显示该表单。

可以通过这种方法做到这一点吗?还是其他什么东西我可以除外?

3 个答案:

答案 0 :(得分:4)

选择一个更好的命名方案,如:

<a href="#vb-showform2">Click To show VB6 Form2</a>
<a href="#vb-waffles">Waffles</a>

然后通过BeforeNavigate2事件拦截链接点击,查看网址,如果匹配#vb-*,请运行您的代码:

Private Sub WebBrowserCtrl_BeforeNavigate2(ByVal pDisp As Object, URL As Variant, Flags As Variant, TargetFrameName As Variant, PostData As Variant, Headers As Variant, Cancel As Boolean)

    '// get #vb-XXX command from url
    Dim pos As Long: pos = InStrRev(URL, "#vb-")

    If pos Then
        Cancel = True '// stop default navigation

        URL = Mid$(URL, pos + 4)

        Select Case LCase$(URL)
            Case "showform2": Form2.Show
            '...
            Case "waffles":   MsgBox "Waffles."
            Case Else:        MsgBox "Unknown Command " & URL
        End Select
    End If

End Sub

答案 1 :(得分:4)

我不相信将表单名称放在href属性中,我相信更好的方法是设置你自己的 data attribute并使用它,在我看来一种更清洁的方式来完成这样的任务。

在我的示例中,在href标记内我使用经典void(0)来阻止链接导航,否则您对VB表单的外部链接可能会破坏浏览器历史记录并产生意外结果。

要使用WebBrowser控件,您应该已经在VB项目中添加了对Microsoft Internet Controls的引用,接下来需要的是添加对Microsoft HTML Library的引用,包含在mshtml.tlb文件中的类型库。

假设您的WebBrowser控件名为“WebBrowser1”,并且您在放置WebBrowser控件的表单中有三个名为“Form1”,“Form2”和“Form3”的其他表单这段代码:

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 = HTML & "<title>Open a VB Form from a Link</title>"
    HTML = HTML & "<body>"
    HTML = HTML & "<a data-vb='Form1' href='javascript:void(0)'>Click To show Form1</a>"
    HTML = HTML & "</br>"
    HTML = HTML & "<a data-vb='Form2' href='javascript:void(0)'>Click To show Form2</a>"
    HTML = HTML & "</br>"
    HTML = HTML & "<a data-vb='Form3' href='javascript:void(0)'>Click To show Form3</a>"
    HTML = HTML & "</br>"
    HTML = HTML & "</body>"
    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

附加说明:

您可以通过以下方式获取所点击链接的内容:

HTMLdoc.activeElement.toString

显然,对于我的测试页面中的所有链接,结果将是:

javascript:void(0)与BeforeNavigate事件的 URL 参数相同。

您可以从HTMLDocument获取并且在BeforeNavigate事件中无法使用的另一个有用信息,例如:

HTMLdoc.activeElement.outerHTML

结果将是:

<A href="javascript:void(0)" data-vb="Form2">Click To show Form2</A>

答案 2 :(得分:2)

要使用按钮而不是链接执行此操作,请将按钮添加到文档中并添加一些javascript:

<input type="button" id="MyButton1_id" style="cursor: pointer" name=MyButton1 value="Show It!">

<SCRIPT LANGUAGE="VBScript">
Sub MyButton1_OnClick()
   location.href = "event:button1_show"
End Sub
</SCRIPT>

然后在BeforeNavigate2事件中:

Public Sub webBrowser_BeforeNavigate2(ByVal pDisp As Object, URL As Variant, flags As Variant, TargetFrameName As Variant, PostData As Variant, Headers As Variant, Cancel As Boolean)

  Select Case LCase$(URL)
    Case "event:button1_show"
      Cancel = True
      Form2.Show
    Case "event:other_stuff"
      'other stuff to do, etc
  End Select
End Sub