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 X 

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

devx-admin

Share the Post: