比如我在webbowser中点击了一个按钮,我需要vb去执行部分代码,该怎么办?如何知道webbowser中哪个按钮被点击了?

解决方案 »

  1.   

    在工程中引用MSHTML库Private Type POINTAPI
            x As Long
            y As Long
    End Type
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    Private Declare Function ScreenToClient Lib "user32" (ByVal hWnd As Long, lpPoint As POINTAPI) As Long
    Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As LongPrivate WithEvents objDocument As HTMLDocumentPrivate Sub Form_Load()
        WebBrowser1.Navigate2 "http://www.baidu.com"
    End SubPrivate Sub Form_Resize()
        On Error Resume Next
        With WebBrowser1
            .Move .Left, .Top, ScaleWidth - .Left * 2, ScaleHeight - .Top - .Left
        End With
    End SubPrivate Function objDocument_onclick() As Boolean
        Dim pt As POINTAPI
        Dim hWnd As Long
        Dim objDoc As IHTMLDocument2
        Dim objElement As IHTMLElement
        
        GetCursorPos pt
        hWnd = WindowFromPoint(pt.x, pt.y)
        ScreenToClient hWnd, pt
        
        Set objDoc = objDocument
        Set objElement = objDoc.elementFromPoint(pt.x, pt.y)
        If Not objElement Is Nothing Then
            MsgBox "" & objElement.outerText
        End If
        objDocument_onclick = True
    End FunctionPrivate Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
        If Not pDisp Is Nothing Then Set objDocument = pDisp.Document
    End Sub
      

  2.   

    根据鼠标位置?测试了下,很不精准,对于按钮无效
    一般人家用html做界面应该不会使用这个技术的吧?
      

  3.   

    用HTML做界面也是使用MSHTML, 在MSHTML中有IHTMLXXXXX一系列的接口和对象,如果在VB中精确定位到你所需的按钮对象的话,基本和上面的代码类似,先在窗体中利用withevents关键字声明一个你所关联的IHTMLElement类型,然后在WebBrowser_DocumentComplete()事件中查找出你DOCUMENT对象中所关联的对象,然后用SET 命令挂接
      

  4.   

    http://blog.csdn.net/technofantasy/archive/2003/08/29/2964.aspx