-
Feb 17th, 2011, 10:23 PM
#1
Thread Starter
Hyperactive Member
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:
Class Control Public Sub New(ByVal Handle As IntPtr) hWnd = Handle End Sub Private hWnd As IntPtr = IntPtr.Zero Public Property ControlHandle() As IntPtr Get Return hWnd End Get Set(ByVal value As IntPtr) hWnd = value End Set End Property Const WM_LBUTTONDOWN As Int32 = &H201 Const WM_LBUTTONUP As Int32 = &H202 <DllImport("user32.dll", SetLastError:=True)> _ 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 End Function Public Sub ClickControl(ByVal x As Integer, ByVal y As Integer) Dim coords As Integer = (y << 16) + x Dim lParam As New IntPtr(coords) PostMessage(hWnd, WM_LBUTTONDOWN, IntPtr.Zero, lParam) PostMessage(hWnd, WM_LBUTTONUP, IntPtr.Zero, lParam) End Sub Public Sub ClickControl(ByVal p As Point) ClickControl(p.X, p.Y) End Sub End Class End Namespace
I call is like this
VB.NET Code:
handle = Control.FindControl(WebBrowser1.Handle, Nothing) '"Internet Explorer_Server") MyControl = New Control(handle) Dim test As Point test.X = 200 test.Y = 58 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 12:50 PM.
-
Feb 18th, 2011, 05:38 AM
#2
Re: Simulate click on webbrowser
-
Feb 18th, 2011, 09:29 PM
#3
Thread Starter
Hyperactive Member
Re: Simulate click on webbrowser
Originally Posted by make me rain
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
-
Feb 19th, 2011, 04:28 PM
#4
Thread Starter
Hyperactive Member
Re: Simulate click on webbrowser
-
Feb 19th, 2011, 04:43 PM
#5
Hyperactive Member
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 04:48 PM.
-
Feb 19th, 2011, 04:56 PM
#6
Thread Starter
Hyperactive Member
Re: Simulate click on webbrowser
Originally Posted by crampz
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!
-
Feb 19th, 2011, 04:59 PM
#7
Hyperactive Member
Re: Simulate click on webbrowser
Originally Posted by simon66
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 05:18 PM.
-
Feb 20th, 2011, 01:19 AM
#8
Thread Starter
Hyperactive Member
Re: Simulate click on webbrowser
Originally Posted by crampz
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.
-
Feb 20th, 2011, 02:51 AM
#9
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!
-
Feb 20th, 2011, 01:12 PM
#10
Thread Starter
Hyperactive Member
Re: Simulate click on webbrowser
Originally Posted by VBDT
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.
-
Feb 23rd, 2011, 01:43 PM
#11
Junior Member
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?
-
Feb 20th, 2011, 05:27 AM
#12
Hyperactive Member
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|