AW: IF Then Mausabfrage
03.10.2020 12:14:10
volti
Hallo Aton,
da das Thema wohl noch offen ist und sonst niemand antwortet, hier noch ein Versuch:
Wenn es noch so ist wie früher, laufen die Mausaktivitäten über einen Port ein und werden vom Maustreiber entsprechend verarbeitet.
Über Mausadressen im Arbeitsspeicher weiß ich nichts. Ich lasse die Frage deshalb mal offen.
Die von mir anfangs vorgeschlagene Abfrage der Maustasten mittels GetAsyncKeyState funktioniert schon, allerdings werden weiterhin die Mausaktivitäten an Excel gesendet, so dass das dann etwas unsauber funktioniert.
Z.B. müsstest Du auch die Rechtsclick-Routine bei den Tabellenevents abschalten, da die ja Dein Vorhaben zunichte machen.
Hier mal eine neue Idee, die allerdings etwas umfangreicher ist und die Mausaktivitäten in einer eigenen Prozedur abfängt.
Vielleicht sagt sie Dir ja mehr zu. Probiere es einfach mal aus.
Diesen Code hier ein das Tabellenmodul:
Code:
[Cc]
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Mausabfrage
End Sub
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
If gbMausaktion Then Cancel = True
End Sub
Diesen Code hier ein normales Modul:
Code:
[Cc][+][-]
Option Explicit
Declare PtrSafe Function SetWindowsHookEx Lib "user32" _
Alias "SetWindowsHookExA" ( _
ByVal idHook As Long, ByVal lpfn As LongPtr, _
ByVal hmod As LongPtr, ByVal dwThreadId As Long) As LongPtr
Declare PtrSafe Function GetCurrentThreadId Lib "kernel32" () As Long
Declare PtrSafe Function CallNextHookEx Lib "user32" ( _
ByVal hHook As LongPtr, ByVal ncode As Long, _
ByVal wParam As LongPtr, lParam As Any) As LongPtr
Declare PtrSafe Function UnhookWindowsHookEx Lib "user32" ( _
ByVal hHook As LongPtr) As Long
Declare PtrSafe Function GetWindowLongPtr Lib "user32" _
Alias "GetWindowLongA" ( _
ByVal hWnd As LongPtr, ByVal nIndex As Long) As LongPtr
Declare PtrSafe Function GetAsyncKeyState Lib "user32" ( _
ByVal vKey As Long) As Integer
Type POINTAPI
x As Long
y As Long
End Type
Type MOUSEHOOKSTRUCT
PT As POINTAPI
hWnd As LongPtr
wHitTestCode As Long
dwExtraInfo As LongPtr
End Type
Public gbMausaktion As Boolean
Const WM_LBUTTONDOWN = &H201
Const WM_RBUTTONDOWN = &H204
Const WM_LBUTTONUP = &H202
Const WM_RBUTTONUP = &H205
Const HC_ACTION = &H0
Const WH_MOUSE = 7
Const GWL_HINSTANCE = (-6)
Dim hHook As LongPtr
Sub Mausabfrage()
gbMausaktion = True
hHook = SetWindowsHookEx(WH_MOUSE, AddressOf MouseProc, _
GetWindowLongPtr(Application.hWnd, GWL_HINSTANCE), _
GetCurrentThreadId)
Do
DoEvents
If GetAsyncKeyState(27) <> 0 Then Exit Do 'Exit
Loop
gbMausaktion = False
UnhookWindowsHookEx hHook 'UnHook ist wichtig
MsgBox "Aktion beendet!", vbInformation, "Maus..."
End Sub
Private Function MouseProc(ByVal ncode As Long, ByVal wParam As Long, _
lParam As MOUSEHOOKSTRUCT) As LongPtr
On Error GoTo Fehler
Select Case ncode
Case HC_ACTION
If lParam.hWnd > 0 Then
Range("B1").Value = lParam.PT.x 'Mausposition X in Zelle ablegen
Range("C1").Value = lParam.PT.y 'Mausposition Y in Zelle ablegen
Select Case wParam
Case WM_LBUTTONDOWN: Cells(1, 7) = Cells(1, 2)
Case WM_RBUTTONDOWN: Cells(1, 8) = Cells(1, 3)
Case WM_LBUTTONUP, WM_RBUTTONUP
End Select
End If
Case Else
MouseProc = CallNextHookEx(hHook, ncode, wParam, lParam)
End Select
Fehler:
End Function
____________________
viele Grüße aus Freigericht
Karl-Heinz