Click/doubleclick at a certain point on the screen through code

I was wondering if anyone knew how to write code that clicked/doubleclicked your mouse at a specific point on the screen.

Chris
khoocAsked:
Who is Participating?
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
You've got it backwards lee_ingram.  Hooks would be used to track the mouse movements/clicks no matter where they occur.

Here is an example of left clicking at a specific point:

Public Class Form1
    Inherits System.Windows.Forms.Form

    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)

    Public Const MOUSEEVENTF_LEFTDOWN As Integer = &H2
    Public Const MOUSEEVENTF_LEFTUP As Integer = &H4
    Public Const MOUSEEVENTF_MIDDLEDOWN As Integer = &H20
    Public Const MOUSEEVENTF_MIDDLEUP As Integer = &H40
    Public Const MOUSEEVENTF_RIGHTDOWN As Integer = &H8
    Public Const MOUSEEVENTF_RIGHTUP As Integer = &H10

    Private Sub LeftClick(ByVal x As Integer, ByVal y As Integer)
        Cursor.Position = New Point(x, y)
        mouse_event(MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
    End Sub

End Class


0
 
lee_ingramCommented:
what are you trying to do or achive? if i know this i can help your further
0
 
khoocAuthor Commented:
Hi Lee

I'm basically wanting to control the mouse, click/doubleclick/drag at certain parts of the screen.

Do you know how to do this?

Chris
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
lee_ingramCommented:
are you clicking or moving within your application or someone elses
0
 
khoocAuthor Commented:
oh - anywhere.. it doesn't matter what app you're in...

basically, i want to tell the mouse to click at a certain position of the screen

I'm basically looking to create a tool for UI testing purposes

Chris
0
 
lee_ingramConnect With a Mentor Commented:

OK,

The only way to do this as far a i know is to use mouse hooks, basically the same as a keyboard hook.

check out : http://support.microsoft.com/?kbid=318804
and http://www.codeproject.com/csharp/NetWin32Hooks.asp

both contain example code.  This should resolve your issue.

another source for a vb.net example is

http://www.vbcity.com/forums/faq.asp?fid=15&cat=Shell

hope this answers your question.

Lee
0
 
lee_ingramCommented:
Idle_Mind you need to read all the comments before jumping in mate ;-)

This is actually what he wanted.  He wanted to beable to recieve mouse events not only for his application but for other applications aswell. He also want to move the mouse etc not just recieve clicks and location.

QUOTE:

"oh - anywhere.. it doesn't matter what app you're in...

basically, i want to tell the mouse to click at a certain position of the screen

I'm basically looking to create a tool for UI testing purposes"
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I have to disagree...

My interpretation of his statements:

    "oh - anywhere.. it doesn't matter what app you're in...
    basically, i want to tell the mouse to click at a certain position of the screen
    I'm basically looking to create a tool for UI testing purposes"

Is that he wants to be able to programmatically move and/or click the mouse anywhere on the screen, regardless of whether that point is within his application or not, for the purpose of testing some application at that point.

From the second line, he wants to "tell" (or "control" from his previous post) the mouse to move/click at a "certain position of the screen".  These are active words...which to me says he doesn't want to receive mouse events, but instead wants to send them.

I'm sure khooc will let us know what interpretation is correct.  Let's wait and see...
0
 
khoocAuthor Commented:
Hi guys

Apologies for the miscommunication... I'll go to my English class now..

I was actually after Idle_Mind's answer, but Lee - your answer is also very useful :-) so points to both.

Chris
0
 
khoocAuthor Commented:
...
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.