Winfrom Panel Scroll End 的实现

场景:在一个panel里面有非常多的自定义绘制的控件,在拖拉滚动条的时候,控件的画面上有残影

不知道大家遇到过这种情况没,一直做web的winform经验太少,有更好的解决办法请贡献

首先放出我的解决思路:需要再滚动停止的时候重绘一下控件,panel的所有事件都加了一个打印输出,发现,滚动条在滚动的时候只有一个Scrol事件,是在滚动时候产生的,问题在于轻轻拖动滚动条,就会一瞬间产生N个Scrol,如果直接这个事件里面放重绘代码,太消耗资源,第二这个滚动条除了滚动事件,别的事件都没有,比如鼠标moseup mosedown等

实在没有办法,写了一个类变现的实现滚动停止的时候执行自己的方法,代码如下,写的不好,抛砖引玉,希望能有更好的解决方式

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Controller
{
    public delegate void VoidNotify();
    /// <summary>
    /// 滚动停止事件
    /// </summary>
    public class ScrollEnd
    {
        private static ScrollEnd instance;
        private static object Lock = new object();
        /// <summary>
        /// 滚动条事件计数
        /// </summary>
        private static int count = 0;
        /// <summary>
        /// Thread Loop 计数器
        /// </summary>
        private static int count2 = 0;

        /// <summary>
        /// 通知自定义事件
        /// </summary>
        public static VoidNotify Notify { get; set; }

        private Thread thread = null;

        private ScrollEnd()
        {

        }
        public static ScrollEnd GetInstance()
        {
            if (instance == null)
            {
                lock (Lock)
                {
                    if (instance == null)
                    {
                        instance = new ScrollEnd();
                    }
                }
            }
            return instance;
        }

        /// <summary>
        /// 滚动条事件触发ADD
        /// </summary>
        public void Add()
        {
            lock (Lock)
            {
                if (count2 == 0)
                {
                    thread = new Thread(new ThreadStart(Run));
                    thread.IsBackground = true;
                    thread.Start();
                }
                else
                {
                    count++;
                }
            }
        }

        /// <summary>
        /// 通过thread的间隔计数跟滚动事件的计数对比,判断滚动结束,然后执行自定义事件
        /// </summary>
        private void Run()
        {
            while (true)
            {
                if (count2 > count)
                {
                    break;
                }
                count2++;
                Thread.Sleep(30);
            }
            if (Notify!=null)
            {
                Notify();
            }
            count = 0;
            count2 = 0;
            thread = null;
            return;
        }
    }
}

  

posted @ 2015-05-06 11:06  无记  阅读(1272)  评论(0编辑  收藏  举报