优秀的编程知识分享平台

网站首页 > 技术文章 正文

C# 绚丽动态圆形动感彩灯(WinForm)

nanyue 2024-11-12 11:46:50 技术文章 1 ℃

绘制动态图案:

 protected override void OnPaint(PaintEventArgs e)
 {
     base.OnPaint(e);
     Graphics g = e.Graphics;
     g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

     int centerX = 260;
     int centerY = 230;
     int radius = 200;

     // 绘制图案
     for (int phi = -90; phi < 90; phi += 10)
     {
         for (int theta = 0; theta < 360; theta += 10)
         {
             double x1 = centerX + radius * Math.Cos(theta * Math.PI / 180) * Math.Cos(phi * Math.PI / 180);
             double y1 = centerY + radius * Math.Sin(theta * Math.PI / 180) * Math.Cos(phi * Math.PI / 180);
             double z1 = radius * Math.Sin(phi * Math.PI / 180);

             double x2 = centerX + radius * Math.Cos(theta * Math.PI / 180) * Math.Cos((phi + 20) * Math.PI / 180);
             double y2 = centerY + radius * Math.Sin(theta * Math.PI / 180) * Math.Cos((phi + 20) * Math.PI / 180);
             double z2 = radius * Math.Sin((phi + 20) * Math.PI / 180);

             g.DrawLine(Pens.Red, (float)x1, (float)y1, (float)x2, (float)y2);

             // 绘制端点的小圆
             g.FillEllipse(Brushes.Fuchsia, (float)x1 - 2, (float)y1 - 4, 8, 8);
             g.FillEllipse(Brushes.Yellow, (float)x2 - 2, (float)y2 - 4, 8, 8);
         }
     }
 }

变量与时间控件:

private Timer timer;
private float angleX = 20;
private float angleY = 20;
public Form1()
{
    InitializeComponent();
    timer = new Timer();
    timer.Interval = 50; // 设置定时器间隔为50ms
    timer.Tick += Timer_Tick;
    timer.Start();
}

private void Timer_Tick(object sender, EventArgs e)
{   
    angleX += 50;
    angleY += 30;
    Invalidate();
}

#头条创作挑战赛##冬日生活打卡季##贵州221起森林火情全部扑灭#

Tags:

最近发表
标签列表