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.
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
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
Để 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ậySử 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ách thức giống như listbox thôi ban, thêm code trong form là đượcĐể 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
Private Sub ComboBox1_MouseMove(ByVal Button As Integer, _
ByVal Shift As Integer, _
ByVal x As Single, _
ByVal y As Single)
HookControlScroll ComboBox1
End Sub
Cảm ơn bạn nhiềuCá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á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 ạ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.
Thay vì cố tìm câu trả lời, cài office 2010 trở lên để dùng đi bạn!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 ạ
Code chạy trên cả 64 và 32 bit bạn nhé!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ì.
Lâu giờ em vẫn dùng office 2016, nhưng khi chuyển file cho người khác (dùng 2007) thì không được ạThay vì cố tìm câu trả lời, cài office 2010 trở lên để dùng đi bạn!
Đã chạy thử chưa vậy. Tui thấy khai báo hàm sethookwindowexa đang bị sai kìa. phiên bản 2007 sao lại có cái ptrsafeCode chạy trên cả 64 và 32 bit bạn nhé!
Đó là code của bác vu_tuan_manh_linh mà bác. Bác sửa dùm em được không ạĐã chạy thử chưa vậy. Tui thấy khai báo hàm sethookwindowexa đang bị sai kìa. phiên bản 2007 sao lại có cái ptrsafe
đợi bác ý sửa cho, nếu ko được tối minhdf sẽ xem lại,online bằng dt nên không xem kỹ đượcĐó là code của bác vu_tuan_manh_linh mà bác. Bác sửa dùm em được không ạ
Cái này thì thêm điều kiện thôi bác, ví dụ: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.
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
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à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.
Nếu bạn rãnh bạn sửa code để anh em trên GPE sử dụng với, nếu hay hơn thì tác giả chắc cũng không có ý kiến àh.đợi bác ý sửa cho, nếu ko được tối minhdf sẽ xem lại,online bằng dt nên không xem kỹ được
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.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à