C# winform 移动绘制圆形

编程入门 行业动态 更新时间:2024-10-10 23:20:39

C# winform 移动绘制<a href=https://www.elefans.com/category/jswz/34/1769013.html style=圆形"/>

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;// 转换为 intint 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;}}
}

更多推荐

C# winform 移动绘制圆形

本文发布于:2024-03-07 09:09:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1717412.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:圆形   winform

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!