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).

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as