窗体飘动的问题(C#)
用Timer控件,合法死循环,参上!两个Timer.
//首先定位窗体
//Form1_Lodo事件:
Point p=new Point(0,100);
this.DesktopLocation=p;
//设置timer1,timer2为10毫秒变一次
//timer1_Tick事件:
Point p=new Point(this.DesktopLocation.x+1,this.DesktopLocation.y);
this.DesktopLocation=p;
if(p.x==550) //如果达成条件就停止timer1执行timer2
{
timer1.Enabled=false;
timer2.Enabled=true;
}
//timer2_Tick事件:
Point p=new Point(this.DesktopLocation.x-1,this.DesktopLocation.y);
this.DesktopLocation=p;
if(p.x==-150) //如果达成条件就停止timer2执行timer1
{
timer1.Enabled=true;
timer2.Enabled=false;
}
//点击按纽停止飘动
//button1_Click:
tihs.timer1.Stop();
this.timer2.Stop();
完了,本以为可以看到飘动的窗体,可看见的是窗体在那里一动不动......:L 是这样的
private System.Windows.Forms.Timer timer1;
private System.Drawing.Point P2 = new System.Drawing.Point(0, 0);
private void Form1_Load(object sender, EventArgs e)//LOAD事件,设置初始位置
{
Point P = new Point();
P.X = 0;
P.Y = 0;
this.DesktopLocation = P;
}
private void timer1_Tick(object sender, EventArgs e)//X坐标和Y坐标+1
{
P2.X += 1;
P2.Y += 1;
this.Location = new Point(P2.X,P2.Y); //因为坐标的位置不是变量,所以要NEW一个出来
}
看明白了吗?~~~其他效果自己想想就可以弄出来了 Point P = new Point();
P.X = 0;
P.Y = 0;
this.DesktopLocation = P;
一出来在左上角
Point p=new Point(0,100);
this.DesktopLocation=p;
一出来在左上角下来一点点
一样的吧 原帖由 zkkpkk 于 2006-6-11 19:13 发表
Point P = new Point();
P.X = 0;
P.Y = 0;
this.DesktopLocation = P;
一出来在左上角
Point p=new Point(0,100);
this.DesktopLocation=p;
一出来在左上角下来一点 ...
一样的~~~这只是定义它的初始坐标而已!
页:
[1]