Winfrom Panel Scroll End 的实现

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

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

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

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

 

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
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 @   无记  阅读(1299)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示