1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
Imports System.Runtime.InteropServices
'pour faciliter le test ajouter dans cet ordre:
'-textbox avec dock=top
'-picturebox avec dock=fill
'-button avec dock=bottom
Public Class frmPicClick
Const MOUSEEVENTF_MOVE As Int32 = &H1 ' mouse move
Const MOUSEEVENTF_LEFTDOWN As Int32 = &H2 ' left button down
Const MOUSEEVENTF_LEFTUP As Int32 = &H4 ' left button up
Const MOUSEEVENTF_RIGHTDOWN As Int32 = &H8 ' right button down
Const MOUSEEVENTF_RIGHTUP As Int32 = &H10 ' right button up
Const MOUSEEVENTF_MIDDLEDOWN As Int32 = &H20 ' middle button down
Const MOUSEEVENTF_MIDDLEUP As Int32 = &H40 ' middle button up
Const MOUSEEVENTF_ABSOLUTE As Int32 = &H8000 ' absolute move
Const MOUSEEVENTF_WHEEL As Int32 = &H800 ' wheel button rolled
' Simule movemement souris au centre du PictureBox & differents cliquages.
Dim cur_x As Single
Dim cur_y As Single
Dim dest_x As Single
Dim dest_y As Single
Dim dx As Single = 500 ' increment du deplacement
Dim dy As Single = 500
Dim factW, FactH As Single 'mise à l'echelle aux coords systemes
Public Sub New()
' Cet appel est requis par le concepteur.
InitializeComponent()
' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
factW = 65535 / Screen.PrimaryScreen.Bounds.Height
FactH = 65535 / Screen.PrimaryScreen.Bounds.Height
End Sub
Private Sub Button1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Button1.Click
txtResults.Clear()
' Mouse_Event se deplace en coords systeme ou
' (0, 0) =>top-left
' (65535,65535) bottom -right
' Calcul coords courant du mouse & conversion en coord systeme..
cur_x = Cursor.Position.X * factW
cur_y = Cursor.Position.Y * FactH
' Convertit coords du centre du PictureBox en coords systeme.
Dim pt As Point = picClicker.PointToScreen(New Point(picClicker.ClientRectangle.Width / 2, picClicker.ClientRectangle.Height / 2))
dest_x = pt.X * factW
dest_y = pt.Y * FactH
'Affiche ces coords
txtResults.Text = txtResults.Text & "From " & Cursor.Position.X & " " & Cursor.Position.Y & " to " & pt.X & " " & pt.Y & vbCrLf
txtResults.Text = txtResults.Text & "From " & cur_x & " " & cur_y & " to " & dest_x & " " & dest_y & vbCrLf
' Deplace la souris vers ces coords(centre du picturebox) et les "clicque" la souris.
' la mise des Flags veut dire on demande:
' MOUSEEVENTF_ABSOLUTE => coords absolues (cad system)
' MOUSEEVENTF_MOVE => deplacer la souris
' MOUSEEVENTF_LEFTDOWN => generer un mousedown(le click est genere aussi ici)
' MOUSEEVENTF_LEFTUP => generer un mouseup
mouse_event(MOUSEEVENTF_ABSOLUTE + MOUSEEVENTF_MOVE + MOUSEEVENTF_LEFTDOWN + MOUSEEVENTF_LEFTUP, dest_x, dest_y, 0, 0)
End Sub
Private Sub picClicker_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles picClicker.Click
txtResults.Text = txtResults.Text & "MouseClick" & vbCrLf
End Sub
Private Sub picClicker_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picClicker.MouseDown
txtResults.Text = txtResults.Text & "MouseDown (" & e.X & ", " & e.Y & ")" & vbCrLf
End Sub
Private Sub picClicker_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picClicker.MouseUp
txtResults.Text = txtResults.Text & "MouseUp (" & e.X & ", " & e.Y & ")" & vbCrLf
End Sub
<DllImport("user32.dll", EntryPoint:="mouse_event", SetLastError:=True, _
CharSet:=CharSet.Unicode, ExactSpelling:=True,
CallingConvention:=CallingConvention.StdCall)>
Public Shared Sub mouse_event(ByVal dwFlags As Integer,
ByVal dx As Integer,
ByVal dy As Integer,
ByVal cButtons As Integer,
ByVal dwExtraInfo As IntPtr)
End Sub
End Class |
Partager