Option Explicit
Private Type POINTAPI
X As Long
Y As Long
End Type
Private Declare PtrSafe Function WindowFromPoint Lib "user32" (ByVal Point As LongPtr) As LongPtr
#Else
Private Declare PtrSafe Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As LongPtr
Private Declare PtrSafe Function IUnknown_GetWindow Lib "shlwapi" Alias "#172" (ByVal pIUnk As IUnknown, ByVal hWnd As LongPtr) As Long
Private Declare PtrSafe Function GetWindow Lib "user32" (ByVal hWnd As LongPtr, ByVal wCmd As Long) As LongPtr
Private Declare PtrSafe Function SetFocus Lib "user32" (ByVal hWnd As LongPtr) As Long
Private Declare PtrSafe Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare PtrSafe Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As LongPtr)
Private TargetHwnd As LongPtr
#Else
Private Enum LongPtr
[_]
End Enum
Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function IUnknown_GetWindow Lib "shlwapi" Alias "#172" (ByVal pIUnk As IUnknown, ByVal hWnd As LongPtr) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hWnd As LongPtr, ByVal wCmd As Long) As LongPtr
Private Declare Function SetFocus Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private TargetHwnd As Long
Private WithEvents IC As MSINKAUTLib.InkCollector
Private TargetControl As MSForms.Control
Private Function PointApiToLong(Point As POINTAPI) As LongLong
Dim DbLL As LongLong, StructLong As LongPtr
StructLong = LenB(DbLL)
If LenB(Point) = StructLong Then CopyMemory DbLL, Point, StructLong
PointApiToLong = DbLL
End Function
Private Sub UserForm_Initialize()
Label1.Picture = New StdPicture
Me.TextBox1.SelStart = 0
ListBox1.List = Evaluate("row(1:30)")
ComboBox1.List = Evaluate("row(1:30)")
End Sub
Private Sub SetupMouseWheel(Optional HandL = 0)
Set IC = Nothing
If HandL = 0 Then
Dim hWnd As LongPtr, TemphWnd As LongPtr
#Else
Dim hWnd As Long, TemphWnd As Long
Call IUnknown_GetWindow(Me, VarPtr(hWnd))
Const GW_CHILD = 5
TemphWnd = GetWindow(hWnd, GW_CHILD)
Else
TemphWnd = HandL
End If
Set IC = New MSINKAUTLib.InkCollector
With IC
SetFocus TemphWnd
.hWnd = TemphWnd
.SetEventInterest ICEI_MouseWheel, True
.MousePointer = IMP_Arrow
.DynamicRendering = False
.DefaultDrawingAttributes.Transparency = 255
.Enabled = True
End With
End Sub
Private Sub Label1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Sheets("Feuil1").Range("B2") = "Label1"
Sheets("Feuil1").Range("B3") = GetWindowFromCurs()
If Not Label1 Is TargetControl Then
Set TargetControl = Label1
End If
If Not GetWindowFromCurs() = TargetHwnd Then
TargetHwnd = GetWindowFromCurs()
SetupMouseWheel TargetHwnd
End If
End Sub
Private Sub TextBox1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Sheets("Feuil1").Range("B2") = "TextBox1"
Sheets("Feuil1").Range("B3") = GetWindowFromCurs()
TextBox1.SetFocus
If Not TextBox1 Is TargetControl Then
Set TargetControl = TextBox1
End If
TargetControl.SetFocus
If Not GetWindowFromCurs() = TargetHwnd Then
TargetHwnd = GetWindowFromCurs()
SetupMouseWheel TargetHwnd
End If
End Sub
Private Sub ListBox1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Sheets("Feuil1").Range("B2") = "ListBox1"
Sheets("Feuil1").Range("B3") = GetWindowFromCurs()
If Not ListBox1 Is TargetControl Then
Set TargetControl = ListBox1
End If
If Not GetWindowFromCurs() = TargetHwnd Then
TargetHwnd = GetWindowFromCurs()
SetupMouseWheel TargetHwnd
End If
End Sub
Private Sub Frame1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Sheets("Feuil1").Range("B2") = "Frame1"
Sheets("Feuil1").Range("B3") = GetWindowFromCurs()
If Not Frame1 Is TargetControl Then
Set TargetControl = Frame1
End If
If Not GetWindowFromCurs() = TargetHwnd Then
TargetHwnd = GetWindowFromCurs()
SetupMouseWheel TargetHwnd
End If
End Sub
Private Sub MultiPage1_MouseMove(ByVal Index As Long, ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Sheets("Feuil1").Range("B2") = "MultiPage1"
Sheets("Feuil1").Range("B3") = GetWindowFromCurs()
If Not MultiPage1 Is TargetControl Then
Set TargetControl = MultiPage1
End If
If Not GetWindowFromCurs() = TargetHwnd Then
TargetHwnd = GetWindowFromCurs()
SetupMouseWheel TargetHwnd
End If
End Sub
Private Sub ComboBox1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Sheets("Feuil1").Range("B2") = "ComboBox1"
Sheets("Feuil1").Range("B3") = GetWindowFromCurs()
If Not ComboBox1 Is TargetControl Then
Set TargetControl = ComboBox1
End If
If Not GetWindowFromCurs() = TargetHwnd Then
TargetHwnd = GetWindowFromCurs()
Debug.Print "Combo"
SetupMouseWheel TargetHwnd
End If
End Sub
Private Sub IC_MouseWheel(ByVal Button As MSINKAUTLib.InkMouseButton, ByVal Shift As MSINKAUTLib.InkShiftKeyModifierFlags, ByVal Delta As Long, ByVal X As Long, ByVal Y As Long, Cancel As Boolean)
Dim ctrl As Control
Set ctrl = TargetControl
Select Case TypeName(TargetControl)
Case "Label"
CallByName TargetControl, "Caption", VbLet, "Delta: " & Delta
Case "TextBox"
Dim CurrentLine As Long
CurrentLine = CallByName(TargetControl, "CurLine", VbGet)
If CurrentLine = TextBox1.LineCount - 1 And Delta < 0 Then Exit Sub
If CurrentLine = 0 And Delta > 0 Then Exit Sub
CallByName TargetControl, "CurLine", VbLet, IIf(Delta > 0, CurrentLine - 1, CurrentLine + 1)
Case "ListBox"
CallByName TargetControl, "TopIndex", VbLet, IIf(Delta > 0, ctrl.TopIndex - 1, ctrl.TopIndex + 1)
Case "ComboBox"
On Error Resume Next
CallByName TargetControl, "TopIndex", VbLet, IIf(Delta > 0, ctrl.TopIndex - 1, ctrl.TopIndex + 1)
Case "Frame"
CallByName TargetControl, "ScrollTop", VbLet, IIf(Delta > 0, ctrl.ScrollTop - 8, ctrl.ScrollTop + 8)
Case "MultiPage"
CallByName TargetControl.Pages(TargetControl.Value), "ScrollTop", VbLet, _
IIf(Delta > 0, TargetControl.Pages(TargetControl.Value).ScrollTop - 8, TargetControl.Pages(TargetControl.Value).ScrollTop + 8)
End Select
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Set IC = Nothing
End Sub
Private Function GetWindowFromCurs() As LongPtr
#Else
Private Function GetWindowFromCurs() As Long
Dim Pt As POINTAPI
Call GetCursorPos(Pt)
GetWindowFromCurs = WindowFromPoint(PointToLongLong(Pt))
#Else
GetWindowFromCurs = WindowFromPoint(Pt.X, Pt.Y)
End Function