Sử dụng con lăn của chuột trong combox list và list box

Liên hệ QC

chisinhvnn

Thành viên tiêu biểu
Tham gia
7/3/08
Bài viết
479
Được thích
104
Kính nhờ GPE chỉ giúp mình sử dụng con lăn của chuột vào trong combox list và listbox với.
 
Sử dụng con lăn chuột trong listbox:

Code này đặt trong modul:

PHP:
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
'************************************************************************************
#If VBA7 Then
Private Declare PtrSafe Function FindWindow Lib "user32" _
        Alias "FindWindowA" ( _
        ByVal lpClassName As String, _
        ByVal lpWindowName As String) As Long
#Else
Private Declare Function FindWindow Lib "user32" _
        Alias "FindWindowA" ( _
        ByVal lpClassName As String, _
        ByVal lpWindowName As String) As Long
#End If
'-------------
#If VBA7 Then
Private Declare PtrSafe Function GetWindowLong Lib "user32.dll" _
        Alias "GetWindowLongA" ( _
        ByVal hwnd As Long, _
        ByVal nIndex As Long) As Long
#Else
Private Declare Function GetWindowLong Lib "user32.dll" _
        Alias "GetWindowLongA" ( _
        ByVal hwnd As Long, _
        ByVal nIndex As Long) As Long
#End If
'-------------
#If VBA7 Then
Private Declare PtrSafe Function SetWindowsHookEx Lib "user32" _
        Alias "SetWindowsHookExA" ( _
        ByVal idHook As Long, _
        ByVal lpfn As Long, _
        ByVal hmod As Long, _
        ByVal dwThreadId As Long) As Long
#Else
Private Declare PtrSafe Function SetWindowsHookEx Lib "user32" _
        Alias "SetWindowsHookExA" ( _
        ByVal idHook As Long, _
        ByVal lpfn As Long, _
        ByVal hmod As Long, _
        ByVal dwThreadId As Long) As Long
#End If
'-----------
#If VBA7 Then
Private Declare PtrSafe Function CallNextHookEx Lib "user32" ( _
        ByVal hHook As Long, _
        ByVal nCode As Long, _
        ByVal wParam As Long, _
        ByRef lParam As Any) As Long
#Else
Private Declare Function CallNextHookEx Lib "user32" ( _
        ByVal hHook As Long, _
        ByVal nCode As Long, _
        ByVal wParam As Long, _
        ByRef lParam As Any) As Long
#End If
'-------------
#If VBA7 Then
Private Declare PtrSafe Function UnhookWindowsHookEx Lib "user32" ( _
        ByVal hHook As Long) As Long
#Else
Private Declare Function UnhookWindowsHookEx Lib "user32" ( _
        ByVal hHook As Long) As Long
#End If
'------------
#If VBA7 Then
Private Declare PtrSafe Function WindowFromPoint Lib "user32" ( _
        ByVal xPoint As Long, _
        ByVal yPoint As Long) As Long
#Else
Private Declare Function WindowFromPoint Lib "user32" ( _
        ByVal xPoint As Long, _
        ByVal yPoint As Long) As Long
#End If
'-----------
#If VBA7 Then
Private Declare PtrSafe Function GetCursorPos Lib "user32.dll" ( _
        ByRef lpPoint As PointAPI) As Long
#Else
Private Declare Function GetCursorPos Lib "user32.dll" ( _
        ByRef lpPoint As PointAPI) As Long
#End If
'************************************************************************************
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 mLngMouseHook As Long
Private mControlHwnd As Long
Private mbHook As Boolean
Private mCtl As MSForms.control
'************************************************************************************
Sub HookControlScroll(ByVal 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 mControlHwnd <> hwndUnderCursor Then
        UnhookControlScroll
        Set mCtl = Ctl
        mControlHwnd = hwndUnderCursor
        lngAppInst = GetWindowLong(mControlHwnd, GWL_HINSTANCE)
        If Not mbHook Then
            mLngMouseHook = _
            SetWindowsHookEx(WH_MOUSE_LL, _
                             AddressOf MouseProc, _
                             lngAppInst, 0)
            mbHook = mLngMouseHook <> 0
        End If
    End If
End Sub
'************************************************************************************
Sub UnhookControlScroll()
    If mbHook Then
        Set mCtl = Nothing
        UnhookWindowsHookEx mLngMouseHook
        mLngMouseHook = 0
        mControlHwnd = 0
        mbHook = False
    End If
End Sub
'************************************************************************************
Private Function MouseProc( _
        ByVal nCode As Long, _
        ByVal wParam As Long, _
        ByRef lParam As MOUSEHOOKSTRUCT) As Long
     
    On Error GoTo ErrorHandler
     
    Dim Index As Long
    If (nCode = HC_ACTION) Then
        If WindowFromPoint(lParam.pt.x, lParam.pt.y) = mControlHwnd Then
            If wParam = WM_MOUSEWHEEL Then
                 
                MouseProc = True
                 
                If lParam.hwnd > 0 Then
                    Index = -1
                Else
                    Index = 1
                End If
                 
                Index = Index + mCtl.TopIndex
                 
                If Index >= 0 Then
                    mCtl.TopIndex = Index
                End If
                 
                Exit Function
            End If
        Else
            UnhookControlScroll
        End If
    End If
    MouseProc = CallNextHookEx( _
    mLngMouseHook, nCode, wParam, ByVal lParam)
    Exit Function
ErrorHandler:
    UnhookControlScroll
End Function

Code này đặt trong Form:

PHP:
Private Sub UserForm_Terminate()
    UnhookControlScroll
End Sub
'************************************************************************************
Private Sub LstPrintSheet_MouseMove(ByVal Button As Integer, _
                               ByVal Shift As Integer, _
                               ByVal x As Single, _
                               ByVal y As Single)
    HookControlScroll LstPrintSheet
End Sub

LstPrintSheet là tên của listbox trong form muốn sử dụng chức năng này.
 
Upvote 0
Sử dụng con lăn chuột trong listbox:

Code này đặt trong modul:

PHP:
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
'************************************************************************************
#If VBA7 Then
Private Declare PtrSafe Function FindWindow Lib "user32" _
        Alias "FindWindowA" ( _
        ByVal lpClassName As String, _
        ByVal lpWindowName As String) As Long
#Else
Private Declare Function FindWindow Lib "user32" _
        Alias "FindWindowA" ( _
        ByVal lpClassName As String, _
        ByVal lpWindowName As String) As Long
#End If
'-------------
#If VBA7 Then
Private Declare PtrSafe Function GetWindowLong Lib "user32.dll" _
        Alias "GetWindowLongA" ( _
        ByVal hwnd As Long, _
        ByVal nIndex As Long) As Long
#Else
Private Declare Function GetWindowLong Lib "user32.dll" _
        Alias "GetWindowLongA" ( _
        ByVal hwnd As Long, _
        ByVal nIndex As Long) As Long
#End If
'-------------
#If VBA7 Then
Private Declare PtrSafe Function SetWindowsHookEx Lib "user32" _
        Alias "SetWindowsHookExA" ( _
        ByVal idHook As Long, _
        ByVal lpfn As Long, _
        ByVal hmod As Long, _
        ByVal dwThreadId As Long) As Long
#Else
Private Declare PtrSafe Function SetWindowsHookEx Lib "user32" _
        Alias "SetWindowsHookExA" ( _
        ByVal idHook As Long, _
        ByVal lpfn As Long, _
        ByVal hmod As Long, _
        ByVal dwThreadId As Long) As Long
#End If
'-----------
#If VBA7 Then
Private Declare PtrSafe Function CallNextHookEx Lib "user32" ( _
        ByVal hHook As Long, _
        ByVal nCode As Long, _
        ByVal wParam As Long, _
        ByRef lParam As Any) As Long
#Else
Private Declare Function CallNextHookEx Lib "user32" ( _
        ByVal hHook As Long, _
        ByVal nCode As Long, _
        ByVal wParam As Long, _
        ByRef lParam As Any) As Long
#End If
'-------------
#If VBA7 Then
Private Declare PtrSafe Function UnhookWindowsHookEx Lib "user32" ( _
        ByVal hHook As Long) As Long
#Else
Private Declare Function UnhookWindowsHookEx Lib "user32" ( _
        ByVal hHook As Long) As Long
#End If
'------------
#If VBA7 Then
Private Declare PtrSafe Function WindowFromPoint Lib "user32" ( _
        ByVal xPoint As Long, _
        ByVal yPoint As Long) As Long
#Else
Private Declare Function WindowFromPoint Lib "user32" ( _
        ByVal xPoint As Long, _
        ByVal yPoint As Long) As Long
#End If
'-----------
#If VBA7 Then
Private Declare PtrSafe Function GetCursorPos Lib "user32.dll" ( _
        ByRef lpPoint As PointAPI) As Long
#Else
Private Declare Function GetCursorPos Lib "user32.dll" ( _
        ByRef lpPoint As PointAPI) As Long
#End If
'************************************************************************************
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 mLngMouseHook As Long
Private mControlHwnd As Long
Private mbHook As Boolean
Private mCtl As MSForms.control
'************************************************************************************
Sub HookControlScroll(ByVal 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 mControlHwnd <> hwndUnderCursor Then
        UnhookControlScroll
        Set mCtl = Ctl
        mControlHwnd = hwndUnderCursor
        lngAppInst = GetWindowLong(mControlHwnd, GWL_HINSTANCE)
        If Not mbHook Then
            mLngMouseHook = _
            SetWindowsHookEx(WH_MOUSE_LL, _
                             AddressOf MouseProc, _
                             lngAppInst, 0)
            mbHook = mLngMouseHook <> 0
        End If
    End If
End Sub
'************************************************************************************
Sub UnhookControlScroll()
    If mbHook Then
        Set mCtl = Nothing
        UnhookWindowsHookEx mLngMouseHook
        mLngMouseHook = 0
        mControlHwnd = 0
        mbHook = False
    End If
End Sub
'************************************************************************************
Private Function MouseProc( _
        ByVal nCode As Long, _
        ByVal wParam As Long, _
        ByRef lParam As MOUSEHOOKSTRUCT) As Long
   
    On Error GoTo ErrorHandler
   
    Dim Index As Long
    If (nCode = HC_ACTION) Then
        If WindowFromPoint(lParam.pt.x, lParam.pt.y) = mControlHwnd Then
            If wParam = WM_MOUSEWHEEL Then
               
                MouseProc = True
               
                If lParam.hwnd > 0 Then
                    Index = -1
                Else
                    Index = 1
                End If
               
                Index = Index + mCtl.TopIndex
               
                If Index >= 0 Then
                    mCtl.TopIndex = Index
                End If
               
                Exit Function
            End If
        Else
            UnhookControlScroll
        End If
    End If
    MouseProc = CallNextHookEx( _
    mLngMouseHook, nCode, wParam, ByVal lParam)
    Exit Function
ErrorHandler:
    UnhookControlScroll
End Function

Code này đặt trong Form:

PHP:
Private Sub UserForm_Terminate()
    UnhookControlScroll
End Sub
'************************************************************************************
Private Sub LstPrintSheet_MouseMove(ByVal Button As Integer, _
                               ByVal Shift As Integer, _
                               ByVal x As Single, _
                               ByVal y As Single)
    HookControlScroll LstPrintSheet
End Sub

LstPrintSheet là tên của listbox trong form muốn sử dụng chức năng này.
Để sử dụng con lăn của chuột mà phức tập ghê vậy. Cho mình hỏi thêm, code trên sử dụng tromg combox list có được không vậy
 
Lần chỉnh sửa cuối:
Upvote 0
Mình chưa tìm hiểu về combobox. Để mình tìm hiểu rồi reply
 
Upvote 0
Để sử dụng con lăn của chuột mà phức tập ghê vậy. Cho mình hỏi thêm, code trên sử dụng tromg combox list có được không vậy
Cách thức giống như listbox thôi ban, thêm code trong form là được
PHP:
Private Sub ComboBox1_MouseMove(ByVal Button As Integer, _
                               ByVal Shift As Integer, _
                               ByVal x As Single, _
                               ByVal y As Single)
    HookControlScroll ComboBox1
End Sub

Bạn phải thay ComboBox1 bằng tên ComboBox trong form của bạn!
 
Upvote 0
Cách thức giống như listbox thôi ban, thêm code trong form là được
PHP:
Private Sub ComboBox1_MouseMove(ByVal Button As Integer, _
                               ByVal Shift As Integer, _
                               ByVal x As Single, _
                               ByVal y As Single)
    HookControlScroll ComboBox1
End Sub

Bạn phải thay ComboBox1 bằng tên ComboBox trong form của bạn!
Cảm ơn bạn nhiều
 
Upvote 0
Sử dụng con lăn chuột trong listbox:

Code này đặt trong modul:

PHP:
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
'************************************************************************************
#If VBA7 Then
Private Declare PtrSafe Function FindWindow Lib "user32" _
        Alias "FindWindowA" ( _
        ByVal lpClassName As String, _
        ByVal lpWindowName As String) As Long
#Else
Private Declare Function FindWindow Lib "user32" _
        Alias "FindWindowA" ( _
        ByVal lpClassName As String, _
        ByVal lpWindowName As String) As Long
#End If
'-------------
#If VBA7 Then
Private Declare PtrSafe Function GetWindowLong Lib "user32.dll" _
        Alias "GetWindowLongA" ( _
        ByVal hwnd As Long, _
        ByVal nIndex As Long) As Long
#Else
Private Declare Function GetWindowLong Lib "user32.dll" _
        Alias "GetWindowLongA" ( _
        ByVal hwnd As Long, _
        ByVal nIndex As Long) As Long
#End If
'-------------
#If VBA7 Then
Private Declare PtrSafe Function SetWindowsHookEx Lib "user32" _
        Alias "SetWindowsHookExA" ( _
        ByVal idHook As Long, _
        ByVal lpfn As Long, _
        ByVal hmod As Long, _
        ByVal dwThreadId As Long) As Long
#Else
Private Declare PtrSafe Function SetWindowsHookEx Lib "user32" _
        Alias "SetWindowsHookExA" ( _
        ByVal idHook As Long, _
        ByVal lpfn As Long, _
        ByVal hmod As Long, _
        ByVal dwThreadId As Long) As Long
#End If
'-----------
#If VBA7 Then
Private Declare PtrSafe Function CallNextHookEx Lib "user32" ( _
        ByVal hHook As Long, _
        ByVal nCode As Long, _
        ByVal wParam As Long, _
        ByRef lParam As Any) As Long
#Else
Private Declare Function CallNextHookEx Lib "user32" ( _
        ByVal hHook As Long, _
        ByVal nCode As Long, _
        ByVal wParam As Long, _
        ByRef lParam As Any) As Long
#End If
'-------------
#If VBA7 Then
Private Declare PtrSafe Function UnhookWindowsHookEx Lib "user32" ( _
        ByVal hHook As Long) As Long
#Else
Private Declare Function UnhookWindowsHookEx Lib "user32" ( _
        ByVal hHook As Long) As Long
#End If
'------------
#If VBA7 Then
Private Declare PtrSafe Function WindowFromPoint Lib "user32" ( _
        ByVal xPoint As Long, _
        ByVal yPoint As Long) As Long
#Else
Private Declare Function WindowFromPoint Lib "user32" ( _
        ByVal xPoint As Long, _
        ByVal yPoint As Long) As Long
#End If
'-----------
#If VBA7 Then
Private Declare PtrSafe Function GetCursorPos Lib "user32.dll" ( _
        ByRef lpPoint As PointAPI) As Long
#Else
Private Declare Function GetCursorPos Lib "user32.dll" ( _
        ByRef lpPoint As PointAPI) As Long
#End If
'************************************************************************************
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 mLngMouseHook As Long
Private mControlHwnd As Long
Private mbHook As Boolean
Private mCtl As MSForms.control
'************************************************************************************
Sub HookControlScroll(ByVal 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 mControlHwnd <> hwndUnderCursor Then
        UnhookControlScroll
        Set mCtl = Ctl
        mControlHwnd = hwndUnderCursor
        lngAppInst = GetWindowLong(mControlHwnd, GWL_HINSTANCE)
        If Not mbHook Then
            mLngMouseHook = _
            SetWindowsHookEx(WH_MOUSE_LL, _
                             AddressOf MouseProc, _
                             lngAppInst, 0)
            mbHook = mLngMouseHook <> 0
        End If
    End If
End Sub
'************************************************************************************
Sub UnhookControlScroll()
    If mbHook Then
        Set mCtl = Nothing
        UnhookWindowsHookEx mLngMouseHook
        mLngMouseHook = 0
        mControlHwnd = 0
        mbHook = False
    End If
End Sub
'************************************************************************************
Private Function MouseProc( _
        ByVal nCode As Long, _
        ByVal wParam As Long, _
        ByRef lParam As MOUSEHOOKSTRUCT) As Long
    
    On Error GoTo ErrorHandler
    
    Dim Index As Long
    If (nCode = HC_ACTION) Then
        If WindowFromPoint(lParam.pt.x, lParam.pt.y) = mControlHwnd Then
            If wParam = WM_MOUSEWHEEL Then
                
                MouseProc = True
                
                If lParam.hwnd > 0 Then
                    Index = -1
                Else
                    Index = 1
                End If
                
                Index = Index + mCtl.TopIndex
                
                If Index >= 0 Then
                    mCtl.TopIndex = Index
                End If
                
                Exit Function
            End If
        Else
            UnhookControlScroll
        End If
    End If
    MouseProc = CallNextHookEx( _
    mLngMouseHook, nCode, wParam, ByVal lParam)
    Exit Function
ErrorHandler:
    UnhookControlScroll
End Function

Code này đặt trong Form:

PHP:
Private Sub UserForm_Terminate()
    UnhookControlScroll
End Sub
'************************************************************************************
Private Sub LstPrintSheet_MouseMove(ByVal Button As Integer, _
                               ByVal Shift As Integer, _
                               ByVal x As Single, _
                               ByVal y As Single)
    HookControlScroll LstPrintSheet
End Sub

LstPrintSheet là tên của listbox trong form muốn sử dụng chức năng này.
Các anh chị cho em em, code này không chạy được trên Office 2007 trở xuống phải không ạ. Em chưa test trên bản 2010, nhưng trên 2016 chạy tốt, còn 2007 thì báo lỗi ạ
 
Upvote 0
code đó đoán là copy ở đâu đó, khi copy thì không để ý nên bị lộn vài chỗ. Khả nămg là nó chỉ chạy được trên excel 64 bít, ngược lại là sai. 2007 là 32 vít còn gì.
 
Upvote 0
Các anh chị cho em em, code này không chạy được trên Office 2007 trở xuống phải không ạ. Em chưa test trên bản 2010, nhưng trên 2016 chạy tốt, còn 2007 thì báo lỗi ạ
Thay vì cố tìm câu trả lời, cài office 2010 trở lên để dùng đi bạn!
 
Upvote 0
Upvote 0
Cũng phải nói là code trên chạy chả ra hồn gì. khi di chuyển chuột lên listbox mà chưa bấm chuột để kích hoạt và xoay chuột thì listbox cũng cuốn, vô lý. trong window chỉ control nào đang kích hoạt mới nhận chuột. Nếu listbox đang kích hoạt mà đang cuộn nếu di chuyển chuột ra chỗ khác và tiếp tục xoay chuột, listbox cungz ko cuộn.
 
Upvote 0
Cũng phải nói là code trên chạy chả ra hồn gì. khi di chuyển chuột lên listbox mà chưa bấm chuột để kích hoạt và xoay chuột thì listbox cũng cuốn, vô lý. trong window chỉ control nào đang kích hoạt mới nhận chuột. Nếu listbox đang kích hoạt mà đang cuộn nếu di chuyển chuột ra chỗ khác và tiếp tục xoay chuột, listbox cungz ko cuộn.
Cái này thì thêm điều kiện thôi bác, ví dụ:
Mã:
Private Sub Listbox1_mousemove(ByVal Button As Integer, _
                               ByVal Shift As Integer, _
                               ByVal X As Single, _
                               ByVal Y As Single)
   
    If Listbox1.ListIndex >= 0 Then
        HookControlScroll Listbox1
    End If

End Sub
Bác giúp em sửa lỗi code trên cho Office 2007 được không ạ
 
Upvote 0
Cũng phải nói là code trên chạy chả ra hồn gì. khi di chuyển chuột lên listbox mà chưa bấm chuột để kích hoạt và xoay chuột thì listbox cũng cuốn, vô lý. trong window chỉ control nào đang kích hoạt mới nhận chuột. Nếu listbox đang kích hoạt mà đang cuộn nếu di chuyển chuột ra chỗ khác và tiếp tục xoay chuột, listbox cungz ko cuộn.
Mình thấy khi đưa ra ngoài listbox nó đâu có hoat động, khi đưa con chuột vào vùng listbox lắn chuột nó mới chạy mà
 
Upvote 0
Mình thấy khi đưa ra ngoài listbox nó đâu có hoat động, khi đưa con chuột vào vùng listbox lắn chuột nó mới chạy mà
Nó không hoạt động thì mới là sai, giả sử bạn thu nhỏ cửa sổ của excel bănngf một nửa, sau đó cuộn chuột, các ô của excel bị cuộn. giờ di chuột ra ngoài excel và cuộn chuột, các ô vẫn được cuộn, vì excel vẫn đang kich hoạt cơ mà. trên tinh thần đó thì code trên mới sai, nó ép chuột phải nằm trên listbox.
 
Upvote 0
Em đã thử test trên máy office 2007 thì bị lỗi này
 

File đính kèm

  • 123.jpg
    123.jpg
    140.7 KB · Đọc: 30
  • Cuon trang bang chuot Listbox.xls
    58.5 KB · Đọc: 37
Upvote 0
Web KT

Bài viết mới nhất

Back
Top Bottom