Rubrik: Maus & Tastatur · Maus | VB-Versionen: VB4, VB5, VB6 | 18.04.03 |
MouseIn/MouseOut für Form-Objekte Dieser Tipp zeigt, wie sich ermitteln lässt, ob sich der Mauszeiger aktuell innerhalb der Form oder außerhalb der Form befindet. | ||
Autor: Dieter Otter | Bewertung: | Views: 16.848 |
www.tools4vb.de | System: Win9x, WinNT, Win2k, WinXP, Win7, Win8, Win10, Win11 | Beispielprojekt auf CD |
Nachfolgender Tipp zeigt, wie sich ermitteln lässt, ob sich der Mauszeiger aktuell innerhalb oder außerhalb der Form befindet. Hierzu benötigen wir zunächst einen Timer mit niedrigem Intervall. Über die API-Funktion GetCursorPos ermitteln wir dann die aktuelle Mauszeiger-Position. Um nun zu prüfen, ob sich der Mauszeiger innerhalb der Form befindet, verwenden wir die API-Funktion PtInRect.
Erstellen Sie ein neues Projekt und platzieren Sie auf die Form ein Timer-Control, sowie ein Label-Control.
Option Explicit ' Benötigte API-Deklarationen Private Declare Function GetCursorPos Lib "user32" ( _ lpPoint As POINTAPI) As Long Private Declare Function PtInRect Lib "user32" ( _ lpRect As RECT, _ ByVal x As Long, _ ByVal y As Long) As Long Private Declare Function GetWindowRect Lib "user32" ( _ ByVal hwnd As Long, _ lpRect As RECT) As Long Private Type POINTAPI x As Long y As Long End Type Private Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type
Private Sub Form_Load() ' Timer initialisieren Timer1.Interval = 50 Timer1.Enabled = True End Sub
Private Sub Timer1_Timer() Dim P As POINTAPI Dim R As RECT ' akuelle Mauszeiger-Position ermitteln GetCursorPos P ' Position und Größe der Form GetWindowRect Me.hwnd, R ' Befindet sich der Mauszeiger innerhalb der Form? If PtInRect(R, P.x, P.y) = 0 Then Label1.Caption = "Maus außerhalb der Form!" Else Label1.Caption = "Maus innerhalb der Form!" End If End Sub