How to move the mouse around in vb.net

G

Glenn Palomar

Hi,

I'm trying to use the following user32 API's in vb.net:
Public 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)

Public Declare Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y
As Long) As Long

When I use the mouse_event to move the mouse to another location, it does
not seem to work correctly. However in VB6, the above APIs work perfectly.

For example, I do the following:

SetCursorPos(640, 512)

Mouse_Event(MOUSEEVENTF_MOVE, 100, 100, 0, 0)

What happens is that instead of moving to location (740, 612) the mouse goes
to location (640, 612). The dx argument somehow is not understood.



Can anyone tell me what else I can do? Or is there an equivalent .net
function that is available to do the same thing instead of using user32 API.

Thanks for the help,

Glenn
 
H

Herfried K. Wagner [MVP]

Glenn Palomar said:
I'm trying to use the following user32 API's in vb.net:
Public 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)

Public Declare Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal
Y As Long) As Long

The declarations are invalid for VB.NET. Use these instead:

\\\
Private Declare Sub mouse_event Lib "user32.dll" ( _
ByVal dwFlags As Int32, _
ByVal dx As Int32, _
ByVal dy As Int32, _
ByVal cButtons As Int32, _
ByVal dwExtraInfo As Int32 _
)

Private Declare Function SetCursorPos Lib "user32.dll" ( _
ByVal X As Int32, _
ByVal Y As Int32 _
) As Boolean
///

Note that 'Cursor.Position' can be used in VB.NET instead of 'SetCursorPos'.
'Long' must be changed to 'Int32' in constant declarations too.
 
G

Glenn Palomar

Thanks. Now, it's working fine... Glenn

Herfried K. Wagner said:
The declarations are invalid for VB.NET. Use these instead:

\\\
Private Declare Sub mouse_event Lib "user32.dll" ( _
ByVal dwFlags As Int32, _
ByVal dx As Int32, _
ByVal dy As Int32, _
ByVal cButtons As Int32, _
ByVal dwExtraInfo As Int32 _
)

Private Declare Function SetCursorPos Lib "user32.dll" ( _
ByVal X As Int32, _
ByVal Y As Int32 _
) As Boolean
///

Note that 'Cursor.Position' can be used in VB.NET instead of
'SetCursorPos'. 'Long' must be changed to 'Int32' in constant declarations
too.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top