- ·上一篇内容:用C#编写物体碰撞的程序
- ·下一篇内容:使用C#编程实现圆的移动
用C#实现单击按钮时移动物体
本例实现了一个通过单击按钮来移动物体的例子,
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 OnPaint( PaintEventArgs 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());
}
}
本文源自:翔宇亭——IT乐园(http://www.biye5u.com),转载请保留此信息!
微信搜索“优雅的代码”关注本站的公众号,或直接使用微信扫描下面二维码关注本站公众号,以获取最新内容。
个人成长离不开各位的关注,你的关注就是我继续前行的动力。