使用计时器旋转椭圆并更改其大小

问题描述 投票:1回答:1

我有一个问题要问你。如何使用计时器旋转椭圆并在90度后更改其大小,然后又变回原来的大小?我想用计时器来做。椭圆应在下一个90度后改变。第一个是原始大小,在接下来的90度中他的大小应该更高。当下一个90度旋转时,应该像开始时一样。我有椭圆和计时器的代码,但是我不知道旋转后如何更改大小。计时器代码

private void timer1_Tick(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();

            if (angle == 90)
            {
                t.Stop();
            }
            {
                angle++;
            }
            this.Invalidate();
        }

我的椭圆形

 void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = this.CreateGraphics();

            g.TranslateTransform(127, 127);
            g.RotateTransform(angle);
            g.DrawEllipse(Pens.Red, new Rectangle(0, 0, 60, 40));

        }

和我的按钮动作

 private void button1_Click(object sender, EventArgs e)
        {
            this.Paint += new PaintEventHandler(Form1_Paint);
            angle = 0;
            t.Tick += new EventHandler(timer1_Tick);
            t.Interval = 50;
            t.Start();
        }

有什么想法吗?

Ps。我想要这样的东西https://www.youtube.com/watch?v=7hc8jUhrbRA

c# winforms ellipse
1个回答
0
投票

旋转角度与特定值匹配时改变大小的旋转形状的示例。

使用Matrix.RotateAt方法执行旋转,指定要旋转的形状的中心。设置了一个计时器,以调用用作绘图画布的控件的Invalidate()方法(此处为PictureBox,因为此控件默认情况下启用了双缓冲)。

使用GraphicsPath.AddEllipse方法绘制形状。图形渲染并不比Graphics.DrawEllipse生成的效果好,但是我们最终可以向GraphicsPath对象添加多个形状,并以某种方式使这些形状相互[[interact。

将通过将Matrix对象传递到GraphicsPath.Transform([Matrix])方法而应用转换。

在示例代码中,可以使用TrackBar控件修改旋转速度。控件Value设置

rotationStep字段:在Timer.Tick事件中使用此值执行旋转步骤。

也可以使用第二个TrackBar控件来修改

bounce

角度(形状改变大小的角度),该控件设置rotationCheckPoint字段。当达到此值时,ellipseInflateStep字段(用作大小增加的乘数)将更改符号(根据其先前状态变为正或负)。

[ellipseInflate

字段跟踪当前的大小增加值,然后使用Rectangle.Inflate()方法将其用于使形状膨胀。此方法在两个维度上都会增加矩形的大小,并相应地重置其Location属性。 这是它的工作方式:

Matrix.RotateAt Ellipse shape

该代码示例可以复制此动画中显示的内容。请记住使用已创建的处理程序订阅事件


注意,这是一种简化的方法。您应该使用专门的类来处理所有形状配置值,并在内部提供在将Graphics对象传递给类时在画布上绘制自身的方法。这还允许同时绘制一个形状列表,而不仅仅是一个图形。

using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; public partial class SomeForm : Form { private RectangleF baseShape = RectangleF.Empty; private RectangleF ellipse = RectangleF.Empty; private float rotationAngle = 0.0f; private float rotationStep = 1.0f; private int rotationCheckPoint = 90; private int lastRotationCheck = -1; private float ellipseInflate = 0.0f; private float ellipseInflateStep = .2f; protected override void OnShown(EventArgs e) { base.OnShown(e); SetupShape(); DrawingTimer = new Timer(); DrawingTimer.Interval = 100; DrawingTimer.Tick += TimerTick; } protected void TimerTick(object sender, EventArgs e) { if (rotationAngle >= 360.0f) rotationAngle -= 360.0f; if (lastRotationCheck > (rotationAngle % rotationCheckPoint)) { ellipseInflateStep *= -1; } lastRotationCheck = (int)rotationAngle % rotationCheckPoint; ellipse = baseShape; ellipse.Inflate(ellipseInflate, ellipseInflate); rotationAngle += rotationStep; ellipseInflate += ellipseInflateStep * rotationStep; ellipseInflate = (float)Math.Round(ellipseInflate, 2, MidpointRounding.ToEven); this.Canvas.Invalidate(); } private void Canvas_Paint(object sender, PaintEventArgs e) { e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; using (var pen = new Pen(Color.Red, 4.0f)) using (var mx = new Matrix()) using (var path = new GraphicsPath()) { PointF centerPoint = GetShapeCenterPoint(ellipse); mx.RotateAt(rotationAngle, centerPoint); path.AddEllipse(ellipse); path.Transform(mx); e.Graphics.DrawPath(pen, path); } } private void btnStart_Click(object sender, EventArgs e) => DrawingTimer.Start(); private void btnStop_Click(object sender, EventArgs e) => DrawingTimer.Stop(); private void trkSpeed_ValueChanged(object sender, EventArgs e) => rotationStep = trkSpeed.Value; private void trkBounceAngle_ValueChanged(object sender, EventArgs e) { rotationCheckPoint = trkBounceAngle.Value; lblBounce.Text = trkBounceAngle.Value.ToString(); ResetShape(); } protected override void OnFormClosing(FormClosingEventArgs e) { if (DrawingTimer != null) { DrawingTimer.Stop(); DrawingTimer.Tick -= TimerTick; DrawingTimer.Dispose(); } base.OnFormClosing(e); } private void SetupShape() { Size ellipseSize = new Size(60, 80); PointF location = new PointF((Canvas.Width - ellipseSize.Width) / 2, (Canvas.Height - ellipseSize.Height) / 2); baseShape = new RectangleF(location, ellipseSize); ellipse = baseShape; } private void ResetShape() { DrawingTimer.Enabled = false; rotationAngle = 0.0f; ellipseInflate = 0.0f; ellipseInflateStep = .2f; lastRotationCheck = -1; ellipse = baseShape; DrawingTimer.Enabled = true; } private PointF GetShapeCenterPoint(RectangleF shape) => new PointF(shape.X + shape.Width / 2, shape.Y + shape.Height / 2); }

© www.soinside.com 2019 - 2024. All rights reserved.