实例1,通过网页元素属性交互:
1. 正常编写HTML页面,利用元素<a ....></a>的url属性传递参数。例如某个链接写成:
<a href="about.htm" url="run:notepad">执行此应用程序</a>
2. VB中编写代码。首富,引入WebBrowser控件,在部件中选中“Microsoft Internet Controls”;
引用它比较安全,发布时不用带上,因为WINDOWS系统都会有它,且会随着电脑上IE的不同而不同,内核版本完全一致。
3. 在”引用“中引用”Microsoft Object Library“(它指向的是MSHTML.TLB文件);
4. 在本窗体模块全局声明:Public WithEvents m_doc As HTMLDocument;
5. 在WebBrowser加载文档结束窗口事件中赋值:
Private Sub webAbout_DocumentComplete(ByVal pDisp As Object,URL As Variant)
Set Me.m_doc = Me.webAbout.Document
End Sub
6. 响应m_doc的点击事件:
Private Function m_doc_onclick() As Boolean
On Error Resume Next
Dim sUrl$
sUrl = m_doc.activeElement.URL
If Left(sUrl,3) = "run" Then
sApp = Right(sUrl,Len(sUrl) - 4)
If Dir(sApp) = "" Then
MsgBox "此文件不存在!" & vbCrLf & sApp,vbCritical,"!"
Exit Function
Else
ShellExecute 0&,vbNullString,sApp,vbNormalFocus
End If
End If
Exit Function
myEnd:
'MsgBox "不能执行,路径或文件不存在!" & m_doc.activeElement.URL,"!"
m_doc_onclick = True
End Function
实例2 一个示例:
不多说了,直接看代码吧
Public WithEvents m_runnotepad As HTMLAnchorElement
Public WithEvents m_doc As HTMLDocument
Private Sub Form_Load()
Me.WebBrowser1.Silent = True
Me.WebBrowser1.Navigate "about:blank"
Const tag$ = "<a href='#' url='$url' style='display:block;width:200px;margin:0 32px'>运行$name</a>"
Dim s$
'//s = "<a href='#' url='aaaa' id='runnotepad' onclick='runnotepad();'>运行NOTEPAD</a>"
Dim oFSO As Variant,oFiles As Variant,oFile As Variant
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFiles = oFSO.getfolder("c:\windows\system32").Files
Dim sDoc$
sDoc = "<body>"
For Each oFile In oFiles
If LCase(Right(oFile.Name,4)) = ".exe" Then
s = Replace(tag,"$url",oFile.Path)
s = Replace(s,"$name",Mid(oFile.Name,1,Len(oFile.Name) - 4))
sDoc = sDoc + s
End If
Next
sDoc = sDoc + "</body>"
Me.WebBrowser1.Document.write (sDoc)
Set Me.m_doc = Me.WebBrowser1.Document
Set Me.m_runnotepad = Me.WebBrowser1.Document.getElementById("runnotepad")
'//MsgBox Me.WebBrowser1.Document.getElementById("runnotepad").onclick
End Sub
Private Function m_doc_onclick() As Boolean
On Error Resume Next
'//Debug.Print m_doc.activeElement.URL
If Err.Number Then Exit Function
Shell m_doc.activeElement.url,vbNormalFocus
End Function
Private Function m_runnotepad_onclick() As Boolean
'//Shell "notepad",vbNormalFocus
End Function
实例3 EXE和JS交互:
首先,要让JS调用EXE里的函数,函数必须写成Public,Sub或Function都可以。然后,在WebBrowser的DocumentComplete事件中给网页body加上窗体的方法,如下:
Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object,URL As Variant)
' On Error Resume Next
pDisp.Document.body.setAttribute "extend",Me
End Sub
Function doSomething(ByVal sParam$)
MsgBox sParam,vbInformation Or vbOKOnly,"doSomething"
End Function
网页的代码如下:
<html>
<a href="void(0)" onclick="document.body.extend.doSomething('www.google.com');return false;">google.com</a><br />
</html>
欧了,就这么多,实现交互了。
实例4
最后再给出一个HTML文档,利用脚本执行EXE。不过这个可能需要权限。代码如下:
<script type="text/javascript"> function run(exename) { //alert('aa'); var wsh; try{ wsh=new ActiveXObject("WScript.Shell"); } catch(e) { //alert(e); } wsh.run(exename); } </script> <a href="#" onclick="run('file:///C:/abc.exe');">运行NOTEPAD</a>
全文结束。鸣谢Confidence! 原文链接:https://www.f2er.com/vb/258234.html