我该如何做?
---- 实现应用程序支持鼠标滚轮的关键是,捕获鼠标滚轮的消息 MSH_MOUSEWHEEL、WM_MOUSEWHEEL。其中MSH_MOUSEWHEEL是为95准备的,需要Intellimouse驱动程序,而WM_MOUSEWHEEL是目前各版本Windows(98/NT40/2000)内置的消息。本文主要处理WM_MOUSEWHEEL消息。下面是WM_MOUSEWHEEL的语法。
WM_MOUSEWHEEL
fwKeys = LOWORD(wParam); /* key flags */
zDelta = (short) HIWORD(wParam);
/* wheel rotation */
xPos = (short) LOWORD(lParam);
/* horizontal position of pointer */
yPos = (short) HIWORD(lParam);
/* vertical position of pointer */
---- 其中:fwKeys指出是否有CTRL、SHIFT、鼠标键(左、中、右、附加)按下,允许复合。zDelta传递滚轮滚动的快慢,该值小于零表示滚轮向后滚动(朝用户方向),大于零表示滚轮向前滚动(朝显示器方向)。lParam指出鼠标指针相对屏幕左上的x、y轴坐标。
---- 滚轮按钮相当于普通的三键鼠标的中键,根据滚轮按钮的动作,Windows分别发出WM_MBUTTONUP、WM_MBUTTONDOWN、WM_MBUTTONDBLCLK消息,这些消息VB已经在鼠标事件中支持。
---- 三、实际应用
---- 根据上述原理,给出一个数据库应用的典型例子。
---- 1.用户界面班级和学生一对多的查询,当用户在学生网格以外滚动鼠标滚轮,班级主表前后移动;用户在网格以内滚动鼠标学生明细表垂直移动;如果在网格以内按住鼠标滚轮键并且滚动鼠标,学生明细表水平移动。
---- 2.Form1上ADO Data 控件对象datPrimaryRS的 ConnectionString为"PROVIDER=MSDataShape;Data PROVIDER=MSDASQL;dsn=SCHOOL;uid=;pwd=;", RecordSelectors 属性的SQL命令文本为"SHAPE {select * from 班级} AS ParentCMD APPEND ({select * from 学生 } AS ChildCMD RELATE 班级名称 TO 班级名称) AS ChildCMD"。
---- 3.TextBox的DataSource均为datPrimaryRS,DataFiled如图所示。
---- 4.窗口下部的网格是DataGrid控件,名称为grdDataGrid。
---- 5.表单From1.frm的清单如下:
Private Sub Form_Load()
Set grdDataGrid.DataSource = _
datPrimaryRS.Recordset("ChildCMD").UnderlyingValue
Hook Me.hWnd
End Sub
Private Sub Form_Unload(Cancel As Integer)
UnHook Me.hWnd
End Sub
---- 6.标准模块Module1.bas清单如下:
Option Explicit
Public Type POINTL
x As Long
y As Long
End Type
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
Declare Function SetWindowLong _
Lib "USER32" Alias "SetWindowLongA" _
(ByVal hWnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
Declare Function SystemParametersInfo _
Lib "USER32" Alias "SystemParametersInfoA" _
(ByVal uAction As Long, _
ByVal uParam As Long, _
lpvParam As Any, _
ByVal fuWinIni As Long) As Long
Declare Function ScreenToClient Lib "USER32" _
(ByVal hWnd As Long, xyPoint As POINTL) As Long
Public Const GWL_WNDPROC = -4
Public Const SPI_GETWHEELSCROLLLINES = 104
Public Const WM_MOUSEWHEEL = &H20A
Public WHEEL_SCROLL_LINES As Long
Global lpPrevWndProc As Long
Public Sub Hook(ByVal hWnd As Long)
lpPrevWndProc = SetWindowLong(hWnd, GWL_WNDPROC, _
AddressOf WindowProc)
'获取"控制面板"中的滚动行数值
Call SystemParametersInfo(SPI_GETWHEELSCROLLLINES, _
0, WHEEL_SCROLL_LINES, 0)
If WHEEL_SCROLL_LINES > Form1.grdDataGrid.VisibleRows Then
WHEEL_SCROLL_LINES = Form1.grdDataGrid.VisibleRows
End If
End Sub
Public Sub UnHook(ByVal hWnd As Long)
Dim lngReturnValue As Long
lngReturnValue = SetWindowLong(hWnd,
GWL_WNDPROC, lpPrevWndProc)
End Sub
Function WindowProc(ByVal hw As Long, _
ByVal uMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Dim pt As POINTL
Select Case uMsg
Case WM_MOUSEWHEEL
Dim wzDelta, wKeys As Integer
wzDelta = HIWORD(wParam)
wKeys = LOWORD(wParam)
pt.x = LOWORD(lParam)
pt.y = HIWORD(lParam)
'将屏幕坐标转换为Form1.窗口坐标
ScreenToClient Form1.hWnd, pt
With Form1.grdDataGrid
'判断坐标是否在Form1.grdDataGrid窗口内
If pt.x > .Left / Screen.TwipsPerPixelX And _
pt.x < (.Left + .Width) / Screen.TwipsPerPixelX And _
pt.y > .Top / Screen.TwipsPerPixelY And _
pt.y < (.Top + .Height) / Screen.TwipsPerPixelY Then
'滚动明细数据库
If wKeys = 16 Then
'滚动键按下,水平滚动grdDataGrid
If Sgn(wzDelta) = 1 Then
Form1.grdDataGrid.Scroll -1, 0
Else
Form1.grdDataGrid.Scroll 1, 0
End If
Else
'垂直滚动grdDataGrid
If Sgn(wzDelta) = 1 Then
Form1.grdDataGrid.Scroll 0, 0 - WHEEL_SCROLL_LINES
Else
Form1.grdDataGrid.Scroll 0, WHEEL_SCROLL_LINES
End If
End If
Else
'鼠标不在grdDataGrid区域,滚动主数据库
With Form1.datPrimaryRS.Recordset
If Sgn(wzDelta) = 1 Then
If .BOF = False Then
.MovePrevious
If .BOF = True Then
.MoveFirst
End If
End If
Else
If .EOF = False Then
.MoveNext
If .EOF = True Then
.MoveLast
End If
End If
End If
End With
End If
End With
Case Else
WindowProc = CallWindowProc(lpPrevWndProc, hw, _
uMsg, wParam, lParam)
End Select
End Function
Public Function HIWORD(LongIn As Long) As Integer
' 取出32位值的高16位
HIWORD = (LongIn And &HFFFF0000) &H10000
End Function
Public Function LOWORD(LongIn As Long) As Integer
' 取出32位值的低16位
LOWORD = LongIn And &HFFFF&
End Function
谢谢版主,可是我运行这个的时候它立马就退出了,不知道为什么.
不提示错误,没有任何的提示,只是退出.
但我只要把
Public Sub Hook(ByVal hWnd As Long)
lpPrevWndProc = SetWindowLong(hWnd, GWL_WNDPROC, _
AddressOf WindowProc)
'获取"控制面板"中的滚动行数值
Call SystemParametersInfo(SPI_GETWHEELSCROLLLINES, _
0, WHEEL_SCROLL_LINES, 0)
If WHEEL_SCROLL_LINES > Form1.grdDataGrid.VisibleRows Then
WHEEL_SCROLL_LINES = Form1.grdDataGrid.VisibleRows
End If
End Sub
标出的那一句给注解掉,它就不会退出,当然,我想要的功能也没了,不知道是什么原因.