当前位置:K88软件开发文章中心编程语言.NET.NET01 → 文章内容

用C#实现单击按钮时移动物体

减小字体 增大字体 作者:佚名  来源:翔宇亭IT乐园  发布时间:2018-12-31 11:48:37

:2010-09-10 07:41:00

本例实现了一个通过单击按钮来移动物体的例子,

using System;
using System.Drawing;
using System.Windows.Forms;

public class ButtonToMove : Form {
  private int x = 50, y = 50;
  private Button move = new Button();

  public ButtonToMove() {
    move.Text = "Move";
    move.Location = new Point(5,5);
    Controls.Add(move);
    move.Click += new EventHandler(Move_Click);
  }    
  protected void Move_Click(object sender, EventArgs e) {
    x += 9;
    y += 9;
    Invalidate();
  }
  protected override void OnPaintPaintEventArgs e )   {
    Graphics g = e.Graphics;
    Brush red = new SolidBrush(Color.Red);
    g.FillEllipse(red ,x ,y, 20 ,20);
    base.OnPaint(e);
  }
  public static void Main( ) {
    Application.Run(new ButtonToMove());
  }         
}



用C#实现单击按钮时移动物体