+ Reply to Thread
Results 1 to 6 of 6

Mouse scroll on a listbox

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-03-2016
    Location
    Rio de Janeiro, Brazil
    MS-Off Ver
    2016
    Posts
    125

    Mouse scroll on a listbox

    I'm using this code from Peter Thornton which enables me to scroll in a userform listbox, but every time my mouse goes through the listbox (just moving the cursor to another part of the userform, without even clicking) it "activates" the listbox. Is there any way I can "block" this to happen? I mean, any way that the mouse scroll works only when I click on that down arrow to open the listbox?

    Ps: and I guess it only works for Windows. If this is right, how can I make it to work with Mac's os also?

    This is Peter Thronton's code:
    '''''' normal module code
    
    Option Explicit
    
    Private Type POINTAPI
            X As Long
            Y As Long
    End Type
    
    Private Type MOUSEHOOKSTRUCT
            pt As POINTAPI
            hwnd As Long
            wHitTestCode As Long
            dwExtraInfo As Long
    End Type
    
    Private Declare Function FindWindow Lib "user32" _
                                            Alias "FindWindowA" ( _
                                                            ByVal lpClassName As String, _
                                                            ByVal lpWindowName As String) As Long
    
    Private Declare Function GetWindowLong Lib "user32.dll" _
                                            Alias "GetWindowLongA" ( _
                                                            ByVal hwnd As Long, _
                                                            ByVal nIndex As Long) As Long
    
    Private Declare Function SetWindowsHookEx Lib "user32" _
                                            Alias "SetWindowsHookExA" ( _
                                                            ByVal idHook As Long, _
                                                            ByVal lpfn As Long, _
                                                            ByVal hmod As Long, _
                                                            ByVal dwThreadId As Long) As Long
    
    Private Declare Function CallNextHookEx Lib "user32" ( _
                                                            ByVal hHook As Long, _
                                                            ByVal nCode As Long, _
                                                            ByVal wParam As Long, _
                                                            lParam As Any) As Long
    
    Private Declare Function UnhookWindowsHookEx Lib "user32" ( _
                                                            ByVal hHook As Long) As Long
    
    'Private Declare Function PostMessage Lib "user32.dll" _
    '                                         Alias "PostMessageA" ( _
    '                                                         ByVal hwnd As Long, _
    '                                                         ByVal wMsg As Long, _
    '                                                         ByVal wParam As Long, _
    '                                                         ByVal lParam As Long) As Long
    
    Private Declare Function WindowFromPoint Lib "user32" ( _
                                                            ByVal xPoint As Long, _
                                                            ByVal yPoint As Long) As Long
    
    Private Declare Function GetCursorPos Lib "user32.dll" ( _
                                                            ByRef lpPoint As POINTAPI) As Long
    
    Private Const WH_MOUSE_LL As Long = 14
    Private Const WM_MOUSEWHEEL As Long = &H20A
    Private Const HC_ACTION As Long = 0
    Private Const GWL_HINSTANCE As Long = (-6)
    
    'Private Const WM_KEYDOWN As Long = &H100
    'Private Const WM_KEYUP As Long = &H101
    'Private Const VK_UP As Long = &H26
    'Private Const VK_DOWN As Long = &H28
    'Private Const WM_LBUTTONDOWN As Long = &H201
    
    Private mLngMouseHook As Long
    Private mListBoxHwnd As Long
    Private mbHook As Boolean
    Private mCtl As MSForms.Control
    Dim n As Long
    
    Sub HookListBoxScroll(frm As Object, ctl As MSForms.Control)
    Dim lngAppInst As Long
    Dim hwndUnderCursor As Long
    Dim tPT As POINTAPI
         GetCursorPos tPT
         hwndUnderCursor = WindowFromPoint(tPT.X, tPT.Y)
         If Not frm.ActiveControl Is ctl Then
                 ctl.SetFocus
         End If
         If mListBoxHwnd <> hwndUnderCursor Then
                 UnhookListBoxScroll
                 Set mCtl = ctl
                 mListBoxHwnd = hwndUnderCursor
                 lngAppInst = GetWindowLong(mListBoxHwnd, GWL_HINSTANCE)
                 ' PostMessage mListBoxHwnd, WM_LBUTTONDOWN, 0&, 0&
                 If Not mbHook Then
                         mLngMouseHook = SetWindowsHookEx( _
                                                         WH_MOUSE_LL, AddressOf MouseProc, lngAppInst, 0)
                         mbHook = mLngMouseHook <> 0
                 End If
         End If
    End Sub
    
    Sub UnhookListBoxScroll()
         If mbHook Then
                    Set mCtl = Nothing
                 UnhookWindowsHookEx mLngMouseHook
                 mLngMouseHook = 0
                 mListBoxHwnd = 0
                 mbHook = False
            End If
    End Sub
    
    Private Function MouseProc( _
                 ByVal nCode As Long, ByVal wParam As Long, _
                 ByRef lParam As MOUSEHOOKSTRUCT) As Long
    Dim idx As Long
            On Error GoTo errH
         If (nCode = HC_ACTION) Then
                 If WindowFromPoint(lParam.pt.X, lParam.pt.Y) = mListBoxHwnd Then
                         If wParam = WM_MOUSEWHEEL Then
                                    MouseProc = True
    '                                If lParam.hwnd > 0 Then
    '                                        PostMessage mListBoxHwnd, WM_KEYDOWN, VK_UP, 0
    '                                Else
    '                                        PostMessage mListBoxHwnd, WM_KEYDOWN, VK_DOWN, 0
    '                                End If
    '                                PostMessage mListBoxHwnd, WM_KEYUP, VK_UP, 0
                                    If lParam.hwnd > 0 Then idx = -1 Else idx = 1
    '                             idx = idx + mCtl.ListIndex
    '                             If idx >= 0 Then mCtl.ListIndex = idx
                                 idx = idx + mCtl.TopIndex
                                 If idx >= 0 Then mCtl.TopIndex = idx
                                    Exit Function
                         End If
                 Else
                         UnhookListBoxScroll
                 End If
         End If
         MouseProc = CallNextHookEx( _
                                 mLngMouseHook, nCode, wParam, ByVal lParam)
         Exit Function
    errH:
         UnhookListBoxScroll
    End Function
    '''''''' end normal module code
    This is my workbook, if needed:
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Mouse scroll on a listbox

    All the API functions you are using 'Private Declare ...' etc. are 32 bit Windows dependent. New versions of the function calls are needed for 64 bit Windows.

    The API calls do not work on the Mac. According to Jaslake in post #2 of the following thread, you may have success if you use a 3rd party Virtual Windows machine on your Mac: http://www.excelforum.com/excel-prog...el-2011-a.html

    Lewis

  3. #3
    Forum Contributor
    Join Date
    01-03-2016
    Location
    Rio de Janeiro, Brazil
    MS-Off Ver
    2016
    Posts
    125

    Re: Mouse scroll on a listbox

    Hmm, got it. But how do I solve that problem? I mean, not this mac/windows problem, but moving cursor through the listbox and it doesn't activate one.
    Quote Originally Posted by LJMetzger View Post
    All the API functions you are using 'Private Declare ...' etc. are 32 bit Windows dependent. New versions of the function calls are needed for 64 bit Windows.

    The API calls do not work on the Mac. According to Jaslake in post #2 of the following thread, you may have success if you use a 3rd party Virtual Windows machine on your Mac: http://www.excelforum.com/excel-prog...el-2011-a.html

    Lewis

  4. #4
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Mouse scroll on a listbox

    Control must be active for code to obtain window handle and this hook code is called from mousemove event. Perhaps you may try dropbuttonclick event instead.
    • Please remember to mark threads Solved with Thread Tools link at top of page.
    • Please use code tags when posting code: [code]Place your code here[/code]
    • Please read Forum Rules

  5. #5
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Mouse scroll on a listbox

    A while back I was able to get Peter Thornton's code to work with a great deal of help from several people. The files associated with Post #10 in the following thread may help you: http://www.excelforum.com/excel-prog...orm-frame.html

    Lewis

  6. #6
    Forum Contributor
    Join Date
    01-03-2016
    Location
    Rio de Janeiro, Brazil
    MS-Off Ver
    2016
    Posts
    125

    Re: Mouse scroll on a listbox

    Quote Originally Posted by LJMetzger View Post
    A while back I was able to get Peter Thornton's code to work with a great deal of help from several people. The files associated with Post #10 in the following thread may help you: http://www.excelforum.com/excel-prog...orm-frame.html

    Lewis
    Thank you!!! It worked.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Controlling Scroll in Listbox with Mouse wheel
    By bagullo in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-09-2014, 09:05 AM
  2. Replies: 1
    Last Post: 11-16-2013, 06:17 PM
  3. Scroll Listbox with Mouse
    By zplugger in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-16-2012, 06:36 AM
  4. [SOLVED] Scroll horizontaly with mouse, create same system used to scroll .
    By frederic in forum Excel - New Users/Basics
    Replies: 5
    Last Post: 10-09-2005, 04:05 PM
  5. my mouse moves diagonally when i scroll on mouse?
    By BKMISHRA in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 06:05 AM
  6. my mouse moves diagonally when i scroll on mouse?
    By BKMISHRA in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 04:05 AM
  7. [SOLVED] scroll listbox with mouse wheel
    By RB Smissaert in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-26-2005, 05:55 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1