使用C#编程实现圆的移动

时间:2021-05-02

本例实现了编程移动一个圆:

usingSystem;usingSystem.Drawing;usingSystem.Threading;usingSystem.Windows.Forms;publicclassAnimateFace:Form{privateintx=10,y=10;privateintwidth=200,height=200;privateButtonsuspend=newButton();privateButtonresume=newButton();privateButtonabort=newButton();privateThreadt;publicAnimateFace(){BackColor=Color.White;abort.Text="Abort";suspend.Text="Suspend";resume.Text="Resume";Controls.Add(suspend);Controls.Add(resume);Controls.Add(abort);intw=20;suspend.Location=newPoint(w,240);resume.Location=newPoint(w+=10+suspend.Width,240);abort.Location=newPoint(w+=10+resume.Width,240);abort.Click+=newEventHandler(Abort_Click);suspend.Click+=newEventHandler(Suspend_Click);resume.Click+=newEventHandler(Resume_Click);t=newThread(newThreadStart(Run));t.Start();}protectedvoidAbort_Click(objectsender,EventArgse){t.Abort();}protectedvoidSuspend_Click(objectsender,EventArgse){t.Suspend();}protectedvoidResume_Click(objectsender,EventArgse){t.Resume();}protectedoverridevoidOnPaint(PaintEventArgse){Graphicsg=e.Graphics;Pengreen=newPen(Color.Green,3);Brushred=newSolidBrush(Color.Red);g.DrawEllipse(green,x,y,width,height);base.OnPaint(e);}publicvoidRun(){intdx=9,dy=9;while(true){for(inti=0;i<30;i++){x+=dx;y+=dy;width-=dx;height-=dy;Invalidate();Thread.Sleep(30);}dx=-dx;dy=-dy;}}publicstaticvoidMain(){Application.Run(newAnimateFace());}}

本文源自:翔宇亭——IT乐园(http://),转载请保留此信息!

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章