Public Class MyTreeView
Inherits TreeView
' Event declaration
Public Delegate Sub ScrollEventHandler(ByVal sender As Object, ByVal e As ScrollEventArgs)
Public Event Scroll As ScrollEventHandler
Private mLastTop As TreeNode
' WM_VSCROLL message constants
Private Const WM_VSCROLL As Integer = &H115
Private Const WM_MOUSEWHEEL As Int32 = &H20A
Private Const SB_THUMBTRACK As Integer = 5
Private Const SB_ENDSCROLL As Integer = 8
Protected Overrides Sub WndProc(ByRef m As Message)
' Trap the WM_VSCROLL message to generate the Scroll event
MyBase.WndProc(m)
If ScrollEvent IsNot Nothing AndAlso m.Msg = WM_VSCROLL AndAlso Me.TopNode IsNot mLastTop Then
Dim nfy As Integer = m.WParam.ToInt32() And &HFFFF
If nfy = SB_THUMBTRACK OrElse nfy = SB_ENDSCROLL Then
ScrollEvent.Invoke(Me, New ScrollEventArgs(Me.TopNode, nfy = SB_THUMBTRACK))
mLastTop = Me.TopNode
End If
ElseIf m.Msg = WM_MOUSEWHEEL AndAlso TopNode IsNot mLastTop Then
RaiseEvent Scroll(Me, New ScrollEventArgs(TopNode, False))
mLastTop = TopNode
End If
End Sub
Public Class ScrollEventArgs
' Scroll event argument
Private mTop As TreeNode
Private mTracking As Boolean
Public Sub New(ByVal top As TreeNode, ByVal tracking As Boolean)
mTop = top
mTracking = tracking
End Sub
Public ReadOnly Property Top() As TreeNode
Get
Return mTop
End Get
End Property
Public ReadOnly Property Tracking() As Boolean
Get
Return mTracking
End Get
End Property
End Class
End Class