MSFlexGrid的鼠标滚轮带动问题
做系统中我用了控件,但是这个控件不支持鼠标的滚轴的拉动,请问怎么办?
[解决办法]
新建一个模块,贴上下面的代码:
Public Declare Function SetWindowLong Lib "user32 " Alias "SetWindowLongA " (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public 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
Public Const GWL_WNDPROC = (-4)
Public Type tGridList
frm As Form
grid As MSFlexGrid
grdHwnd As Long
grdPreProc As Long
End Type
Private GridList() As tGridList
Private nGridCount As Long
Public Function WindowProcGridHook(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim nIndex As Long
nIndex = GetGridIndex(hwnd)
If uMsg <> 522 Then
WindowProcGridHook = CallWindowProc(GridList(nIndex).grdPreProc, hwnd, uMsg, wParam, lParam)
Else '滚轮
On Error Resume Next
With GridList(nIndex).grid
Dim lngTopRow As Long, lngBottomRow As Long
lngTopRow = 1
lngBottomRow = .Rows - 1
If wParam > 0 Then
If Not .RowIsVisible(lngTopRow) Then
.TopRow = .TopRow - 1
End If
Else
.TopRow = .TopRow + 1
End If
End With
End If
End Function
Public Sub StartHook(frm As Form)
Dim x As Variant
Dim proc As Long
For Each x In frm.Controls
If TypeOf x Is MSFlexGrid Then
nGridCount = nGridCount + 1
ReDim Preserve GridList(1 To nGridCount) As tGridList
Set GridList(nGridCount).grid = x
Set GridList(nGridCount).frm = frm
GridList(nGridCount).grdHwnd = x.hwnd
proc = SetWindowLong(x.hwnd, GWL_WNDPROC, AddressOf WindowProcGridHook)
GridList(nGridCount).grdPreProc = proc
End If
Next
End Sub
Public Sub EndHook(frm As Form)
Dim i As Long, j As Long, n As Long
For i = nGridCount To 1 Step -1
If GridList(i).frm Is frm Then
SetWindowLong GridList(i).grdHwnd, GWL_WNDPROC, GridList(i).grdPreProc
n = n + 1
For j = i To nGridCount - n
GridList(j) = GridList(j + 1)
Next
End If
Next
nGridCount = nGridCount - n
End Sub
Private Function GetGridIndex(hwnd As Long) As Long
Dim i As Long
For i = 1 To nGridCount
If GridList(i).grdHwnd = hwnd Then
GetGridIndex = i
Exit Function
End If
Next
End Function
然后在每个包含MSFlexGrid控件的窗体调用StartHook和EndHook这两个过程
例如:
Private Sub Form_Load()
StartHook Me
End Sub
Private Sub Form_Unload(Cancel As Integer)
EndHook Me
End Sub
这样就可以支持滚轮了