VB中实现网页和EXE交互
2014-01-20 15:24
232 查看
实例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 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)" 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!
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 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)" 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!
相关文章推荐
- 网页(aspx)与用户控件(ascx)交互逻辑处理实现
- c#winform中使用WebKit传递js对象实现与网页交互
- 通过URL前缀实现网页和程序交互
- 用VB实现网页下载跟踪程序
- nodejs之http、fs、querystring模块实现网页与服务器的交互
- vb.net 2005实现自动登陆网页
- CSS 网页表单实现鼠标悬停交互效果
- 网页(aspx)与用户控件(ascx)交互逻辑处理实现
- win32+QT 实现exe中执行返回的网页内容重定向到txt文件,根据需求读取里面内容
- 用idhttp post 方法发送数据到服务器上。实现数据和网页的交互。
- 静态网页怎样实现动态交互?-JavaScript
- 静态网页与安卓交互的实现
- vb实现网页自动填表
- 用CSS实现网页中鼠标交互的方法
- vb.net 2005实现自动登陆网页
- Js调用exe程序方法(通过URL Protocol实现网页调用本地应用程序)
- #vb#实现获取网页标签中的值并按行存储到excel表格中
- QWebChannel实现QT与网页JS交互
- 利用vb向网页提交表单数据的实现代码
- C#在WinForm中使用WebKit传递js对象实现与网页交互的方法