鼠标滑动或横拉用户控件无闪缩

前言

一、重写容器

二、添加用户控件并且生成图片

添加用户控件

2.把用户控件生成图片

总结


前言

目标处理在容器中添加大量用户控件后下拉闪缩问题


一、重写容器

在容器中添加鼠标按下,移动,释放,重绘等操作,并且在移动或者重绘中添加滚动范围界限。

cs 复制代码
 public class PanelMouse : Panel
    {
        public PanelMouse()
        {
            SetStyle(ControlStyles.AllPaintingInWmPaint |
                    ControlStyles.OptimizedDoubleBuffer, true);
        }
        private Point lastMousePosition;
        private bool isDragging = false;
        public int scrollOffset = 0;
        public int contentWidth = 0; // 内容的实际宽度
        public Image imagePanel;

        protected override void OnMouseDown(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isDragging = true;
                lastMousePosition = e.Location;
                //Console.WriteLine("mousedown:" + lastMousePosition.X + ";" + lastMousePosition.Y);
            }
            base.OnMouseDown(e);
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (isDragging)
            {
                int offsetX = 0;
                offsetX = e.X - lastMousePosition.X;
                // 更新滚动偏移
                scrollOffset += offsetX;
                // 限制滚动范围
                scrollOffset = Math.Max(Math.Min(scrollOffset, 0), -Math.Max(0, contentWidth - this.Width));
                lastMousePosition = e.Location;
                //Console.WriteLine("mouseMove:" + lastMousePosition.X + ";" + lastMousePosition.Y+";"+ scrollOffset);

                this.Refresh();
            }
            base.OnMouseMove(e);
        }
        protected override void OnMouseUp(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isDragging = false;
            }
            base.OnMouseUp(e);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            if (imagePanel != null)
            {
                scrollOffset = Math.Max(Math.Min(scrollOffset, 0), -Math.Max(0, contentWidth - this.Width));
                e.Graphics.DrawImage(imagePanel, scrollOffset, 0, imagePanel.Width, imagePanel.Height);
            }
            base.OnPaint(e);
        }
    }

二、添加用户控件并且生成图片

添加用户控件

cs 复制代码
 public partial class MsgUC : UserControl
    {
        public MsgUC(int num)
        {
            InitializeComponent();
            this.lblsubject.Text = "测试"+num;
        }
    }
复制代码

2.把用户控件生成图片

cs 复制代码
for (int i = 0; i < 20; i++)
            {
                MsgUC msgUC = new MsgUC(i);
                msgUCs.Add(msgUC);
            }
            bimg = new Bitmap(msgUCs.Count * msgUCs[0].Width + msgUCs.Count * 8, msgUCs[0].Height);
            Graphics g = Graphics.FromImage(bimg);
            for (int i = 0; i < msgUCs.Count; i++)
            {
                Bitmap bmp = new Bitmap(msgUCs[i].Width, msgUCs[i].Height);
                msgUCs[i].DrawToBitmap(bmp, new Rectangle(Point.Empty, bmp.Size));
                //然后,将这个Bitmap绘制到Panel上
                g.DrawImage(bmp, i * msgUCs[i].Width + i * 8, 0, bmp.Width, bmp.Height);
            }
            g.Dispose();
            panelMouse1.imagePanel = bimg;
            panelMouse1.contentWidth = bimg.Width;

后续需要锚定位置可以设置scrollOffset参数。


总结

此文章主要处理在容器中有大量的用户控件导致下拉闪缩问题。而且可以延伸出用GDI绘画出页面并生成图片展示然后利用用户的点击位置坐标来触发用户控件的点击事件。

代码相关链接:https://download.csdn.net/download/qq_35319925/92366446

相关推荐
bjzhang7517 分钟前
Dorisoy.AMS--一款采用C# WinForm框架+SQLite数据库的企业/机构资产管理解决方案
sqlite·c#·资产管理
零度@1 小时前
Java消息中间件-Kafka全解(2026精简版)
java·kafka·c#·linq
2501_941882482 小时前
在开普敦跨区域部署环境中构建高可靠分布式配置中心的设计思路与实现实践
开发语言·c#
zxy28472253013 小时前
利用C#的BotSharp本地部署第一个大模型AI Agent示例(1)
人工智能·c#·对话·ai agent·botsharp
刘97533 小时前
【第25天】25c#今日小结
java·开发语言·c#
玩泥巴的4 小时前
基于.NET操作Excel COM组件生成数据透视报表
c#·.net·excel·二次开发·com互操作
马达加斯加D5 小时前
分布式系统开发核心问题总结
c#
钰fly5 小时前
DataGridView 与 DataTable 与csv 序列
前端·c#
Kapaseker5 小时前
C# 斩获TIOBE年度编程语言
c#·编程语言
工业甲酰苯胺5 小时前
使用 C# 和 SQL Server 自动化邮件中的用户分配数据处理
数据库·c#·自动化