2009-12-05 105 views
12

我有一个使用backgroundworker生成缩略图的listview。当列表视图正在滚动时,我想暂停背景工作并获取滚动区域的当前值,当用户停止滚动列表视图时,根据滚动区域的值从项目开始恢复背景工作。c处理listview中的scroll事件#

是否有可能处理列表视图的滚动事件?如果是的话如何?如果不是,那么根据我上面所描述的,什么是一个好的选择?

回答

16

您必须添加对ListView类的支持,以便您可以收到关于滚动事件的通知。为您的项目添加一个新类并粘贴下面的代码。编译。将新的listview控件从工具箱的顶部拖放到表单上。为新的Scroll事件实现一个处理程序。

using System; 
using System.Windows.Forms; 

    class MyListView : ListView { 
     public event ScrollEventHandler Scroll; 
     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 == 0x115) { // Trap WM_VSCROLL 
      OnScroll(new ScrollEventArgs((ScrollEventType)(m.WParam.ToInt32() & 0xffff), 0)); 
     } 
     } 
    } 

当心滚动位置(ScrollEventArgs.NewValue)是没有意义的,这取决于在ListView的项目数。我将它强制为0.遵循您的要求,您需要查看ScrollEventType.EndScroll通知,以了解用户何时停止滚动。其他任何东西都可以帮助您检测到用户开始滚动。例如:

ScrollEventType mLastScroll = ScrollEventType.EndScroll; 

private void myListView1_Scroll(object sender, ScrollEventArgs e) { 
    if (e.Type == ScrollEventType.EndScroll) scrollEnded(); 
    else if (mLastScroll == ScrollEventType.EndScroll) scrollStarted(); 
    mLastScroll = e.Type; 
} 
+0

非常感谢nobugz。这正是我想要实现的。 – murasaki5 2009-12-05 12:34:58

+3

阅读http://stackoverflow.com/questions/1176703/listview-onscroll-event/1182232#1182232看到WM_VSCROLL消息的一些限制。 – Grammarian 2009-12-06 07:59:49

+0

这个答案已经解决了网格框架2.0与网格线上的一个讨厌的bug – Mandrake 2011-05-27 17:52:51

1

看到这个职位ListView Scroll Event

使用原生窗口类听 有关 列表框的滚动消息。将与任何控制工作。

+0

根据@Adriaan Stander发布我的课程提出滚动事件的帖子如下。 http://stackoverflow.com/a/35645892/254215 – Dib 2016-02-26 07:27:03

0

捕获滚动事件现在很容易在.NET做4

赶上Loaded事件从您的ListView(m_ListView),并做到这一点:

 if (VisualTreeHelper.GetChildrenCount(m_ListView) != 0) 
     { 
      Decorator border = VisualTreeHelper.GetChild(m_ListView, 0) as Decorator; 
      ScrollViewer sv = border.Child as ScrollViewer; 
      sv.ScrollChanged += ScrollViewer_ScrollChanged; 
     } 

然后,实现您的ScrollViewer_ScrollChanged功能:

private void ScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e) 
    { 
     ... 
    } 
+1

这是针对WPF的。有用的信息,但问题是Windows窗体。 – flobadob 2014-11-07 16:05:59

0

根据@Adriaan Stander的职位d我的课程提出滚动事件在下面。

internal class ControlScrollListener : NativeWindow, IDisposable 
{ 
    public event ControlScrolledEventHandler ControlScrolled; 
    public delegate void ControlScrolledEventHandler(object sender, EventArgs e); 

    private const uint WM_HSCROLL = 0x114; 
    private const uint WM_VSCROLL = 0x115; 
    private readonly Control _control; 

    public ControlScrollListener(Control control) 
    { 
     _control = control; 
     AssignHandle(control.Handle); 
    } 

    protected bool Disposed { get; set; } 

    public void Dispose() 
    { 
     Dispose(true); 
     GC.SuppressFinalize(this); 
    } 

    private void Dispose(bool disposing) 
    { 
     if (Disposed) return; 

     if (disposing) 
     { 
      // Free other managed objects that implement IDisposable only 
     } 

     // release any unmanaged objects 
     // set the object references to null 
     ReleaseHandle(); 

     Disposed = true; 
    } 

    protected override void WndProc(ref Message m) 
    { 
     HandleControlScrollMessages(m); 
     base.WndProc(ref m); 
    } 

    private void HandleControlScrollMessages(Message m) 
    { 
     if (m.Msg == WM_HSCROLL | m.Msg == WM_VSCROLL) 
     { 
      if (ControlScrolled != null) 
      { 
       ControlScrolled(_control, new EventArgs()); 
      } 
     } 
    } 
} 

使用它是这样的...

声明一个字段:

_processListViewScrollListener = new ControlScrollListener(ProcessesListView); 

丝:

private ControlScrollListener _processListViewScrollListener; 

与您需要知道issrolling控件中实例化处理者:

_processListViewScrollListener.ControlScrolled += ProcessListViewScrollListener_ControlScrolled; 

处理函数的事件:

void ProcessListViewScrollListener_ControlScrolled(object sender, EventArgs e) 
{ 
    // do what you need to do 
} 

在引发事件的事件参数可以调整,以包含更多有用的信息。我只需要知道我的控制已经滚动!

+0

不处理鼠标滚轮。必须使用滚动条。如果你想要的话,需要添加'private const uint WM_MOUSEWHEEL = 0x020A;'。 – rism 2017-12-18 07:10:40