C#/WinForm 自定义控件绘制章鱼

1.创建OctopusControl类,继承Control类

2.定义定时器刷新,这里我们使用System.Timers.Timer定时器,逻辑处理与UI线程分开以便减少对UI线程的负载,如下:

_timer_Elapsed事件主要用定时生成数据供UI现场绘制图像,如下:

3.重写OnPaint方法,供UI线程绘制图像,最好用DrawRectangles绘制,如下:

到此基本完成,但是绘制动画图像我们一般需要开启双缓冲,防止动画闪烁,增加动画流畅,如下:

全部代码:

cs 复制代码
 public class OctopusControl : Control
    {
        private readonly System.Timers.Timer _timer;
        public OctopusControl()
        {
            SetStyle(
          ControlStyles.ResizeRedraw
          | ControlStyles.DoubleBuffer
          | ControlStyles.UserPaint
          | ControlStyles.AllPaintingInWmPaint
          | ControlStyles.SupportsTransparentBackColor,
          true
          );

            this.BackColor = Color.Black;
            _timer = new System.Timers.Timer();
            _timer.Interval = 50;
            _timer.Elapsed += _timer_Elapsed;
        }
        public void Start()
        {
            _timer.Start();
        }
        public void Stop() { _timer.Stop(); }
        private double t = 0d;
        RectangleF[] doubles=new RectangleF[200*200];
        private volatile bool _isRun = false;
        private void _timer_Elapsed(object? sender, System.Timers.ElapsedEventArgs e)
        {
            if (_isRun) return;
            _isRun = true;
            t += Math.PI / 120;
            int j = 0;
            for (int y = 100; y < 300; y++)
            {
                for (int x = 100; x < 300; x++)
                {
                    var k = x / 8f - 25;
                    var e2 = y / 8f - 25;
                    var d = 5 * Math.Cos(Mag(k, e2) / 3);
                    var newX = (x + (d * k + k * 2 * Math.Atan(9 * Math.Cos(e2 * 2 - t / 2))) * Math.Sin(d * 2.5d - t)) / 2 + 100;
                    var newY = d * 13 + d * 3 * (3 + Math.Cos(d * 3 - t)) + d * e2 + 200;
                    doubles[j++] = new RectangleF((float)newX, (float)newY, 0.1f, 0.1f);
                }
            }
            this.Invalidate();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            g.Smooth();
            Draw(g);
            g.Smooth(false);
        }
        private void Draw(Graphics g)
        {
            if (!_isRun) return;

            Stopwatch stopwatch = Stopwatch.StartNew();

            g.DrawRectangles(Pens.Green, doubles);
            stopwatch.Stop();
            g.DrawString($"{stopwatch.ElapsedMilliseconds}ms", this.Font, Brushes.Green, 10, 10);

            _isRun = false;
        }
        private double Mag(double x1, double y1) => Dist(0, 0, x1, y1);
        private double Dist(double x1, double y1, double x2, double y2) => Math.Sqrt(Math.Pow(x1 - x2, 2) + Math.Pow(y1 - y2, 2));

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }
    }
相关推荐
谢平康几秒前
在IDEA里用XDebug调试PHP,断点....
开发语言·php·bug
Pandaconda4 分钟前
【计算机网络 - 基础问题】每日 3 题(三十四)
开发语言·经验分享·笔记·后端·计算机网络·面试·职场和发展
Minyy1122 分钟前
小程序项目实践(一)--项目的初始化以及前期的准备工作
开发语言·前端·git·小程序·gitee·uni-app
今晚吃什么呢?22 分钟前
module
开发语言·javascript·ecmascript
碳苯29 分钟前
【rCore OS 开源操作系统】Rust 异常处理
开发语言·人工智能·后端·rust·操作系统·os
尘浮生1 小时前
Java项目实战II基于Java+Spring Boot+MySQL的桂林旅游景点导游平台(源码+数据库+文档)
java·开发语言·数据库·spring boot·mysql·maven·intellij-idea
这孩子叫逆1 小时前
axios 使用
开发语言·javascript·ecmascript
careathers1 小时前
【Python】物流行业数据分析与可视化案例
开发语言·python·数据分析
多多*1 小时前
OJ在线评测系统 微服务高级 网关跨域权限校验 集中解决跨域问题 拓展 JWT校验和实现接口限流降级
java·开发语言·微服务·云原生·架构·maven
另类程序员1321 小时前
安装jdk安装开发环境与maven
java·开发语言