Visual Basic - Technik, FAQ, Tricks, Beispiele

Home / System / Maus u. Kbd. / Click

Mausklick simulieren

Mit folgendem Code ist es möglich, das Click-Ereignis auszulösen:
Private Type POINTAPI
  X As Long
  Y As Long
End Type

Private Declare Function ClientToScreen Lib "user32" ( _
    ByVal hWnd As Long, 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 Declare Function SetCursorPos Lib "user32" ( _
    ByVal X As Long, ByVal Y As Long) As Long

Public Sub MouseClick(Optional ByRef ctl As Control)
  Const MOUSEEVENT_LEFTDOWN = &H2
  Const MOUSEEVENT_LEFTUP = &H4
  Dim Point As POINTAPI
  
  If Not ctl Is Nothing Then
    'Mitte des Controls bestimmen (in Pixel):
    With ctl.Parent
      Point.X = _
          .ScaleX(ctl.Width, vbTwips, vbPixels) \ 2
      Point.Y = _
          .ScaleY(ctl.Height, vbTwips, vbPixels) \ 2
    End With
    
    'Maus positionieren:
    ClientToScreen ctl.hWnd, Point
    SetCursorPos Point.X, Point.Y
  End If
  
  'Klick-Ereignis generieren:
  mouse_event MOUSEEVENT_LEFTDOWN, 0, 0, 0, 0
  DoEvents
  mouse_event MOUSEEVENT_LEFTUP, 0, 0, 0, 0
End Sub
Optional kann der Routine ein Control übergeben werden, worauf die Maus klicken soll.

© Jost Schwider, 01.07.2000-01.07.2000 - http://vb-tec.de/mausclk.htm