Skip to content

Instantly share code, notes, and snippets.

@Benshi
Created January 25, 2023 08:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Benshi/0042cb7b23ca310181bff15c4c3a0042 to your computer and use it in GitHub Desktop.
Save Benshi/0042cb7b23ca310181bff15c4c3a0042 to your computer and use it in GitHub Desktop.
[VB][WinForms] TreeView に Scroll イベントを追加する
Option Strict On
Imports System.ComponentModel
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
'<DefaultEvent(NameOf(TreeViewEx.Scroll))>
<DefaultEvent("Scroll")>
Public Class TreeViewEx
Inherits TreeView
Public Event Scroll As EventHandler(Of ScrollEventArgs)
Protected Overridable Sub OnScroll(se As ScrollEventArgs)
RaiseEvent Scroll(Me, se)
End Sub
Protected Overrides Sub OnKeyDown(e As KeyEventArgs)
MyBase.OnKeyDown(e)
Select Case e.KeyCode
Case Keys.Home, Keys.End, Keys.Up, Keys.Down, Keys.PageUp, Keys.PageDown
Dim t = Me.TopNode
BeginInvoke(New MethodInvoker(Sub() If Me.TopNode IsNot t Then OnWmScroll(Me.Handle, ScrollBarDirection.SB_VERT, Orientation.Vertical)))
End Select
End Sub
Protected Overrides Sub WndProc(ByRef m As Message)
MyBase.WndProc(m)
Const WM_HSCROLL As Integer = &H114
Const WM_VSCROLL As Integer = &H115
Const WM_MOUSEWHEEL As Integer = &H20A
Const WM_MOUSEHWHEEL As Integer = &H20E
Select Case m.Msg
Case WM_VSCROLL
OnWmScroll(If(m.LParam = IntPtr.Zero, Me.Handle, m.LParam), ScrollBarDirection.SB_VERT, Orientation.Vertical)
Case WM_HSCROLL
OnWmScroll(If(m.LParam = IntPtr.Zero, Me.Handle, m.LParam), ScrollBarDirection.SB_HORZ, Orientation.Horizontal)
Case WM_MOUSEWHEEL
OnWmScroll(Me.Handle, ScrollBarDirection.SB_VERT, Orientation.Vertical)
Case WM_MOUSEHWHEEL
OnWmScroll(Me.Handle, ScrollBarDirection.SB_HORZ, Orientation.Horizontal)
End Select
End Sub
Private Sub OnWmScroll(hwnd As IntPtr, sb As ScrollBarDirection, vh As Orientation)
Dim si As New SCROLLINFO()
GetScrollInfo(hwnd, sb, si)
OnScroll(ScrollEventArgs.Create(vh, si))
End Sub
<DefaultValue("Position")>
Public Class ScrollEventArgs
Inherits EventArgs
Public Property Orientation As System.Windows.Forms.Orientation
Public Property Page As UInteger
Public Property Position As Integer
Public Property TrackPosition As Integer
Public Property Minimum As Integer
Public Property Maximum As Integer
Friend Shared Function Create(vh As System.Windows.Forms.Orientation, si As SCROLLINFO) As ScrollEventArgs
Dim se As New ScrollEventArgs()
se.Orientation = vh
se.Page = si.nPage
se.Position = si.nPos
se.TrackPosition = si.nTrackPos
se.Minimum = si.nMin
se.Maximum = si.nMax
Return se
Dim p As SplitContainer
p.Orientation = Orientation.Horizontal
End Function
End Class
Private Declare Function GetScrollInfo Lib "user32" (hwnd As IntPtr, fnBar As ScrollBarDirection, lpsi As SCROLLINFO) As Boolean
<StructLayout(LayoutKind.Sequential)>
Friend Class SCROLLINFO
Public cbSize As UInteger = CUInt(Marshal.SizeOf(GetType(SCROLLINFO)))
Public fMask As ScrollInfoMask = ScrollInfoMask.SIF_ALL
Public nMin As Integer
Public nMax As Integer
Public nPage As UInteger
Public nPos As Integer
Public nTrackPos As Integer
End Class
Private Enum ScrollBarDirection As Integer
SB_HORZ = 0
SB_VERT = 1
SB_CTL = 2
SB_BOTH = 3
End Enum
<Flags>
Friend Enum ScrollInfoMask As UInteger
SIF_RANGE = &H1
SIF_PAGE = &H2
SIF_POS = &H4
SIF_DISABLENOSCROLL = &H8
SIF_TRACKPOS = &H10
SIF_ALL = SIF_RANGE Or SIF_PAGE Or SIF_POS Or SIF_TRACKPOS
End Enum
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment