Board logo

標題: [發問] 如何讓ComboBox可以用滑鼠滾動? [打印本頁]

作者: acdx    時間: 2013-12-11 16:52     標題: 如何讓ComboBox可以用滑鼠滾動?

我的ComboBox裡總共有多達45個選項
如何使用滑鼠滾動來來選擇選單中其他選項?
[attach]17045[/attach]
作者: sunnyso    時間: 2013-12-11 19:21

回復 1# acdx

有附件嗎?
作者: acdx    時間: 2013-12-16 10:46

[attach]17059[/attach]
附件如上
作者: c_c_lai    時間: 2013-12-16 19:18

回復 3# acdx
試試看!
[attach]17063[/attach]
作者: c_c_lai    時間: 2013-12-16 19:21

回復 3# acdx
表單->UserForm1
  1. Private Sub UserForm_Activate()   
  2.     ComboBox1.Value = "150"
  3.     ComboBox1.List = Array("150", "200", "250", "300", "350", "400", "450", "500", "510", "570", "630", _
  4.         "700", "770", "840", "920", "1000", "1080", "1170", "1270", "1370", "1480", "1600", "1720", "1850", "2000", _
  5.         "2150", "2320", "2500", "2700", "2900", "3150", "3400", "3700", "4000", "4400", "4800", "5300", "5800", _
  6.         "6400", "7000", "7700", "8500", "9500", "10500", "12000", "13500")
  7. End Sub

  8. Sub ComboBox1_DropButton()
  9.     'Store the first TopIndex Value
  10.     intTopIndex = ComboBox1.TopIndex
  11.     Hook_Mouse
  12. End Sub

  13. Private Sub ComboBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
  14.     UnHook_Mouse
  15. End Sub
複製代碼

作者: c_c_lai    時間: 2013-12-16 19:23

回復 4# c_c_lai
模組->Module1:
  1. Option Explicit

  2. Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
  3.         (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

  4. Declare Function GetForegroundWindow Lib "user32" () As Long

  5. Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
  6.         (ByVal Destination As Long, ByVal Source As Long, ByVal Length As Long)

  7. Declare Function SetWindowsHookEx Lib _
  8.         "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, _
  9.         ByVal hmod As Long, ByVal dwThreadId As Long) As Long

  10. Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, _
  11.         ByVal nCode As Long, ByVal wParam As Long, lParam As Any) As Long

  12. Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long

  13. Type POINTAPI
  14.     X As Long
  15.     Y As Long
  16. End Type

  17. Type MSLLHOOKSTRUCT 'Will Hold the lParam struct Data
  18.     pt As POINTAPI
  19.     mouseData As Long ' Holds Forward\Bacward flag
  20.     flags As Long
  21.     time As Long
  22.     dwExtraInfo As Long
  23. End Type

  24. Const HC_ACTION = 0
  25. Const WH_MOUSE_LL = 14
  26. Const WM_MOUSEWHEEL = &H20A

  27. Dim hhkLowLevelMouse, lngInitialColor As Long
  28. Dim udtlParamStuct As MSLLHOOKSTRUCT
  29. Public intTopIndex As Integer

  30. Private Sub Auto_open()
  31.     UserForm1.ComboBox1_DropButton
  32.     UserForm1.Show
  33. End Sub

  34. '  ==========================================================================
  35. '  \\Copy the Data from lParam of the Hook Procedure argument to our Struct
  36. Function GetHookStruct(ByVal lParam As Long) As MSLLHOOKSTRUCT
  37.    CopyMemory VarPtr(udtlParamStuct), lParam, LenB(udtlParamStuct)
  38.    GetHookStruct = udtlParamStuct
  39. End Function

  40. '  ===========================================================================
  41. Function LowLevelMouseProc _
  42.         (ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  43.     '  Avoid XL crashing if RunTime error occurs due to Mouse fast movement
  44.     On Error Resume Next

  45.     If (nCode = HC_ACTION) Then
  46.         If wParam = WM_MOUSEWHEEL Then
  47.                 '\\ Don't process Default WM_MOUSEWHEEL Window message
  48.                 LowLevelMouseProc = True
  49.                 '  \\ Change this to your userform name
  50.                 With UserForm1.ComboBox1
  51.                 '  \\ if rolling forward increase Top index by 1 to cause an Up Scroll
  52.                 If GetHookStruct(lParam).mouseData > 0 Then
  53.                     .TopIndex = intTopIndex - 1
  54.                     '  \\ Store new TopIndex value
  55.                     intTopIndex = .TopIndex
  56.                 Else '  \\ if rolling backward decrease Top index by 1 to cause _
  57.                     '  \\a Down Scroll
  58.                     .TopIndex = intTopIndex + 1
  59.                     '  \\ Store new TopIndex value
  60.                     intTopIndex = .TopIndex
  61.                 End If
  62.            End With
  63.         End If
  64.         Exit Function
  65.     End If

  66.     LowLevelMouseProc = CallNextHookEx(0, nCode, wParam, ByVal lParam)
  67. End Function

  68. '  =======================================================================
  69. Sub Hook_Mouse()
  70.     hhkLowLevelMouse = SetWindowsHookEx _
  71.         (WH_MOUSE_LL, AddressOf LowLevelMouseProc, Application.Hinstance, 0)
  72. End Sub

  73. '  ========================================================================
  74. Sub UnHook_Mouse()
  75.     If hhkLowLevelMouse <> 0 Then UnhookWindowsHookEx hhkLowLevelMouse
  76. End Sub
複製代碼

作者: c_c_lai    時間: 2013-12-16 19:32

回復 3# acdx
模組裡再加上:
  1. Private Sub Auto_Close()
  2.     UnHook_Mouse
  3. End Sub
複製代碼
於離開時同時關閉 Hook。
作者: acdx    時間: 2013-12-17 15:09

回復 6# c_c_lai
為何會需要
Private Sub Auto_open()
UserForm1.ComboBox1_DropButton
UserForm1.Show
End Sub
我不需要自動開啟表單 是要把它拿掉嗎?
作者: c_c_lai    時間: 2013-12-17 16:37

回復 8# acdx
那只是要提示你,在使用開啟 UserForm1前要先執行
Hook 的動作而已,同時也為了讓你打開檔案時,便於
觀察到 Mouse Wheel 的運用。
作者: c_c_lai    時間: 2013-12-17 18:47

回復 8# acdx
請參考:
Mouse wheel scroll in a combobox on userform
作者: acdx    時間: 2013-12-20 11:41

回復 10# c_c_lai
這已經超出我的程度太多了
我研究了好幾天還是覺得霧煞煞 有更容易懂的解說嗎?
作者: c_c_lai    時間: 2013-12-21 09:26

回復 11# acdx
  1. Sub Hook_Mouse()
  2.     hhkLowLevelMouse = SetWindowsHookEx _
  3.         (WH_MOUSE_LL, AddressOf LowLevelMouseProc, Application.Hinstance, 0)
  4. End Sub
複製代碼
Hook_Mouse 去呼叫 Windlows 的程式庫函數
  1. Declare Function SetWindowsHookEx Lib _
  2.         "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, _
  3.         ByVal hmod As Long, ByVal dwThreadId As Long) As Long
複製代碼
此 Function 將 LowLevelMouseProc  帶回來的位址,以及 WH_MOUSE_LL (=14) 與事件、執行緒參數等
資訊傳入給 LowLevelMouseProc 以啟動 MOUSEWHEEL 的功能設定。
如此概略解說你應該會有所了解吧?




歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)