最近刚学完c#,老师要求我们开始做项目了,经我们小组(3人)讨论,决定做一个和仓库管理差不多的系统,但是问题来,如图:假如点击第二个按钮,它就会往上走,且速度像是被拉上去一般,很是好看,它上去之后,就会有一些新的按钮出来,然后又点击第三个按钮,也是和第二个按钮一样,下面的按钮也是这样,不知道这样的效果是怎么做出来的,想请大家帮忙指点指点,还没去问老师,小弟感激不尽,这是我们第一个项目啊!!!感觉太繁琐了.........最好能有代码外加注释了(我在别的板块也发了这个帖子,可是没啥人回....真的挺急的)c#按钮移动
WinForm还是WebForm啊?
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace TestWin
{
public partial class Form1 : Form
{
private Thread threadForm = null;
public Form1()
{
InitializeComponent();
threadForm = Thread.CurrentThread;
Button tButton = new Button()
{
Text="测试按钮!"
};
SetNewPostion(tButton, new Point(0,0));
tButton.Click+=new EventHandler(tButton_Click);
Controls.Add(tButton);
}
private void SetNewPostion(Control ct, Point positon)
{
ct.Location = positon;
}
private delegate void MoveDelegate(object o);
private bool IsMoving = false;
private IAsyncResult result = null;
protected void tButton_Click(object o, EventArgs e)
{
if (IsMoving)
{
this.EndInvoke(result);
}
else
{
result = this.BeginInvoke(new MoveDelegate(RandomMove), o);
}
}
private const int radomDelay = 1;
private const int moveDelay = 1;
private const int stepCount = 10;
protected void RandomMove(object o)
{
if (!(o is Control)) return;
Control ct = o as Control;
Random rd = new Random();
int i=0;
int y = (rd.Next(0, this.ClientSize.Height - this.Padding.All-ct.Size.Height) - ct.Location.Y) / stepCount;
Thread.CurrentThread.Join(radomDelay);
int x = (rd.Next(0, this.ClientSize.Width-this.Padding.All-ct.Size.Width) - ct.Location.X) / stepCount;
while (i++<stepCount)
{
IsMoving = true;
Point p = new Point(ct.Location.X + x, ct.Location.Y + y);
SetNewPostion(ct, p);
Thread.CurrentThread.Join(moveDelay);
}
IsMoving = false;
}
}
}
//代码应该不难看懂吧,没必要上注释了