[英]Show VB6 forms when click a link in html page of webbrowser
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我正在使用 VB6 WebBrowser,当用户单击
WebBrowser
链接的任何特定链接时,我需要打开一个 vb6 表单,例如
In HTML在 HTML 中
<html>
<head>
<body>
<a href="--show vb6 form--">Click To show VB6 Form2</a>
</body>
</html>
I do't have any idea how to do it.我不知道该怎么做。 I thought sometime it can be done a third text file like when the link clicked will write a cod like
002
in a text file.我认为有时可以完成第三个文本文件,例如单击链接时会在文本文件中写入像
002
这样的 cod。
And the in vb form a Timer will check once a second the file, when timer detect the file contains 002
it will show the form.并且以 vb 形式,计时器将每秒检查一次文件,当计时器检测到文件包含
002
,它将显示该表格。
Can be do this by this method?可以通过这种方法做到这一点吗? or anything else shorter i can except?
或者其他更短的我可以除外?
Pick a better naming scheme like:选择一个更好的命名方案,例如:
<a href="#vb-showform2">Click To show VB6 Form2</a>
<a href="#vb-waffles">Waffles</a>
Then intercept link clicks via the BeforeNavigate2
event, look at the url and if it matches #vb-*
run your code:然后通过
BeforeNavigate2
事件拦截链接点击,查看 url,如果匹配#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
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.与其将表单名称放在
href
属性中,我相信更好的方法是设置您自己的data attribute
并使用它,在我看来,这是完成此类任务的更简洁的方法。
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.在我的示例中,在
href
标签内,我使用经典的void(0)
来阻止链接导航,否则您到 VB 表单的外部链接可能会破坏浏览器历史记录并产生意想不到的结果。
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.要使用
WebBrowser
控件,您应该已经在 VB 项目中添加了对Microsoft Internet Controls
的引用,接下来您需要添加对Microsoft HTML Library
的引用,该类型库包含在mshtml.tlb
文件中。
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:假设您的
WebBrowser
控件名为“WebBrowser1”,并且您有三个额外的表单,称为“Form1”、“Form2”和“Form3”,在放置WebBrowser
控件的表单中放置了这段代码:
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
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. javascript:void(0)
与 BeforeNavigate 事件的URL参数相同。
Another useful information which you can get from the HTMLDocument
and wouldn't be available in the BeforeNavigate
event is, for example:另一个可以从
HTMLDocument
获取但在BeforeNavigate
事件中不可用的有用信息是,例如:
HTMLdoc.activeElement.outerHTML
the result will be:结果将是:
<A href="javascript:void(0)" data-vb="Form2">Click To show Form2</A>
To do this with a button instead of a link, add the button to the document and a bit of javascript:要使用按钮而不是链接来执行此操作,请将按钮添加到文档和一些 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>
Then in the BeforeNavigate2
event:然后在
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.