VS 2010 Simulate click on webbrowser-VBForums
Results 1 to 12 of 12

Thread: Simulate click on webbrowser

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Posts
    291

    Simulate click on webbrowser

    Sup all?

    OK, I made a program that simulates a mouse click on a specific X , Y coordinates on the webbrowser. The thing is that, is not working here is my code

    VB.NET Code:
    1. Class Control
    2.         Public Sub New(ByVal Handle As IntPtr)
    3.             hWnd = Handle
    4.         End Sub
    5.  
    6.         Private hWnd As IntPtr = IntPtr.Zero
    7.         Public Property ControlHandle() As IntPtr
    8.             Get
    9.                 Return hWnd
    10.             End Get
    11.             Set(ByVal value As IntPtr)
    12.                 hWnd = value
    13.             End Set
    14.         End Property
    15.  
    16. Const WM_LBUTTONDOWN As Int32 = &H201
    17. Const WM_LBUTTONUP As Int32 = &H202
    18.  
    19. <DllImport("user32.dll", SetLastError:=True)> _
    20.         Private Shared Function PostMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
    21.         End Function    
    22.    
    23.        Public Sub ClickControl(ByVal x As Integer, ByVal y As Integer)
    24.             Dim coords As Integer = (y << 16) + x
    25.             Dim lParam As New IntPtr(coords)
    26.             PostMessage(hWnd, WM_LBUTTONDOWN, IntPtr.Zero, lParam)
    27.             PostMessage(hWnd, WM_LBUTTONUP, IntPtr.Zero, lParam)
    28.         End Sub
    29.  
    30.         Public Sub ClickControl(ByVal p As Point)
    31.             ClickControl(p.X, p.Y)
    32.         End Sub
    33.   End Class
    34. End Namespace

    I call is like this

    VB.NET Code:
    1. handle = Control.FindControl(WebBrowser1.Handle, Nothing) '"Internet Explorer_Server")
    2.         MyControl = New Control(handle)
    3.         Dim test As Point
    4.         test.X = 200
    5.         test.Y = 58
    6.         MyControl.ClickControl(test)

    But the thing is that, is does not work! What did I do wrong here?

    I also tried changing
    Const WM_LBUTTONDOWN As Int32 = &H201
    to
    Const WM_LBUTTONDOWN As Int32 = &H2

    and my webbrowser goes blank.
    Last edited by simon66; Feb 20th, 2011 at 11:50 AM.

  2. #2
    Frenzied Member make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    1,912

    Wink Re: Simulate click on webbrowser

    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Posts
    291

    Re: Simulate click on webbrowser

    Quote Originally Posted by make me rain View Post
    None of those examples helped me. They involve clicking on a button or so (By grabbing it's id) the thing is, this is a flash game, so I need it to click on a specific x,y coordinates. Thanks for the help tho.
    Maybe jmc can help out

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Posts
    291

    Re: Simulate click on webbrowser

    BUMP!
    Any help?

  5. #5
    Hyperactive Member
    Join Date
    Dec 2006
    Posts
    283

    Re: Simulate click on webbrowser

    Is this something your looking for? i have been working on this button clicker.

    http://www.mediafire.com/?pakb99ixx5zrjgf
    Last edited by crampz; Feb 19th, 2011 at 03:48 PM.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Posts
    291

    Re: Simulate click on webbrowser

    Quote Originally Posted by crampz View Post
    Is this something your looking for? i have been working on this button clicker.

    http://www.mediafire.com/?pak********
    Oh plz, I'm not that stupid. Not gonna run it, since it might be a virus. Plus as the forum rules, no compiled files, source code only!

  7. #7
    Hyperactive Member
    Join Date
    Dec 2006
    Posts
    283

    Re: Simulate click on webbrowser

    Quote Originally Posted by simon66 View Post
    Oh plz, I'm not that stupid. Not gonna run it, since it might be a virus. Plus as the forum rules, no compiled files, source code only!
    Ok my apologies, well here is part of the code then that will get your started.

    Code:
        Private Declare Sub SetCursorPos Lib "User32" (ByVal X As Integer, ByVal Y As Integer)
        Private Const MOUSEEVENTF_LEFTDOWN As Integer = &H2
        Private Const MOUSEEVENTF_LEFTUP As Integer = &H4
        Private Const MOUSEEVENTF_RIGHTDOWN As Integer = &H6
        Private Const MOUSEEVENTF_RIGHTUP As Integer = &H8
        Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Integer, ByVal dx As Integer, _
                                                      ByVal dy As Integer, ByVal cButtons As Integer, ByVal dwExtraInfo As Integer)
            Cursor.Position = New Point(CursorX, CursorY)
            Dim HowManyClicks As Int32 = tsbClicks.Text
            For i As Int32 = 0 To HowManyClicks
                mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
                mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
            Next
    But i built the program to use on X,Y Co-ordinates of a button or something that could be clicked without using html code
    Last edited by crampz; Feb 19th, 2011 at 04:18 PM.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Posts
    291

    Re: Simulate click on webbrowser

    Quote Originally Posted by crampz View Post
    Ok my apologies, well here is part of the code then that will get your started.

    Code:
        Private Declare Sub SetCursorPos Lib "User32" (ByVal X As Integer, ByVal Y As Integer)
        Private Const MOUSEEVENTF_LEFTDOWN As Integer = &H2
        Private Const MOUSEEVENTF_LEFTUP As Integer = &H4
        Private Const MOUSEEVENTF_RIGHTDOWN As Integer = &H6
        Private Const MOUSEEVENTF_RIGHTUP As Integer = &H8
        Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Integer, ByVal dx As Integer, _
                                                      ByVal dy As Integer, ByVal cButtons As Integer, ByVal dwExtraInfo As Integer)
            Cursor.Position = New Point(CursorX, CursorY)
            Dim HowManyClicks As Int32 = tsbClicks.Text
            For i As Int32 = 0 To HowManyClicks
                mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
                mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
            Next
    But i built the program to use on X,Y Co-ordinates of a button or something that could be clicked without using html code
    Sorry but no luck. It wont Lock on webbrowser.

  9. #9
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,920

    Re: Simulate click on webbrowser

    These two are not the same
    1. Public Sub Clickonform(ByVal x As Integer, ByVal y As Integer)
    2. MyControl.ClickControl(test)
    Not by the name or by the signature!

  10. #10
    Hyperactive Member
    Join Date
    Dec 2006
    Posts
    283

    Re: Simulate click on webbrowser

    Code:
     Private Declare Sub SetCursorPos Lib "User32" (ByVal X As Integer, ByVal Y As Integer)
        Private Const MOUSEEVENTF_LEFTDOWN As Integer = &H2
        Private Const MOUSEEVENTF_LEFTUP As Integer = &H4
        Private Const MOUSEEVENTF_RIGHTDOWN As Integer = &H6
        Private Const MOUSEEVENTF_RIGHTUP As Integer = &H8
        Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Integer, ByVal dx As Integer, _
                                                      ByVal dy As Integer, ByVal cButtons As Integer, ByVal dwExtraInfo As Integer)
        Dim CursorX, CursorY As Integer
    
        Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
    
            For Each ch As Char In tsbClicks.Text
                If Not Char.IsDigit(ch) Then
                    If Not ch = ":" Then
                        'bad character
                    End If
                End If
            Next
            Cursor.Position = New Point(CursorX, CursorY)
            Dim HowManyClicks As Int32 = tsbClicks.Text
            For i As Int32 = 0 To HowManyClicks
                mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
                mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
            Next
    
        End Sub
    tsbClicks.Text = the textbox for amount of clicks

    Code:
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            lblMousepos.Text = (MousePosition.X & "," & MousePosition.Y)
        End Sub
    This timer is on a 100 millisecond interval which then displays the current x,y postion

    Code:
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Timer1.Start()
        End Sub
    When form starts, timer starts

    Code:
        Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
            If e.KeyCode = Keys.ControlKey Then
                CursorX = (MousePosition.X)
                CursorY = (MousePosition.Y)
                tslCursor.Text = (CursorX & "," & CursorY)
            End If
        End Sub
        Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
            Dim document As HtmlDocument = WebBrowser1.Document
            AddHandler document.Body.KeyDown, New HtmlElementEventHandler(AddressOf WebBrowser1_KeyDown)
        End Sub
    
        Private Sub WebBrowser1_KeyDown(ByVal sender As Object, ByVal e As HtmlElementEventArgs)
            If e.CtrlKeyPressed And e.AltKeyPressed <> Keys.ControlKey Then
                CursorX = (MousePosition.X)
                CursorY = (MousePosition.Y)
                tslCursor.Text = (CursorX & "," & CursorY)
                tsbClicks.ReadOnly = False
            End If
        End Sub
    And all of that is when the CTRL key ONLY is pressed it takes the X,Y postions and loads them into CursorX and CursorY.

    That code works

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Posts
    291

    Re: Simulate click on webbrowser

    Quote Originally Posted by VBDT View Post
    These two are not the same
    1. Public Sub Clickonform(ByVal x As Integer, ByVal y As Integer)
    2. MyControl.ClickControl(test)
    Not by the name or by the signature!

    My bad. I fixed it. I copied the wrong function :P

    Still need help tho.

  12. #12
    Junior Member
    Join Date
    Jan 2011
    Posts
    26

    Re: Simulate click on webbrowser

    I got all of this but at debug time I get an error from the line

    Code:
    mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)

    the error is:-

    System.Runtime.InteropServices.MarshalDirectiveException was unhandled

    Message=PInvoke restriction: cannot return variants.

    any ideas?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.