VB中实现网页和EXE交互
实例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, vbNullString, vbNullString, vbNormalFocus
End If
End If
Exit Function
myEnd:
‘MsgBox "不能执行,路径或文件不存在!" & m_doc.activeElement.URL, vbCritical, "!"
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:
最后再给出一个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!
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。