devxlogo

Provide a Horizontal Scroll Event for a Listbox

Provide a Horizontal Scroll Event for a Listbox

Subclassing a listbox allows you to monitor horizontal scrolling. To subclass a listbox, you store the original WndProc in the UserData area of the listbox, allowing a single replacement of WndProc to work for all ListBox controls. WndProc notifies your form of a horizontal scroll message by sending a WM_MOUSEMOVE message with negative coordinates. The MouseMove event receives negative X and Y values, plus a Button value when horizontally scrolled, which is impossible under normal operation. Be sure to restore the original WndProc in the Form_Unload event:

 '--- Form codePrivate Sub Form_Load()	SetWindowLong List1.hwnd, GWL_USERDATA, _		SetWindowLong(List1.hwnd,GWL_WNDPROC, _		AddressOf WndProc)	SetWindowLong List2.hwnd, GWL_USERDATA, _		SetWindowLong(List2.hwnd, GWL_WNDPROC, _		AddressOf WndProc)End SubPrivate Sub Form_Unload(Cancel As Integer)	SetWindowLong List1.hwnd, GWL_WNDPROC, _		GetWindowLong(List1.hwnd, GWL_USERDATA)	SetWindowLong List2.hwnd, GWL_WNDPROC, _		GetWindowLong(List2.hwnd, GWL_USERDATA)End SubPrivate Sub List1_MouseMove(Button As Integer, Shift _	As Integer, X As Single, Y As Single)	If Button > 0 And X < 0 And Y < 0 Then Debug.Print _		"List1 Horizontal Scroll"End SubPrivate Sub List2_MouseMove(Button As Integer, Shift _	As Integer, X As Single, Y As Single)	If Button > 0 And X < 0 And Y < 0 Then Debug.Print _		"List2 Horizontal Scroll"End Sub'--- Module codePublic Declare Function CallWindowProc Lib "user32" Alias _	"CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal _	hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, _	ByVal lParam As Long) As LongPublic Declare Function GetWindowLong Lib "user32" Alias _	"GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex _	As Long) As LongPublic Declare Function SendMessage Lib "user32" Alias _	"SendMessageA" (ByVal hwnd As Long, ByVal wMsg As _	Long, ByVal wParam As Long, lParam As Any) As LongPublic Declare Function SetWindowLong Lib "user32" Alias _	"SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As _	Long, ByVal dwNewLong As Long) As LongPublic Const GWL_WNDPROC = (-4)Public Const GWL_USERDATA = (-21)Public Const WM_HSCROLL = &H114Public Const WM_MOUSEMOVE = &H200Public Function WndProc(ByVal hwnd As Long, ByVal Msg As _	Long, ByVal wParam As Long, ByVal lParam As Long) _	As Long	If Msg = WM_HSCROLL Then SendMessage hwnd, _		WM_MOUSEMOVE, 1, ByVal &HFFFF		WndProc = CallWindowProc(GetWindowLong(hwnd, _			GWL_USERDATA), hwnd, Msg, wParam, lParam)End Function

For brevity, this example omits the code that would add horizontal scrollbars to the List1 and List2 controls, but is readily available in the Knowledge Base (Q192184).

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist