chitika

Friday, September 16, 2011

vb: how to move cursor and click where cursor is?

This site provides users with the information about vb, visual basic, vb6, move cursor and click it, how to move cursor and click where cursor is, example, and more.

If you think that this site is helpful, please recommend your friends to visit our site.


How to move cursor and click it in vb?

The follow code is very useful when you want to write a automation program to move a cursor to where the textbox is and use Sendkeys to add some text to it or move the cursor to where the button is and click the button. The following are steps to move cursor and click where the cursor is in vb:

1. Add the following code in declare section:


--------------------------------------------------------------------------------
'declare for moving cursor position
Private Declare Function SetCursorPos Lib "user32" _
(ByVal x As Long, ByVal y As Long) As Long

'declare for clicking where the cursor is
Private Declare Sub mouse_event Lib "user32" _
(ByVal dwFlags As Long, _
ByVal dx As Long, _
ByVal dy As Long, _
ByVal cButtons As Long, _
ByVal dwExtraInfo As Long)

Private Declare Function GetCursorPos Lib "user32" _
(lpPoint As POINT) As Long

Private Type POINT
x As Long
y As Long
End Type

Private Const MOUSEEVENTF_ABSOLUTE = &H8000 'absolute move
Private Const MOUSEEVENTF_LEFTDOWN = &H2 'left button down
Private Const MOUSEEVENTF_LEFTUP = &H4 'left button up
Private Const MOUSEEVENTF_MIDDLEDOWN = &H20 'middle button down
Private Const MOUSEEVENTF_MIDDLEUP = &H40 'middle button up
Private Const MOUSEEVENTF_RIGHTDOWN = &H8 'right button down
Private Const MOUSEEVENTF_RIGHTUP = &H10 'right button up

Dim p As POINT
--------------------------------------------------------------------------------

2. Add the following code in a button procedure:

--------------------------------------------------------------------------------
Private Sub cmdOK_Click()
'move cursor
Call SetCursorPos(400, 300)

'find cursor position
Call GetCursorPos(p)

'mouse click
Call mouse_event(MOUSEEVENTF_LEFTDOWN, p.x, p.y, 0&, 0&)
Call mouse_event(MOUSEEVENTF_LEFTUP, p.x, p.y, 0&, 0&)

End Sub

No comments:

Post a Comment

For other hundreds of computer repair tips, tricks, tweaks, guide, help, please go to


http://website.edusoftmax.com