优秀的编程知识分享平台

网站首页 > 技术文章 正文

C# 绘制旋转六边形(WinForm)(c# 控件旋转)

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

变量:

private bool isClockwise = true;
private float angle = 0;
public Form1()
{
    InitializeComponent();
    DoubleBuffered = true;
}

绘制图案:

private void Form1_Paint(object sender, PaintEventArgs e)
{
    Graphics g = e.Graphics;
    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

    int centerX = this.Width / 2;
    int centerY = this.Height / 2;
    int radius = 100;

    int thickness = 25; // 设置线条粗细

    // 绘制风车的支架
    using (Pen pen = new Pen(Color.Blue, thickness))
    {
        g.DrawLine(pen, centerX, centerY, centerX, centerY + radius * 2);
    }           
    // 绘制风车的叶片
    PointF[] points = new PointF[6];
    for (int i = 0; i < 6; i++)
    {
        points[i] = new PointF((float)(centerX + radius * Math.Sin(2 * Math.PI * i / 6)), (float)(centerY - radius * Math.Cos(2 * Math.PI * i / 6)));
    }
    
    // 旋转叶片
    Matrix matrix = new Matrix();
    matrix.RotateAt(angle, new PointF(centerX, centerY));
    g.Transform = matrix;

    // 绘制风车的叶片
    g.FillPolygon(Brushes.Red, points);
    // 绘制中心实心圆
    int circleRadius = 25; // 圆的半径
    Rectangle circleRect = new Rectangle(centerX - circleRadius, centerY - circleRadius, circleRadius * 2, circleRadius * 2);
    g.FillEllipse(Brushes.Blue, circleRect);
}

正反转:

private void button3_Click(object sender, EventArgs e)
{
    isClockwise = !isClockwise;
}

#文章首发挑战赛##头条创作挑战赛##冬日生活打卡季#

Tags:

最近发表
标签列表