我有我的面板中點擊鼠標時將畫一個球的代碼。現在,我想要的是當我點擊面板時,不僅球出現,而且它也以某種速度移動。現在我並不在乎球是否通過了小組的邊界。我該怎麼做呢?如何使用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