Hi, ich muß zwei ListView´s vertikal synchron scrollen. Nach ausgiebiger Suche im Internet habe ich was Ähnliches mit ListBoxen in C# gefunden.
Es wird eine neue Klasse des ListViews erzeugt die von ListView erbt. Hier wird ein neues Ereignis 'Scroll' und eine neue Methode 'ScrollToPosition' implementiert.
Den Code habe ich soweit nach VB portiert.
Die Funktion ist aber nur halb gegeben. Wenn ich in einem ListView scrolle, dann wird im jeweils anderen die Scroll-Leiste mitgescrollt, aber nicht der Inhalt.
Hier der Code der neuen ListView-Klasse
Imports System
Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Public Delegate Sub ScrollEventHandler(ByVal sender As Object, ByVal e As _
System.EventArgs)
Public Class ScrollEventArgs
Inherits EventArgs
Private _si As MyListView.NativeMethods.SCROLLINFO
Public ScrollEventArgs
Public Property SINFO() As MyListView.NativeMethods.SCROLLINFO
Get
Return _si
End Get
Set(ByVal value As MyListView.NativeMethods.SCROLLINFO)
_si = value
End Set
End Property
End Class
Public Class MyListView
Inherits ListView
Public Event Scroll As ScrollEventHandler
Protected Sub OnScroll(ByVal e As ScrollEventArgs)
RaiseEvent Scroll(Me, e)
End Sub
Protected Overrides Sub WndProc(ByRef m As Message)
MyBase.WndProc(m)
If m.Msg = NativeMethods.WM_VSCROLL Or m.Msg = _
NativeMethods.SBM_SETSCROLLINFO Or m.Msg = _
NativeMethods.WM_MOUSEWHEEL Then
RaiseScrollEvent(m.HWnd)
End If
End Sub
Protected Overrides Sub OnKeyDown(ByVal e As KeyEventArgs)
MyBase.OnKeyDown(e)
RaiseScrollEvent(Me.Handle)
End Sub
Private Sub RaiseScrollEvent(ByRef handle)
Dim si As MyListView.NativeMethods.SCROLLINFO = New _
MyListView.NativeMethods.SCROLLINFO
si.fMask = NativeMethods.ScrollInfoMask.SIF_ALL
si.cbSize = Marshal.SizeOf(si)
NativeMethods.GetScrollInfo(Me.Handle, NativeMethods.SB_VERT, si)
Dim e As ScrollEventArgs = New ScrollEventArgs()
e.SINFO = si
OnScroll(e)
End Sub
Public Sub ScrollToPosition(ByVal e As ScrollEventArgs)
Dim si As MyListView.NativeMethods.SCROLLINFO = e.SINFO
Dim siOwn As MyListView.NativeMethods.SCROLLINFO = New _
MyListView.NativeMethods.SCROLLINFO
siOwn.fMask = NativeMethods.ScrollInfoMask.SIF_ALL
siOwn.cbSize = Marshal.SizeOf(si)
NativeMethods.GetScrollInfo(Me.Handle, NativeMethods.SB_VERT, siOwn)
If (siOwn.nPos <> si.nPos) Then
NativeMethods.SetScrollInfo(Me.Handle, NativeMethods.SB_VERT, si, _
True)
NativeMethods.SendMessageLong(Me.Handle, NativeMethods.WM_VSCROLL, _
NativeMethods.SB_THUMBTRACK + &H10000 * si.nPos, 0)
End If
End Sub
Public Class NativeMethods
Public Structure SCROLLINFO
Public cbSize As Integer
Public fMask As Integer
Public nMin As Integer
Public nMax As Integer
Public nPage As Integer
Public nPos As Integer
Public nTrackPos As Integer
End Structure
Public Enum ScrollBarDirection
SB_HORZ = 0
SB_VERT = 1
SB_CTL = 2
SB_BOTH = 3
End Enum
Public Enum ScrollInfoMask
SIF_RANGE = &H1
SIF_PAGE = &H2
SIF_POS = &H4
SIF_DISABLENOSCROLL = &H8
SIF_TRACKPOS = &H10
SIF_ALL = (SIF_RANGE Or SIF_PAGE Or SIF_POS Or SIF_TRACKPOS)
End Enum
Public Const SBM_SETSCROLLINFO = &HE9
Public Const WM_VSCROLL = &H115
Public Const WM_MOUSEWHEEL = &H20A
Public Const SB_VERT = &H1
Public Const SB_THUMBTRACK = &H5
Declare Function GetScrollInfo Lib "user32" (ByVal hWnd As IntPtr, _
ByVal fnBar As ScrollBarDirection, ByRef lpsi As SCROLLINFO) As _
Integer
Public Declare Function SetScrollInfo Lib "user32" (ByVal hWnd As _
IntPtr, ByVal fnBar As ScrollBarDirection, ByRef lpsi As SCROLLINFO, _
ByVal bRepaint As Boolean) As Integer
Public Declare Function SendMessageLong Lib "user32.dll" Alias _
"SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As _
Long, ByVal lParam As Long) As Long
End Class
End Class Der Aufruf im Formular erfolgt so:
Private Sub MyListView1_Scroll(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MyListView1.Scroll
MyListView2.ScrollToPosition(e)
End Sub
Private Sub MyListView2_Scroll(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MyListView2.Scroll
MyListView1.ScrollToPosition(e)
End Sub
End Class Bitte um Hilfe!
Gruß Dot 0 |