优秀的编程知识分享平台

网站首页 > 技术文章 正文

C# 绘制旋"四圆同轴转动"(WinForm)

nanyue 2024-11-12 11:47:09 技术文章 1 ℃

变量:

private int circleSize = 150;
private int[] circleX = { 100, 200, 100, 200 };
private int[] circleY = { 100, 100, 200, 200 };
private Color[] colors = { Color.Red, Color.Blue, Color.Green, Color.Yellow };
private int[] angles = { 0, 90, 180, 270 };
private int angleIncrement = 5;
private string[] texts = { "美", "人", "爱", "你" };

绘制四圆图案:

 private void Form1_Paint(object sender, PaintEventArgs e)
 {
    
     for (int i = 0; i < 3; i++)
     {
         for (int j = i + 1; j < 4; j++)
         {
             e.Graphics.DrawLine(new Pen(Color.FromArgb(100, Color.Wheat), 2), circleX[i] + circleSize / 2, circleY[i] + circleSize / 2, circleX[j] + circleSize / 2, circleY[j] + circleSize / 2); // 绘制透明直线
         }
     }
     for (int i = 0; i < 4; i++)
     {
         e.Graphics.FillEllipse(new SolidBrush(colors[i]), circleX[i], circleY[i], circleSize, circleSize); // 绘制圆
         SizeF textSize = e.Graphics.MeasureString(texts[i], new Font("Arial", 36)); // 计算文字的尺寸
         float x = circleX[i] + (circleSize - textSize.Width) / 2; // 计算文字的水平位置
         float y = circleY[i] + (circleSize - textSize.Height) / 2; // 计算文字的垂直位置
         e.Graphics.DrawString(texts[i], new Font("Arial", 36), new SolidBrush(Color.Fuchsia), x, y); // 添加文字
     }
 }

时间控件:

private void timer1_Tick(object sender, EventArgs e)
{
    for (int i = 0; i < 4; i++)
    {
        angles[i] = (angles[i] + angleIncrement) % 360; // 增加角度,并确保在0到359之间
        circleX[i] = (int)(150 + 100 * Math.Cos(angles[i] * Math.PI / 180)); // 根据角度计算新的圆心坐标
        circleY[i] = (int)(150 + 100 * Math.Sin(angles[i] * Math.PI / 180));
    }
    Invalidate(); // 重绘窗体
}

#文章首发挑战赛##头条创作挑战赛##直击南方多地降雪现场#

Tags:

最近发表
标签列表