OVH Cloud OVH Cloud

Générer un clic souris

4 réponses
Avatar
mimi
Bonjour =E0 tous,

Quelqu'un pourrait-il m'indiquer le moyen de g=E9n=E9rer un=20
clic souris =E0 partir du code VB, comme par exemple lorsque=20
l'on utilise la commande "Sendkeys" pour envoyer une=20
touche clavier =E0 une application.

Merci et bonne journ=E9e
Mimi

4 réponses

Avatar
François Picalausa
Bonjour/soir,

c'est faisable à l'aide de l'API mouse_event.
Voici un exemple:
Option Explicit

Private Const MOUSEEVENTF_LEFTDOWN = &H2 ' left button down
Private Const MOUSEEVENTF_LEFTUP = &H4 ' left button up
Private Const MOUSEEVENTF_ABSOLUTE = &H8000
Private Const MOUSEEVENTF_MOVE = &H1

Private Type POINTAPI
X As Long
Y As Long
End Type

Private Declare Function GetCursorPos _
Lib "user32" _
( _
lpPoint As POINTAPI _
) _
As Long
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 Sub DoClick()
Dim ptaCursorPos As POINTAPI

GetCursorPos ptaCursorPos
mouse_event MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_ABSOLUTE, _
ptaCursorPos.X, _
ptaCursorPos.Y, _
0&, _
0&
mouse_event MOUSEEVENTF_LEFTUP Or MOUSEEVENTF_ABSOLUTE, _
ptaCursorPos.X, _
ptaCursorPos.Y, _
0&, _
0&
End Sub

Private Sub DoMoveRelative(X As Long, Y As Long)
mouse_event MOUSEEVENTF_MOVE, _
X, _
Y, _
0&, _
0&
End Sub

Private Sub DoMoveAbsolute(X As Long, Y As Long)
'Absolute data is specified as the mouse's actual x-coordinate;
'relative data is specified as the number of mickeys moved.
'A mickey is the amount that a mouse has to move for it to report that
it has moved.
mouse_event MOUSEEVENTF_MOVE Or MOUSEEVENTF_ABSOLUTE, _
X, _
Y, _
0&, _
0&
End Sub

Private Sub Command1_Click()
'Déplace le curseur de 50,50
DoMoveRelative 50, 50
'Click
DoClick
End Sub

--
François Picalausa (MVP VB)
FAQ VB : http://faq.vb.free.fr
MSDN : http://msdn.microsoft.com


"mimi" a écrit dans le message de
news:330e01c3fd45$08531820$
Bonjour à tous,

Quelqu'un pourrait-il m'indiquer le moyen de générer un
clic souris à partir du code VB, comme par exemple lorsque
l'on utilise la commande "Sendkeys" pour envoyer une
touche clavier à une application.

Merci et bonne journée
Mimi


Avatar
Mimi
Merci beaucoup François (si je peux me permettre)

Cela répond très bien à mon problème.

Cordialement
Mimi.

-----Message d'origine-----
Bonjour/soir,

c'est faisable à l'aide de l'API mouse_event.



--
François Picalausa (MVP VB)
FAQ VB : http://faq.vb.free.fr
MSDN : http://msdn.microsoft.com


"mimi" a écrit dans le message


de
news:330e01c3fd45$08531820$
Bonjour à tous,

Quelqu'un pourrait-il m'indiquer le moyen de générer un
clic souris à partir du code VB, comme par exemple




lorsque
l'on utilise la commande "Sendkeys" pour envoyer une
touche clavier à une application.

Merci et bonne journée
Mimi




.



Avatar
Yves Boyer
Eh ben, dis donc François, t'as accouché d'une souris !!! :-)

Yves Boyer
Avatar
François Picalausa
Hello!


"Yves Boyer" a écrit dans le message de
news:O1I33xV$
Eh ben, dis donc François, t'as accouché d'une souris !!! :-)



Même de mickey mouse...
Pour ceux qui voudraient utiliser des coordonnées absolues avec ces fameux
mickeys, voici un exemple de code:
Option Explicit

Private Const MOUSEEVENTF_ABSOLUTE = &H8000
Private Const MOUSEEVENTF_MOVE = &H1

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 GetDeviceCaps _
Lib "gdi32" _
( _
ByVal hdc As Long, _
ByVal nIndex As Long _
) _
As Long

Const HORZRES = 8
Const VERTRES = 10

Private Sub DoMoveAbsolute(X As Long, Y As Long)
'Absolute data is specified as the mouse's actual x-coordinate;
'relative data is specified as the number of mickeys moved.
'A mickey is the amount that a mouse has to move for it to report that
'it has moved.
mouse_event MOUSEEVENTF_MOVE Or MOUSEEVENTF_ABSOLUTE, _
X, _
Y, _
0&, _
0&
End Sub

Private Sub Command1_Click()
'If MOUSEEVENTF_ABSOLUTE value is specified, dx and dy contain
normalized
'absolute coordinates between 0 and 65,535. The event procedure maps
these
'coordinates onto the display surface. Coordinate (0,0) maps onto the
upper-left
'corner of the display surface, (65535,65535) maps onto the lower-right
corner.
Const COORDINATES_MAX As Long = 65535

'On va tenter de positionner la souris en 100,50 (pixels)
Const PosX As Long = 100
Const PosY As Long = 50

Dim MickeysX As Long
Dim MickeysY As Long

'http://support.microsoft.com/default.aspx?kbid%3940
MickeysX = PosX / GetDeviceCaps(Me.hdc, HORZRES) * COORDINATES_MAX
MickeysY = PosY / GetDeviceCaps(Me.hdc, VERTRES) * COORDINATES_MAX

DoMoveAbsolute MickeysX, MickeysY
End Sub

Celà ne devrait pas onctionner si a définit une accélération du curseur.
Pour plus d'informations, voir les remarques de la fiche sur mouse_event:
http://msdn.microsoft.com/library/en-us/winui/winui/windowsuserinterface/userinput/mouseinput/mouseinputreference/mouseinputfunctions/mouse_event.asp

Mais faire se déplacer le curseur pour clicker est aussi précis qu'un
sendkeys...
Je ne recommande donc pas cette méthode (sauf si on ne peut pas faire
autrement.. mais on peut toujours faire autrement.. récupérer le hwnd et
envoyer un WM_CLICK par exemple).

--
François Picalausa (MVP VB)
FAQ VB : http://faq.vb.free.fr
MSDN : http://msdn.microsoft.com