vb检测html事件,VB代码VB小程序:捕获 WebBrowser 控件的鼠标事件
49. 捕獲 WebBrowser 控件的鼠標事件
VB 的網頁瀏覽控件 WebBrowser 沒有 MouseDown、MouseMove、MouseUp 等鼠標事件,要在程序中捕獲這些事件,必須另想辦法。本文使用注入 java 腳本的方法來捕獲控件的鼠標事件。
另一更為有效的方法,可以捕獲 WebBrowser 眾多的鼠標和鍵盤事件,如鼠標坐標、按下了鍵盤哪個鍵,以及鍵盤 Ctrl、Alt、Shift 鍵的狀態、當前網頁元素的ID、索引等等。參見另一文章:VB 捕獲 WebBrowser控件的鼠標和鍵盤事件
'以下代碼在 VB6 調試通過
'勾選部件:Microsoft Internet Controls,在窗體放置控件:WebBrowser1
'程序運行后,在 WebBrowser1 內移動或按下鼠標,注意觀察窗口標題欄給出的信息
'本人原創,轉載請注明來源:http://hi.baidu.com/100bd/blog/item/19795a80b2ba61d5bd3e1ee1.html
Private Sub Form_Load()
Me.Caption = "WebBrowser 鼠標事件例子"
WebBrowser1.Navigate "about:blank" '設置為空白頁或某網頁,否則無法進行鼠標捕獲
'??? WebBrowser1.Navigate "http://www.baidu.com" '顯示百度首頁
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
Me.Caption = "WebBrowser 鼠標事件例子"
End Sub
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 nStr As String
nStr = URL
If Left(nStr, 8) <> "mouse://" Then Exit Sub '讓網頁正常跳轉
Cancel = True '讓網頁不要跳轉
Dim nMouse As String, x As Long, y As Long, S As Long
nStr = Mid(nStr, 9)
S = InStr(nStr, "|")
nMouse = Left(nStr, S - 1): nStr = Mid(nStr, S + 1)
S = InStr(nStr, "|")
x = Val(Left(nStr, S - 1)): y = Val(Mid(nStr, S + 1))
Me.Caption = nMouse & ":" & x & " " & y '顯示鼠標捕獲的狀態
End Sub
Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
On Error Resume Next
Dim js As String
'腳本:用于捕獲 鼠標按下
js = "document.body.οnmοusedοwn=function()" & vbCrLf & _
"{location.href='mouse://down|'+window.event.x + '|'+window.event.y;}"
WebBrowser1.Document.parentWindow.execScript js, "javascript"
'腳本:用于捕獲 鼠標移動
js = "document.body.οnmοusemοve=function()" & vbCrLf & _
"{location.href='mouse://move|'+window.event.x + '|'+window.event.y;}"
WebBrowser1.Document.parentWindow.execScript js, "javascript"
'腳本:用于捕獲 鼠標抬起
js = "document.body.οnmοuseup=function()" & vbCrLf & _
"{location.href='mouse://up|'+window.event.x + '|'+window.event.y;}"
WebBrowser1.Document.parentWindow.execScript js, "javascript"
End Sub
'本人原創,轉載請注明來源:http://hi.baidu.com/100bd/blog/item/19795a80b2ba61d5bd3e1ee1.html
另一更為有效的方法,可以捕獲 WebBrowser 眾多的鼠標和鍵盤事件,如鼠標坐標、按下了鍵盤哪個鍵,以及鍵盤 Ctrl、Alt、Shift 鍵的狀態、當前網頁元素的ID、索引等等。參見另一文章:VB 捕獲 WebBrowser控件的鼠標和鍵盤事件
總結
以上是生活随笔為你收集整理的vb检测html事件,VB代码VB小程序:捕获 WebBrowser 控件的鼠标事件的全部內容,希望文章能夠幫你解決所遇到的問題。