怎么才能知道鼠标飞轮是向哪一个方向滚动的?
我已经知道了通过子类化处理WM_MOUSEWHEEL消息可以知道鼠标飞轮滚动了,但是又发现了一个问题,怎么才能知道鼠标飞轮是向哪个方向滚的?
[解决办法]
下面是使本不支持鼠标滚轮的控件支持滚轮的代码,相信楼主从中可以知道如何判断滚轮的方向了:
Public Function FlexScroll(ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Select Case wMsg Case WM_MOUSEWHEEL Select Case wParam Case -7864320 '向下滚 SendKeys "{PGDN}" Case 7864320 '向上滚 SendKeys "{PGUP}" End Select End Select FlexScroll = CallWindowProc(Oldwinproc, hWnd, wMsg, wParam, lParam)End Function
[解决办法]
看看我写的这个小工具,里面用到了鼠标滚轮,具体是通过鼠标滚轮来控制图片大小缩放的,和windows的图片查看器的缩放功能类似。
[解决办法]
[code=VB][/code]
Option Explicit
'检测鼠标滚轮信息
'API函数
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function CallWindowProc Lib "user32" 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 HT_CAPTION = 2
Private Const WM_NCLBUTTONDOWN = &HA1
Private Const WM_MOUSEWHEEL = &H20A
Private Const WM_MOUSEMOVE = &H200
Private Const WM_LBUTTONDOWN = &H201
'全局变量,存放控件标志性数据
Public preWinProc As Long
'本函数用来接收子分类时截取的消息的
Public Function wndproc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
'截取下来的消息存放在msg参数中.
'Debug.Print "wndproc msg =" & Msg
Select Case Msg
Case WM_MOUSEWHEEL '检测到鼠标滚轮消息
Select Case wParam
Case -7864320 '向下滚
Case 7864320 '向上滚
Case Else
End Select
Case WM_NCLBUTTONDOWN '检测到鼠标单击消息
'Debug.Print "wndproc m"
Case Else
End Select
wndproc = CallWindowProc(preWinProc, hwnd, Msg, wParam, lParam)
End Function
Public Sub subclass(hwnd As Long)
'记录Window Procedure的地址
'开始截取消息,并将消息交给wndproc过程处理.
preWinProc = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf wndproc)
End Sub
Public Sub EndSubclass(hwnd As Long)
'取消消息截取,结束子分类过程.
Call SetWindowLong(hwnd, GWL_WNDPROC, preWinProc)
End Sub
[解决办法]