Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Visual Basic
Expertise: Advanced
Mar 5, 1999

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 code
Private 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 Sub

Private 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 Sub

Private 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 Sub

Private 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 code
Public 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 Long
Public Declare Function GetWindowLong Lib "user32" Alias _
	"GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex _
	As Long) As Long
Public Declare Function SendMessage Lib "user32" Alias _
	"SendMessageA" (ByVal hwnd As Long, ByVal wMsg As _
	Long, ByVal wParam As Long, lParam As Any) As Long
Public Declare Function SetWindowLong Lib "user32" Alias _
	"SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As _
	Long, ByVal dwNewLong As Long) As Long
Public Const GWL_WNDPROC = (-4)
Public Const GWL_USERDATA = (-21)
Public Const WM_HSCROLL = &H114
Public Const WM_MOUSEMOVE = &H200

Public 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).
Matt Hart
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date