scroll event in listview

Posted on 2007-10-15
Last Modified: 2013-12-20
From my search, I know that the SCROLL EVENT for listview can be achieved through a certain method. But I could not get anything from it because it is quite messy. Could someone please show me how this could be done step by step.
Question by:ee020165
    LVL 48

    Expert Comment


    Author Comment

    I just want it to work for 1 listview. But the code is for 2 listviews which makes things quite messy. Could you try to make it more brief and include the minimum lines of codes to trigger the scroll event in a SINGLE listview. I have used api programs before such as shutting down windows from VB and so on and it was very easy to use. But the codes here really make me at lost.
    LVL 85

    Accepted Solution

    Boiled down...

    ' -------------------------
    '  Form1
    ' -------------------------
    Option Explicit

    Private Sub Form_Load()
        Call WindowHook(ListView1.hwnd)
    End Sub

    Private Sub Form_Unload(Cancel As Integer)
        Call WindowHook(ListView1.hwnd)
    End Sub

    ' -------------------------
    '  Module1
    ' -------------------------
    Option Explicit

    Private 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
    Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

    Private Const GWL_WNDPROC As Long = (-4)
    Private Const WM_VSCROLL = &H115
    Private Const WM_HSCROLL = &H114

    Private lngPrevProc As Long

    Public Sub WindowHook(ByVal lngHandle As Long)
        If lngPrevProc = 0 Then
            lngPrevProc = SetWindowLong(lngHandle, GWL_WNDPROC, AddressOf WindowProc)
            Call SetWindowLong(lngHandle, GWL_WNDPROC, lngPrevProc)
            lngPrevProc = 0
        End If
    End Sub

    Public Function WindowProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
        Select Case uMsg
            Case WM_VSCROLL, WM_HSCROLL
                Debug.Print "ListView was scrolled..."
        End Select
        WindowProc = CallWindowProc(lngPrevProc, hwnd, uMsg, wParam, lParam)
    End Function

    Author Comment

    Idle Mind, you're a genius! The code is incredibly simple. I thought after a few days no one could answer it but I'm wrong. I'll be stuck with ee from now on.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Introduction While answering a recent question ( in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
    This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now