32 비트 Excel에서만 작동하도록 설정할 수 있습니다. 코드는 64 비트 Excel에서 전혀 컴파일 및 실행되지 않습니다. 32 비트와 64 비트 모두와 호환되는 (조금 더 복잡한) 버전을 만들었지 만 64 비트에서는 스크롤하지 않고 적어도 컴파일 만하면됩니다 (누군가가 64 비트 버전을 필요로하는지 알려주세요) bit 호환 코드). (mouseOverFrame_를
Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Const GWL_STYLE As Long = (-16) 'The offset of a window's style
Private Const WS_SYSMENU As Long = &H80000 'Style to add a system menu
Private Const WS_MINIMIZEBOX As Long = &H20000 'Style to add a Minimize box on the title bar
Private Const WS_MAXIMIZEBOX As Long = &H10000 'Style to add a Maximize box to the title bar
'To be able to scroll with mouse wheel within Userform
Private Declare Function CallWindowProc Lib "user32.dll" Alias "CallWindowProcA" (_
ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, _
ByVal lParam As Long) As Long
Private Const GWL_WNDPROC = -4
Private Const WM_MOUSEWHEEL = &H20A
Dim LocalHwnd As Long
Dim LocalPrevWndProc As Long
Dim myForm As UserForm
Private Function WindowProc(ByVal Lwnd As Long, ByVal Lmsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
'To handle mouse events
Dim MouseKeys As Long
Dim Rotation As Long
If Lmsg = WM_MOUSEWHEEL Then
MouseKeys = wParam And 65535
Rotation = wParam/65536
'My Form s MouseWheel function
'=================================================================
YOUR_USERFORM_NAME_HERE.MouseWheel Rotation
'=================================================================
End If
WindowProc = CallWindowProc(LocalPrevWndProc, Lwnd, Lmsg, wParam, lParam)
End Function
Public Sub WheelHook(PassedForm As UserForm)
'To get mouse events in userform
On Error Resume Next
Set myForm = PassedForm
LocalHwnd = FindWindow("ThunderDFrame", myForm.Caption)
LocalPrevWndProc = SetWindowLong(LocalHwnd, GWL_WNDPROC, AddressOf WindowProc)
End Sub
Public Sub WheelUnHook()
'To Release Mouse events handling
Dim WorkFlag As Long
On Error Resume Next
WorkFlag = SetWindowLong(LocalHwnd, GWL_WNDPROC, LocalPrevWndProc)
Set myForm = Nothing
End Sub
을 그리고 당신은 "frames_을 대체하는 것을 잊지 마세요 ... (사용자 정의 폼에 간단한 코드를 추가
그래서, 당신은 새 모듈을 만들고 WinAPI를 호출 거기에 코드를 붙여 넣습니다) ")를 스크롤하려는 UI 컨트롤의 이름으로 바꿉니다. 나는 세 가지 프레임을 스크롤하고 싶었 기 때문에
Public Sub MouseWheel(ByVal Rotation As Long)
'************************************************
' To respond from MouseWheel event
' Scroll accordingly to direction
'
' Made by: Mathieu Plante
' Date: July 2004
'************************************************
Select Case frames_(mouseOverFrame_).ScrollTop - Sgn(Rotation) * 18
Case Is < 0
frames_(mouseOverFrame_).ScrollTop = 0
Case Is > frames_(mouseOverFrame_).ScrollHeight
frames_(mouseOverFrame_).ScrollTop = frames_(mouseOverFrame_).ScrollHeight
Case Else
frames_(mouseOverFrame_).ScrollTop = frames_(mouseOverFrame_).ScrollTop - Sgn(Rotation) * 18
End Select
End Sub
(프레임은 마우스 커서 아래에 현재에 따라) - I 프레임 번호 "를 할당하는 각 프레임에 세 개의 프레임 사용"MouseMove 이벤트 "이벤트의 컬렉션을 만든 mouseOverFrame_ "변수. 예를 들어 마우스가 움직일 때 첫 번째 프레임에서 스크롤러는 "mouseOverFrame_"변수 안에 "1"을 포함하여 스크롤 할 프레임을 알 수 있습니다 ...
[이 SO Q & A] (http://stackoverflow.com/)를 확인하십시오. 질문/15992475/excel-vba-how-to-enable-mouse-wheel-scroll-in-combobox-listbox) –
@KazJaw - 나는 그 질문을 보았다. 나는 UserForm 자체와 관련이 없으며 대신 OP가 폼 내에서 컨트롤을 스크롤하려고합니다. 저는 VBA를 처음 사용 합니다만, 닷넷과 다른 언어로 컨트롤을 스크롤하는 것과 폼을 스크롤하는 것이 매우 다른 것들이 있습니다. – Brian