Page 1 of 2
Mouse click!
Posted: Thu Jul 15, 2010 9:41 am
by Noob.exe
How to make cursor click od certain position (just like with mouse)
outside your application...?
I guess this is solution:
http://www.devx.com/vb2themax/Tip/19299
but i really dont know how to use it.
thanks in advance...
Re: Mouse click!
Posted: Thu Jul 15, 2010 12:35 pm
by mandai
That uses the win32 function mouse_event, also it is in VB5/6 so the syntax might be a little different to VB.Net.
SendInput has superseded mouse_event so I'd recommend you use that instead.
The usage is in Button1_Click:
Code: Select all Const MOUSEEVENTF_LEFTDOWN As UInteger = &H2
Const MOUSEEVENTF_LEFTUP As UInteger = &H4
Const MOUSEEVENTF_RIGHTDOWN As UInteger = &H8
Const MOUSEEVENTF_RIGHTUP As UInteger = &H10
Const MOUSEEVENTF_MIDDLEDOWN As UInteger = &H20
Const MOUSEEVENTF_MIDDLEUP As UInteger = &H40
Const MOUSEEVENTF_ABSOLUTE As UInteger = &H8000
Const MOUSEEVENTF_MOVE As UInteger = &H1
Public Structure MOUSEINPUT
Public dx As Integer
Public dy As Integer
Public mouseData As UInteger
Public dwFlags As UInteger
Public time As UInteger
Public dwExtraInfo As IntPtr
End Structure
Public Structure INPUT
Public type As UInteger
Public mi As MOUSEINPUT
End Structure
<DllImport("user32.dll")> Shared Function SendInput(ByVal nInputs As UInteger, ByRef pInputs As INPUT, ByVal cbSize As Integer) As UInteger
End Function
Dim inp As INPUT = New INPUT()
Dim inpsize As Integer = Marshal.SizeOf(inp)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'inp.mi.dwFlags = MOUSEEVENTF_MOVE 'move mouse relative to its current position
'inp.mi.dwFlags = MOUSEEVENTF_ABSOLUTE Or MOUSEEVENTF_MOVE 'move to a set X/Y
'inp.mi.dx = 65535 / (Screen.PrimaryScreen.Bounds.Width / 50) 'this works with absolute positioning x:50, Y:100
'inp.mi.dy = 65535 / (Screen.PrimaryScreen.Bounds.Height / 100)
'inp.mi.dx = 30' this works with relative positioning +X:30, +Y:60
'inp.mi.dy = 60
'inp.mi.dwFlags = MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP 'simulate left click down/up
'inp.mi.dwFlags = MOUSEEVENTF_RIGHTDOWN Or MOUSEEVENTF_RIGHTUP 'simulate right click down/up
'inp.mi.dwFlags = MOUSEEVENTF_MIDDLEDOWN Or MOUSEEVENTF_MIDDLEUP 'simulate middle click down/up
SendInput(1, inp, inpsize)
End Sub
Re: Mouse click!
Posted: Sun Jul 18, 2010 9:15 am
by Noob.exe
Sorry but i still cant get it to work...
could someone make source and post it here? (timer sets position and clicks on it)
thanks
Re: Mouse click!
Posted: Sun Jul 18, 2010 1:17 pm
by mandai
Clicks on what/where?
Code: Select all Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Timer1.Interval = 3000 'delay
inp.mi.dwFlags = MOUSEEVENTF_ABSOLUTE Or MOUSEEVENTF_MOVE
inp.mi.dx = 65535 / (Screen.PrimaryScreen.Bounds.Width / 5)
inp.mi.dy = 65535 / (Screen.PrimaryScreen.Bounds.Height / 10)
SendInput(1, inp, inpsize)
inp.mi.dwFlags = MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP
SendInput(1, inp, inpsize)
End Sub
Re: Mouse click!
Posted: Sun Jul 18, 2010 1:22 pm
by Noob.exe
do i set x,y coordinates here?
Re: Mouse click!
Posted: Sun Jul 18, 2010 1:26 pm
by mandai
Yes
As the comments above say use MOUSEEVENTF_MOVE to move the cursor relative to its current position, and use (MOUSEEVENTF_ABSOLUTE Or MOUSEEVENTF_MOVE) to move the cursor to an absolute position.
Re: Mouse click!
Posted: Mon Jul 19, 2010 8:24 am
by Noob.exe
type DllImport is not defined
name Marshal is not declared
Re: Mouse click!
Posted: Mon Jul 19, 2010 10:52 am
by mandai
Try importing System.Runtime.InteropServices
Re: Mouse click!
Posted: Mon Jul 19, 2010 1:48 pm
by Noob.exe
Thanks, that worked...
but if i set
Code: Select allinp.mi.dx = 25555
inp.mi.dy = 25555
it comes almost in center of the screen and my monitor's resolution is 1280 X 1024
any idea how to convert that inp.mi.dy into... well normal pixels
Re: Mouse click!
Posted: Mon Jul 19, 2010 1:52 pm
by Noob.exe
SOLVED!
inp.mi.dx = (65535 / Screen.PrimaryScreen.Bounds.Width) * pixels
inp.mi.dy = (65535 / Screen.PrimaryScreen.Bounds.Height) * pixels
thanks for all help