使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=13017755562;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
运行后没有效果,请高手指点。
--------------------编程问答-------------------- 类模块代码:
.....
....
这些代码得放在标准模块里面。 --------------------编程问答-------------------- 类模块代码:
我写错,是放在模块代码里 --------------------编程问答-------------------- 调试过没?在窗口函数内先下个断点(有点危险,记得先保存)看一下sendkeys 有没有执行,或者该控件的pagedown按键的事件会处理吗?或者当前光标定位不在该控制上,而上控件之内的子控件上,那你的sendkeys能滚动吗? --------------------编程问答-------------------- 首先把你放入类模块里的代码放到模块里去(添加一个Module1,复制你类模块里的所有代码到这里,然后删除类模块)
第二在Form_Load()添加DataGrid1.MarqueeStyle = dbgHighlightCell,这是必须的,否则只有在选中行或选中列时才可以滚动。
Private Sub Form_Load()
'
'
'
DataGrid1.MarqueeStyle = dbgHighlightCell
End Sub
--------------------编程问答-------------------- 加上你给的语句是可以了,
滚动是一页一页的,
能不能修改成一行一行滚动呢?
请给出修改后的代码。 --------------------编程问答-------------------- 查了一上资料,应如下。
Case -7864320 '向下滚
SendKeys "{DowN}"
Case 7864320 '向上滚
SendKeys "{UP}" --------------------编程问答-------------------- 有没有更好的办法 --------------------编程问答-------------------- 这个是可以的 --------------------编程问答-------------------- 向高手学习了!
补充:VB , API