標題:
[發問]
如何讓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
Private Sub UserForm_Activate()
ComboBox1.Value = "150"
ComboBox1.List = Array("150", "200", "250", "300", "350", "400", "450", "500", "510", "570", "630", _
"700", "770", "840", "920", "1000", "1080", "1170", "1270", "1370", "1480", "1600", "1720", "1850", "2000", _
"2150", "2320", "2500", "2700", "2900", "3150", "3400", "3700", "4000", "4400", "4800", "5300", "5800", _
"6400", "7000", "7700", "8500", "9500", "10500", "12000", "13500")
End Sub
Sub ComboBox1_DropButton()
'Store the first TopIndex Value
intTopIndex = ComboBox1.TopIndex
Hook_Mouse
End Sub
Private Sub ComboBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
UnHook_Mouse
End Sub
複製代碼
作者:
c_c_lai
時間:
2013-12-16 19:23
回復
4#
c_c_lai
模組->Module1:
Option Explicit
Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function GetForegroundWindow Lib "user32" () As Long
Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(ByVal Destination As Long, ByVal Source As Long, ByVal Length As Long)
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
Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, _
ByVal nCode As Long, ByVal wParam As Long, lParam As Any) As Long
Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Type POINTAPI
X As Long
Y As Long
End Type
Type MSLLHOOKSTRUCT 'Will Hold the lParam struct Data
pt As POINTAPI
mouseData As Long ' Holds Forward\Bacward flag
flags As Long
time As Long
dwExtraInfo As Long
End Type
Const HC_ACTION = 0
Const WH_MOUSE_LL = 14
Const WM_MOUSEWHEEL = &H20A
Dim hhkLowLevelMouse, lngInitialColor As Long
Dim udtlParamStuct As MSLLHOOKSTRUCT
Public intTopIndex As Integer
Private Sub Auto_open()
UserForm1.ComboBox1_DropButton
UserForm1.Show
End Sub
' ==========================================================================
' \\Copy the Data from lParam of the Hook Procedure argument to our Struct
Function GetHookStruct(ByVal lParam As Long) As MSLLHOOKSTRUCT
CopyMemory VarPtr(udtlParamStuct), lParam, LenB(udtlParamStuct)
GetHookStruct = udtlParamStuct
End Function
' ===========================================================================
Function LowLevelMouseProc _
(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
' Avoid XL crashing if RunTime error occurs due to Mouse fast movement
On Error Resume Next
If (nCode = HC_ACTION) Then
If wParam = WM_MOUSEWHEEL Then
'\\ Don't process Default WM_MOUSEWHEEL Window message
LowLevelMouseProc = True
' \\ Change this to your userform name
With UserForm1.ComboBox1
' \\ if rolling forward increase Top index by 1 to cause an Up Scroll
If GetHookStruct(lParam).mouseData > 0 Then
.TopIndex = intTopIndex - 1
' \\ Store new TopIndex value
intTopIndex = .TopIndex
Else ' \\ if rolling backward decrease Top index by 1 to cause _
' \\a Down Scroll
.TopIndex = intTopIndex + 1
' \\ Store new TopIndex value
intTopIndex = .TopIndex
End If
End With
End If
Exit Function
End If
LowLevelMouseProc = CallNextHookEx(0, nCode, wParam, ByVal lParam)
End Function
' =======================================================================
Sub Hook_Mouse()
hhkLowLevelMouse = SetWindowsHookEx _
(WH_MOUSE_LL, AddressOf LowLevelMouseProc, Application.Hinstance, 0)
End Sub
' ========================================================================
Sub UnHook_Mouse()
If hhkLowLevelMouse <> 0 Then UnhookWindowsHookEx hhkLowLevelMouse
End Sub
複製代碼
作者:
c_c_lai
時間:
2013-12-16 19:32
回復
3#
acdx
模組裡再加上:
Private Sub Auto_Close()
UnHook_Mouse
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
Sub Hook_Mouse()
hhkLowLevelMouse = SetWindowsHookEx _
(WH_MOUSE_LL, AddressOf LowLevelMouseProc, Application.Hinstance, 0)
End Sub
複製代碼
Hook_Mouse 去呼叫 Windlows 的程式庫函數
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
複製代碼
此 Function 將 LowLevelMouseProc 帶回來的位址,以及 WH_MOUSE_LL (=14) 與事件、執行緒參數等
資訊傳入給 LowLevelMouseProc 以啟動 MOUSEWHEEL 的功能設定。
如此概略解說你應該會有所了解吧?
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)