Kontakt
DSVGO
Historie | |
---|---|
11.04.2003 | Beispiel korrigiert, Artikel umformatiert |
30.04.2000 | Erste Version |
Die folgende Klasse erlaubt es, die diversen Eigenschaften der Maus abzufragen (Position und Tasten) und ggf. auch zu setzen (Position).
'Name: Mouse.cls 'Instancing: 2 - PublicNotCreatable Option Explicit Private Type POINTAPI X As Long Y As Long End Type Private Declare Function GetAsyncKeyState Lib "user32" _ (ByVal vKey As Long) As Integer Private Declare Function GetCursorPos Lib "user32" ( _ lpPoint As POINTAPI) As Long Private Declare Function SetCursorPos Lib "user32" ( _ ByVal X As Long, ByVal Y As Long) As Long Public Property Get Button() As MouseButtonConstants If GetAsyncKeyState(vbLeftButton) Then _ Button = vbLeftButton If GetAsyncKeyState(vbRightButton) Then _ Button = Button + vbRightButton End Property Public Sub Move(ByVal X As Long, ByVal Y As Long) SetCursorPos CLng(X \ Screen.TwipsPerPixelX), _ CLng(Y \ Screen.TwipsPerPixelY) End Sub Public Property Get X() As Long Dim MousePos As POINTAPI GetCursorPos MousePos X = MousePos.X * Screen.TwipsPerPixelX End Property Public Property Let X(ByVal NewX As Long) Dim MousePos As POINTAPI GetCursorPos MousePos SetCursorPos CLng(NewX \ Screen.TwipsPerPixelX), MousePos.Y End Property Public Property Get Y() As Long Dim MousePos As POINTAPI GetCursorPos MousePos Y = MousePos.Y * Screen.TwipsPerPixelY End Property Public Property Let Y(ByVal NewY As Long) Dim MousePos As POINTAPI GetCursorPos MousePos SetCursorPos MousePos.X, CLng(NewY \ Screen.TwipsPerPixelY) End Property
Folgender Code erleichtert den Zugriff auf die Maus wesentlich durch Einrichtung eines globalen Maus-Objekts:
'Name: Global.cls (oder so) 'Instancing: 6 - GlobalMultiUse Option Explicit Private pMouse As Mouse Public Property Get Mouse() As Mouse If pMouse Is Nothing Then Set pMouse = New Mouse Set Mouse = pMouse End Property
Dann kann das globale Maus-Objekt direkt benutzt werden, ohne erst eine Instanz erzeugen zu müssen. Hier ein kleines Beispiel, das bei jedem Klick den Mauscursor ein wenig wandern läßt (mit der linken Maustaste nach rechts, ansonsten nach unten):
Sub Form_Click() If Mouse.Button And vbLeftButton Then Mouse.X = Mouse.X + 100 Else Mouse.Y = Mouse.Y + 100 End If End Sub
© Jost Schwider, 30.04.2000-11.04.2003 - http://vb-tec.de/mousecls.htm