Mouse click!

If you need help with a project or need to know how to do something specific in VB.NET then please ask your questions in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
11 posts Page 1 of 2
Contributors
User avatar
Noob.exe
Top Poster
Top Poster
Posts: 233
Joined: Wed Mar 31, 2010 6:42 pm

Mouse click!
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...
<3 VB & Python
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: Mouse click!
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
Last edited by mandai on Thu Jan 26, 2012 11:51 pm, edited 2 times in total.
User avatar
Noob.exe
Top Poster
Top Poster
Posts: 233
Joined: Wed Mar 31, 2010 6:42 pm

Re: Mouse click!
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
<3 VB & Python
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: Mouse click!
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
Last edited by mandai on Thu Jan 26, 2012 11:54 pm, edited 2 times in total.
User avatar
Noob.exe
Top Poster
Top Poster
Posts: 233
Joined: Wed Mar 31, 2010 6:42 pm

Re: Mouse click!
Noob.exe
Code: Select all
inp.mi.dx =
inp.mi.dy =
do i set x,y coordinates here?
<3 VB & Python
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: Mouse click!
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.
User avatar
Noob.exe
Top Poster
Top Poster
Posts: 233
Joined: Wed Mar 31, 2010 6:42 pm

Re: Mouse click!
Noob.exe
type DllImport is not defined
name Marshal is not declared
<3 VB & Python
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: Mouse click!
mandai
Try importing System.Runtime.InteropServices
User avatar
Noob.exe
Top Poster
Top Poster
Posts: 233
Joined: Wed Mar 31, 2010 6:42 pm

Re: Mouse click!
Noob.exe
Thanks, that worked...
but if i set
Code: Select all
inp.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
<3 VB & Python
User avatar
Noob.exe
Top Poster
Top Poster
Posts: 233
Joined: Wed Mar 31, 2010 6:42 pm

Re: Mouse click!
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
<3 VB & Python
11 posts Page 1 of 2
Return to “Coding Help & Support”