在列表视图处理滚动事件在c#

在列表视图处理滚动事件在c#

本文介绍了在列表视图处理滚动事件在c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用一个BackgroundWorker生成缩略图列表视图。当列表视图被滚动我想暂停BackgroundWorker的,并获得滚动区域的当前值,当用户停止滚动列表视图,恢复的BackgroundWorker从项目按照滚动区域的值开始。

I have a listview that generates thumbnail using a backgroundworker. When the listview is being scrolled i want to pause the backgroundworker and get the current value of the scrolled area, when the user stopped scrolling the listview, resume the backgroundworker starting from the item according to the value of the scrolled area.

是否可以处理滚动列表视图的事件?如果是如何?如果不按什么上述我那么什么是一个很好的选择?

Is it possible to handle scroll event of a listview? if yes how? if not then what is a good alternative according to what i described above?

推荐答案

您将不得不支持添加到ListView类,因此你可以通知有关滚动事件。添加一个新类到项目,并粘贴以下code。编译。从工具箱的上方新的ListView控件到窗体。实施新的滚动事件的处理程序。

You'll have to add support to the ListView class so you can be notified about scroll events. Add a new class to your project and paste the code below. Compile. Drop the new listview control from the top of the toolbox onto your form. Implement a handler for the new Scroll event.

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通知知道当用户停止滚动。还有什么可以帮助您检测用户开始滚动。例如:

Beware that the scroll position (ScrollEventArgs.NewValue) isn't meaningful, it depends on the number of items in the ListView. I forced it to 0. Following your requirements, you want to watch for the ScrollEventType.EndScroll notification to know when the user stopped scrolling. Anything else helps you detect that the user started scrolling. For example:

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;
}

这篇关于在列表视图处理滚动事件在c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 00:40