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' οnclick='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

再写个doSomething函数(方法)给网页调用:

Function doSomething(ByVal sParam$)
    MsgBox sParam, vbInformation Or vbOKOnly, "doSomething"
End Function

网页的代码如下:

<html>
<a href="void(0)" οnclick="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="#" οnclick="run('file:///C:/abc.exe');">运行NOTEPAD</a>



全文结束。鸣谢Confidence!


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值