问题描述
我有一个使用后台工作者生成缩略图的列表视图.当列表视图正在滚动时,我想暂停后台工作人员并获取滚动区域的当前值,当用户停止滚动列表视图时,根据滚动区域的值从项目开始恢复后台工作人员.
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 类的支持,以便您可以收到有关滚动事件的通知.向您的项目添加一个新类并粘贴下面的代码.编译.将新的列表视图控件从工具箱顶部拖放到您的表单上.为新的 Scroll 事件实现一个处理程序.
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# 中处理列表视图上的滚动事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!