本文和大家分享的是.NET中窗体的飘动。希望对大家有所帮助吧。
用.NET制作飘动的窗体时,需要使用Timer控件。首先,当窗体加载时设定一个初始位置,然后在窗体中定义两个Timer控件,其中timer1用来控制窗体从左向右飘动,timer2控制窗体从右向左飘动,当timer1启动后,每隔0.01s,都会在触发的事件中给窗体的左上角横坐标加1,这时看到窗体从左向右不断移动,当移动到一定的位置后,timer1停止。timer2启动,每隔0.01s,在触发的事件中给窗体的左上角的横坐标减1,这时看到窗体从右向左不断移动,当移动到一定位置后,timer1启动,timer2停止,如此反覆,窗体就飘动起来了。单击“停止飘动”按钮时,调用Timer对象的Stop方法将两个Timer控件停止,这样既可让窗体停止飘动。制作飘动窗体的主要代码如下:
Private void Form1_Load(object sender, EventArgs e)
{
Point P=new Point(0,240);
This.DesktopLocation=p;
}
Private void timer1_Tick(object sender,EventArgs e)
{
//窗体的左上角横坐标随着timer1不断加1
Point p=new Point(this.DesktipLocation.XML+1,this.DesktopLocation.Y);
This.DesktopLocation=p;
If(p.X==630)
{
Timer1.Enabled=false;
Timer2.Enabled=true;
}
}
Private void timer_Tick(object sender,EventArgs e)
{
//窗体的左上角横坐标随着timer2不断减一
Point P=New Point(this.DesktopLocation.X-1,this.DesktopLocation.Y);
This.DesktopLocation=p;
If(p.X==20)
{
Timer1.Enabled=true;
Timer2.Enabled=false;
}
}
Private void button1_Click(object sender,EventArgs e)
{
Timer1.Stop();
Timer2.Stop();
}