1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | using System.Windows.Forms; namespace ListViewSharedScroll { class CustListView : ListView { public event ScrollEventHandler Scroll; private const int WM_HSCROLL = 0x114; private const int WM_VSCROLL = 0x115; protected virtual void OnScroll(ScrollEventArgs e) { ScrollEventHandler handler = this.Scroll; if (handler != null) handler(this, e); } protected override void WndProc(ref Message m) { base.WndProc(ref m); if (m.Msg == WM_VSCROLL) { // Trap WM_VSCROLL OnScroll(new ScrollEventArgs((ScrollEventType)(m.WParam.ToInt32() & 0xffff), -1, 0, ScrollOrientation.VerticalScroll)); } else if (m.Msg == WM_HSCROLL) { OnScroll(new ScrollEventArgs((ScrollEventType)(m.WParam.ToInt32() & 0xffff),-1,0,ScrollOrientation.HorizontalScroll)); } } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | using System; using System.Drawing; using System.Windows.Forms; using System.Runtime.InteropServices; namespace ListViewSharedScroll { public partial class Form1 : Form { public Form1() { InitializeComponent(); } [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam); int TopItemIndex = 0; CustListView ListView1 = new CustListView(); CustListView ListView2 = new CustListView(); private void Form1_Load(object sender, EventArgs e) { ListView1.Size = new Size(201, 146); ListView1.Margin = new Padding(3, 3, 3, 3); ListView1.Location = new Point(48, 85); ListView1.Scrollable = true; ListView1.View = View.Details; ListView2.Size = new Size(201, 146); ListView2.Margin = new Padding(3, 3, 3, 3); ListView2.Location = new Point(280, 85); ListView2.Scrollable = true; ListView2.View = View.Details; ListView1.Columns.Add("Header", 100); ListView1.Columns.Add("Details", 100); ListView2.Columns.Add("Header", 100); ListView2.Columns.Add("Details", 100); for (int i = 0; i < 50; i++) { ListView1.Items.Add(new ListViewItem(new string[] { "Alpha"+i.ToString(), "Some details"+i.ToString() })); ListView1.Items.Add(new ListViewItem(new string[] { "Bravo" + i.ToString(), "More details" + i.ToString() })); ListView2.Items.Add(new ListViewItem(new string[] { "Alpha" + i.ToString(), "Some details" + i.ToString() })); ListView2.Items.Add(new ListViewItem(new string[] { "Bravo" + i.ToString(), "More details" + i.ToString() })); } ListView1.Scroll += ListView1_Scroll; ListView2.Scroll += ListView1_Scroll; this.Controls.Add(ListView1); this.Controls.Add(ListView2); } private void ListView1_Scroll(object sender, ScrollEventArgs e) { if (e.ScrollOrientation == ScrollOrientation.VerticalScroll) { if (TopItemIndex != ListView1.TopItem.Index || TopItemIndex != ListView2.TopItem.Index) { if (TopItemIndex != ListView1.TopItem.Index) { ListView1.EnsureVisible(ListView1.TopItem.Index); ListView2.EnsureVisible(ListView1.TopItem.Index); TopItemIndex = ListView1.TopItem.Index; } else { ListView1.EnsureVisible(ListView2.TopItem.Index); ListView2.EnsureVisible(ListView2.TopItem.Index); TopItemIndex = ListView2.TopItem.Index; } } } if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll) { ScrollH(1); } } private void ScrollH(int pixelsToScroll) { const Int32 LVM_FIRST = 0x1000; const Int32 LVM_SCROLL = LVM_FIRST + 20; SendMessage(ListView1.Handle, LVM_SCROLL, (IntPtr)pixelsToScroll, IntPtr.Zero); SendMessage(ListView2.Handle, LVM_SCROLL, (IntPtr)pixelsToScroll, IntPtr.Zero); } } } |
Refernced articles:
http://stackoverflow.com/questions/1851620/handling-scroll-event-on-listview-in-c-sharp http://stackoverflow.com/questions/626315/winforms-listview-remembering-scrolled-location-on-reload
http://stackoverflow.com/questions/473148/c-sharp-listview-how-do-i-add-items-to-columns-2-3-and-4-etc
http://stackoverflow.com/questions/7146567/winforms-listview-not-showing-items-in-detailsview http://stackoverflow.com/questions/372034/how-do-i-listen-for-scrolling-in-a-listview http://stackoverflow.com/questions/372034/how-do-i-listen-for-scrolling-in-a-listview http://bytes.com/topic/c-sharp/answers/255418-scrolling-listview