我有我的面板中点击鼠标时将画一个球的代码。现在,我想要的是当我点击面板时,不仅球出现,而且它也以某种速度移动。现在我并不在乎球是否通过了小组的边界。我该怎么做呢?如何使用WinForms绘制在面板中移动的球?
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);
}
以什么速度移动? – 2011-02-07 16:09:17
我想你的意思是恒定的速度?您可以使用“定时器”来“动画”球的运动。您还应该指定球应该如何移动(从左到右,随机,反弹,...)。 – 2011-02-07 16:11:12