用户界面-C#GDI旋转投影
本文关键字:投影 旋转 -C#GDI 用户界面 | 更新日期: 2023-09-27 18:00:00
我有这个代码
Graphics g;
g.FillRectangle(new SolidBrush(Color.Red), _Location.X - 2, _Location.Y - 2, 10, 10);
并且矩形是以一定角度向某个方向拍摄的,我如何让矩形在移动或旋转的同时旋转。
这应该会旋转一个在屏幕上移动的矩形。
private int _angle = 0;
private Point _location = new Point(0, 0);
private void _timer_Tick(object sender, System.EventArgs e)
{
// nothing interesting here, moving the top left co-ordinate of the
// rectangle at constant rate
_location = new System.Drawing.Point(_location.X + 2, _location.Y + 2);
_angle += 5; // our current rotation angle
this.Invalidate();
}
void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
// rebase the co-ordinate system so our current x,y is 0, 0 and that is
// the center of the rotation
g.TranslateTransform(_location.X, _location.Y, MatrixOrder.Append);
g.RotateTransform(_angle); // do the rotation
// make sure the centre of the rectangle is the centre of rotation (0, 0)
g.FillRectangle(new SolidBrush(Color.Red), -5, -5, 10, 10);
}
我在看到@steve16351的响应之前就已经弄清楚了,但他的代码仍然很有用。我所做的是从使用PointF切换到矩形,因为矩形有一个保持左上角坐标值的属性,所以我可以在游戏循环中以稳定的速率递增/递减,使其旋转。