VB中如何用SendMessage在拖动ListView1的水平滚动条时,ListView2的水平条同步,是水平滚动条...

并且在手动改变Listview1列头宽度时,listivew2的列头宽度也同时改变,.... 并且在手动改变Listview1列头宽度时,listivew2的列头宽度也同时改变,. 展开
 我来答
若以下回答无法解决问题,邀请你更新回答
zx001z7d53
2014-05-06 · TA获得超过2万个赞
知道大有可为答主
回答量:2.4万
采纳率:52%
帮助的人:5687万
展开全部
'==================窗体代码 Form1.frm=========================
'添加两个ListView。ListView1、ListView2

Option Explicit

Private Sub Form_Load()
Dim Index As Long
ListView1.ColumnHeaders.Add , , "Column 1"
ListView1.LabelEdit = lvwManual
ListView1.View = lvwReport
ListView2.ColumnHeaders.Add , , "Column 1"
ListView2.LabelEdit = lvwManual
ListView2.View = lvwReport
For Index = 1 To 50
ListView1.ListItems.Add , , "Item " & CStr(Index)
ListView2.ListItems.Add , , "Item " & CStr(Index)
Next
ListViewSubClass ListView1.hwnd
ListViewSubClass ListView2.hwnd
End Sub

'===================模块代码 Module1.mod======================

Option Explicit
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
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 lpfnOldWinProcLV As Long 'old WindowProc address for ListView
Private Const GWL_WNDPROC As Long = (-4)
Private Const WM_VSCROLL As Long = &H115
Private Const WM_DESTROY As Long = &H2
Private Const SB_THUMBPOSITION As Long = 4
Private Const SB_THUMBTRACK As Long = 5
Private Const LVM_FIRST As Long = &H1000
Private Const LVM_SCROLL As Long = (LVM_FIRST + 20)

Public Function ListViewSubClass(hwnd As Long) As Boolean
'This function enables subclassing
ListViewSubClass = True
'Get the address for the previous window procedure
lpfnOldWinProcLV = GetWindowLong(hwnd, GWL_WNDPROC)
If lpfnOldWinProcLV = 0 Then
ListViewSubClass = False
Exit Function
End If
'The return value of SetWindowLong is the address of the previous procedure,
'so if it's not what we just got above, something went wrong.
If SetWindowLong(hwnd, GWL_WNDPROC, AddressOf ListViewWndProc) <> lpfnOldWinProcLV Then
ListViewSubClass = False
End If
End Function

Public Function ListViewUnSubClass(hwnd As Long) As Boolean
'Restore default window procedure
If SetWindowLong(hwnd, GWL_WNDPROC, lpfnOldWinProcLV) = 0 Then
ListViewUnSubClass = False
Else
ListViewUnSubClass = True
lpfnOldWinProcLV = 0
End If
End Function

Private Function ListViewWndProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Static bIgnore As Boolean
ListViewWndProc = CallWindowProc(lpfnOldWinProcLV, hwnd, uMsg, wParam, lParam)
If bIgnore Then
Exit Function
End If
'Determine the message that was received
Select Case uMsg
Case WM_VSCROLL
bIgnore = True
Select Case hwnd
Case Form1.ListView1.hwnd
If LoWord(wParam) = SB_THUMBTRACK Then
'拖动滚动条时并没有使另一个ListView发生滚动
'增加了相应的处理
'The high-order word specifies the current position of the scroll box if the low-order word is SB_THUMBPOSITION or SB_THUMBTRACK; otherwise, this word is not used.
'The low-order word specifies a scroll bar value that indicates the user's scrolling request. This parameter can be one of the following values.
'SB_THUMBPOSITION
'The user has dragged the scroll box (thumb) and released the mouse button. The high-order word indicates the position of the scroll box at the end of the drag operation.
'SB_THUMBTRACK
'The user is dragging the scroll box. This message is sent repeatedly until the user releases the mouse button. The high-order word indicates the position that the scroll box has been dragged to.
Call SendMessage(Form1.ListView2.hwnd, LVM_SCROLL, 0, -65535) '先向上回滚到顶部
Call SendMessage(Form1.ListView2.hwnd, LVM_SCROLL, 0, HiWord(wParam) * 13)
'滚动到指定位置,13这个常数是试验出来的,在不同的系统或ListView每列高度不同时可能要修改
End If
'Send the message to ListView2
Call SendMessage(Form1.ListView2.hwnd, uMsg, wParam, lParam)
Case Form1.ListView2.hwnd
'Send the message to ListView1
If LoWord(wParam) = SB_THUMBTRACK Then
Call SendMessage(Form1.ListView1.hwnd, LVM_SCROLL, 0, -65535) '向上回滚
Call SendMessage(Form1.ListView1.hwnd, LVM_SCROLL, 0, HiWord(wParam) * 13) '滚动到
End If
Call SendMessage(Form1.ListView1.hwnd, uMsg, wParam, lParam)
End Select
bIgnore = False
Case WM_DESTROY
Call CallWindowProc(lpfnOldWinProcLV, hwnd, uMsg, wParam, lParam)
Call ListViewUnSubClass(hwnd)
ListViewWndProc = CallWindowProc(lpfnOldWinProcLV, hwnd, uMsg, wParam, lParam)
End Select
End Function

Public Function LoWord(num As Long) As Integer
LoWord = num Mod &H10000
End Function

Public Function HiWord(num As Long) As Integer
HiWord = (num And &HFFFF0000) / &H10000
End Function
更多追问追答
追问
网上的代码了,是垂直的同步,要水平滑动条的同步
追答
垂直拖到头就水平同步了
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式