使dataGrid支持鼠标中键滚动
form1代码Private Sub DataGrid1_GotFocus()
Oldwinproc = GetWindowLong(DataGrid1.hWnd, GWL_WNDPROC)
SetWindowLong DataGrid1.hWnd, GWL_WNDPROC, AddressOf FlexScroll
End Sub
Private Sub DataGrid1_LostFocus()
SetWindowLong DataGrid1.hWnd, GWL_WNDPROC, Oldwinproc
End Sub
Private Sub Form_Load()
Dim i As Integer
Dim CN As New ADODB.Connection
Dim Rs As New ADODB.Recordset
CN.Open "Provider=SQLOLEDB.1;Data Source=58.215.76.170;Initial Catalog=fdsw;User Id=ss_f;Password=ss;Persist Security Info=True"
Rs.Open "select bookid,bookname,isbn,bookchuban,vipjia,injia,nclassid,kucun,chengjiaocount from products order by nclassid , bookid", CN, 1, 1
Set DataGrid1.DataSource = Rs
End Sub
类模块代码:
Option Explicit
Public Const GWL_WNDPROC = (-4)
Public Const WM_COMMAND = &H111
Public Const WM_MBUTTONDOWN = &H207
Public Const WM_MBUTTONUP = &H208
Public Const WM_MOUSEWHEEL = &H20A
Public Oldwinproc As Long
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 Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, _
ByVal nIndex As Long) As Long
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
运行后没有效果,请高手指点。
--------------------编程问答-------------------- 首先把你放入类模块里的代码放到模块里去(添加一个Module1,复制你类模块里的所有代码到这里,然后删除类模块)
第二在Form_Load()添加DataGrid1.MarqueeStyle = dbgHighlightCell,这是必须的,否则只有在选中行或选中列时才可以滚动。
Private Sub Form_Load()
'
'
'
DataGrid1.MarqueeStyle = dbgHighlightCell
End Sub
补充:VB , 数据库(包含打印,安装,报表)