C# winform 移动绘制圆形

C# winform 移动绘制圆形

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace DrawImage
{
    public partial class Form1 : Form
    {
        private float c_cx;   // 圆心 x坐标
        private float c_cy;   // 圆心 y坐标
        private List<OffsetPoint> pointLst;    // 偏移点坐标
        private int indexSelect = 0;  // 当前选择索引

        public Form1()
        {   
            // 消除移动的闪动
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
            SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲

            InitializeComponent();

            // 初始化圆心点
            this.c_cx = this.Width / 2;
            this.c_cy = this.Height / 2;

            // 添加中心偏移点
            pointLst = new List<OffsetPoint>();
            pointLst.Add(new OffsetPoint(100, -100));
            pointLst.Add(new OffsetPoint(0, 0));
            pointLst.Add(new OffsetPoint(0, 50));
            pointLst.Add(new OffsetPoint(0, -50));
            pointLst.Add(new OffsetPoint(-100, 100));
            pointLst.Add(new OffsetPoint(-100, -100));
            pointLst.Add(new OffsetPoint(100, 100));
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 测试点移动 
            if (this.indexSelect % 7 == 0)
                this.indexSelect = 0;

            // 获取并计算 目标坐标点
            OffsetPoint curPoint = this.pointLst[indexSelect];
            int nTgCx= this.Width / 2 + curPoint.o_x;
            int nTgCy = this.Height / 2 + curPoint.o_y;
            System.Diagnostics.Debug.WriteLine(nTgCx.ToString(), nTgCy.ToString() );

            // 启动线程  移动点
            ThreadPool.QueueUserWorkItem(state => movePoint(nTgCx, nTgCy));
            this.indexSelect += 1;
        }

        // 移动并显示点
        private void movePoint(int nTgCx, int nTgCy) 
        {
            // 计算与目标偏移距离 单位偏移距离
            float all_diff_x = System.Math.Abs(this.c_cx - nTgCx) * 0.01f; 
            float all_diff_y = System.Math.Abs(this.c_cy - nTgCy) * 0.01f;

            while (true)
            {   
                // 判断是否移动完成
                float cx_diff = System.Math.Abs(this.c_cx - nTgCx);
                float cy_diff = System.Math.Abs(this.c_cy - nTgCy);
                if (cx_diff <= 0.001 && cy_diff <= 0.001) break;

                Thread.Sleep(5);
                // 修改绘制点坐标
                this.c_cx = this.c_cx > nTgCx ? this.c_cx - all_diff_x : this.c_cx + all_diff_x;
                this.c_cy = this.c_cy > nTgCy ? this.c_cy - all_diff_y : this.c_cy + all_diff_y;

                // 重绘窗体
                this.Invalidate();
            }
            
        }
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            // 提升显示效果
            e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            // 高清显示
            e.Graphics.SmoothingMode = SmoothingMode.HighQuality;  //图片柔顺模式选择
            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;//高质量
            e.Graphics.CompositingQuality = CompositingQuality.HighQuality;//再加一点

            // 显示颜色
            Color drawColor = Color.FromArgb(0, 0, 255);

            // 半径长度
            int cirR = 10;
            // 转换为 int
            int cx = Convert.ToInt32(System.Math.Round(this.c_cx));
            int cy = Convert.ToInt32(System.Math.Round(this.c_cy));

            // 绘制圆形
            e.Graphics.DrawEllipse(new Pen(drawColor, 10), cx - cirR, cy - cirR, cirR * 2, cirR * 2);
        }
    }

    // 偏移点
    public class OffsetPoint
    {
        public int o_x;
        public int o_y;
        public OffsetPoint(int o_x, int o_y)
        {
            this.o_x = o_x;
            this.o_y = o_y;
        }
    }
}

在这里插入图片描述

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

廷益--飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值