[英]How to draw circle and move in c#
I'm trying to draw one circle and move it in one form in c #. 我试图绘制一个圆圈并在c#中以一种形式移动它。 I use GDI to draw, as follows. 我使用GDI绘制,如下所示。 Suppose that I have 1 class Circle 假设我有1个圆圈
int postitionX, postitionY, radius, angle;
void Draw(Graphics g)
{
g.DrawEllipse(Pen, postitionX, postitionY, radius, radius);
g.FillEllipse(SolidBrush, postitionX, postitionY, radius, radius);
}
and in form main I init circle(postitionX=0, postitionY=10, radius=20, angle=30;) 并且在表格main I init circle(postitionX = 0,postitionY = 10,radius = 20,angle = 30;)
private void form_Paint(object sender, PaintEventArgs e)
{
<caculation postition next>
mycircle.Draw(e.Graphics)
}
But the problem is that function form_Paint run many times and make circle move out display. 但问题是函数form_Paint运行多次并使圆圈移出显示。 Can someone give me no solution? 有人能给我解决方案吗?
Invalidate
will cause a full repaint of your form and will invoke your `form_Paint' event handler. Invalidate
将导致表单的完全重绘,并将调用您的`form_Paint'事件处理程序。 This will cause an endless loop. 这将导致无限循环。 (I see now TaW was just earlier). (我现在看到TaW就在前面)。 If you want to animate a circle on your form, you could use the following approach: 如果要在表单上为圆圈设置动画,可以使用以下方法:
Put a Timer
on your form, set Interval
on 30 and Enabled
to True. 在表单上放置一个Timer
,将Interval
设置为30, Enabled
为True。 Implement the Tick
event: 实现Tick
事件:
private int deltaX = 1;
private int deltaY = 1;
private void timer1_Tick(object sender, EventArgs e)
{
// TO DO your caculation postition, like so:
// be sure window width/height is much larger than 2 * radius:
if ((postitionX - radius) <= 0)
deltaX = 1;
if ((postitionX + radius) >= ClientRectangle.Width)
deltaX = -1;
positionX += deltaX;
if ((postitionY - radius) <= 0)
deltaY = 1;
if ((postitionY + radius) >= ClientRectangle.Heigth)
deltaY = -1;
positionY += deltaY;
// Now you have calculated a 'new animation frame'.
// Now force repaint to draw.
Invalidate(); // This will force a repaint
}
Now update your form_Paint handler: 现在更新你的form_Paint处理程序:
private void form_Paint(object sender, PaintEventArgs e)
{
// caculation postition next HAS TO BE REMOVED FROM HERE
mycircle.Draw(e.Graphics)
// Invalidate(); HAS TO BE REMOVED FROM HERE
}
By playing with the valye for timer1.Interval
in combination with your calculations of next position, you can make the animation slower or faster. 通过使用timer1.Interval的timer1.Interval
结合下一个位置的计算,可以使动画更慢或更快。
So suppose I draw the path of the circle go to 3 points. 所以假设我绘制圆圈的路径转到3点。 if i use timer tick then how. 如果我使用计时器滴答,那么如何。
private void timer1_Tick(object sender, EventArgs e)
{
// Go to point 1
// Go to point 2
// Go to point 3
}
private void form_Paint(object sender, PaintEventArgs e)
{
// caculation postition next HAS TO BE REMOVED FROM HERE
mycircle.Draw(e.Graphics)
// Invalidate(); HAS TO BE REMOVED FROM HERE
}
So the circle will go through point 3 then form_main start redraw.I did not want that. 所以圆圈将经过第3点然后form_main开始重绘。我不想要那样。 So, how can not you 那么,你怎么可能
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.