当我在面板中单击鼠标时,我的代码会画一个球。现在,我想要的是当我点击面板时,不仅会出现一个球,而且还会以某种速度移动。现在我并不关心球是否越过了面板的边界。我该怎么做?
public partial class Form1 : Form
{
ArrayList dotPts = new ArrayList();
public Form1()
{
InitializeComponent();
}
private void mainPanel_Paint(object sender, PaintEventArgs e)
{
foreach (Point p in dotPts)
{
e.Graphics.FillEllipse(Brushes.Black, p.X, p.Y, 20, 20);
}
}
private void mainPanel_MouseUp(object sender, MouseEventArgs e)
{
Graphics g = Graphics.FromHwnd(this.Handle);
dotPts.Add(new Point(e.X - 10, e.Y - 10));
mainPanel.Invalidate();
}
}
的InitializeComponent():
private void InitializeComponent()
{
this.mainPanel = new System.Windows.Forms.Panel();
this.SuspendLayout();
//
// mainPanel
//
this.mainPanel.BackColor = System.Drawing.SystemColors.GradientInactiveCaption;
this.mainPanel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.mainPanel.Location = new System.Drawing.Point(12, 12);
this.mainPanel.Name = "mainPanel";
this.mainPanel.Size = new System.Drawing.Size(790, 424);
this.mainPanel.TabIndex = 0;
this.mainPanel.Paint += new System.Windows.Forms.PaintEventHandler(this.mainPanel_Paint);
this.mainPanel.MouseUp += new System.Windows.Forms.MouseEventHandler(this.mainPanel_MouseUp);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(821, 447);
this.Controls.Add(this.mainPanel);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
答案 0 :(得分:2)
你需要一个计时器。在Tick事件处理程序中计算对象的新位置并调用面板的Invalidate()方法,以便重新绘制它。如果闪烁太明显,您可以使用PictureBox而不是Panel。
也可以使用ArrayList。这需要成为一个List<Ball>
,其中Ball类存储位置以及速度矢量。加上你将来添加的任何其他属性,如颜色或半径。
答案 1 :(得分:0)
随着时间的推移,让某些东西平稳移动的一般解决方案是制作一个计时器对象,以便通过每个刻度的速度(或其一部分)来改变球的位置:
ball.x += xVelocity;
ball.y += yVelocity;
答案 2 :(得分:0)
我有一个示例(快速和脏)代码我试图给你一个想法..(在LINQPAD中运行)
void Main()
{
Application.Run(new Form1());
}
public class Form1 :Form
{
float time3 =0, time =0,time2=1000000,height=20,width=20;int a = -1;
PointF Location = new PointF(0,0);
PointF Velocity = new PointF(50,50);
DateTime dt;
float x=0;
System.Timers.Timer tmr = new System.Timers.Timer();
System.Timers.Timer gmlp = new System.Timers.Timer();
public Form1()
{
this.Size = new Size(700,700);
Label lb = new Label();
tmr.Interval =20;
tmr.Elapsed += (s,e) => {
//if(Location.X >= 500) Velocity.X *= a;
//if(time3 >= 1000) time=0; else
time3 +=20;// (DateTime.Now.Ticks - dt.Ticks)/10000;
Location.X = Velocity.X * (time3/1000);
Location.Y = Velocity.Y * (time3/1000);
this.Invalidate();
if(time >= time2) {tmr.Stop(); tmr.Enabled = false; gmlp.Stop(); gmlp.Enabled = false;}
};
this.DoubleBuffered =true;
gmlp.Interval = 1000;
gmlp.Elapsed += (s,e) => {
//dt = dt.AddSeconds(1);
lb.Text =
"time: " + time +
"\ntime2: " + time2 +
"\ntime3: " +time3 +
"\nlocx: " +Location.X +
"\ntimespan: " + (DateTime.Now.Ticks - dt.Ticks)/10000 +
"\nx moved: " + (Location.X - x);
};
gmlp.Enabled = true;
gmlp.Start();
tmr.Enabled =true;
tmr.Start();
lb.Location = new Point(20,20);
lb.Size = new Size(80,200);
this.Controls.Add(lb);
dt = DateTime.Now;
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
pe.Graphics.FillEllipse(Brushes.Tomato, new Rectangle((int)Location.X,(int)Location.Y,(int)height,(int)width));
}
}