运动轨迹
代码:四个timer实现:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _07气泡屏保
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Label lb = new Label();//实例化创建一个新的对象
Timer time = new Timer();//创建一个timer
Timer time1 = new Timer();
Timer time2 = new Timer();
Timer time3 = new Timer();
private void Form1_Load(object sender, EventArgs e)
{
this.FormBorderStyle = FormBorderStyle.None;//去边框
this.WindowState = FormWindowState.Maximized;//窗口最大化
lb.Text = "不许碰";//添加text文本
lb.Font = new Font("宋体",40);//设置文本
lb.AutoSize = true;//设置lable自适应内容大小
this.Controls.Add(lb);//将创建的lable装进this控件集合里面
time.Interval = 10;//设置timer的频率
//+ = tab tab
time.Tick += Time_Tick;
//启动定时器的两种方式
time.Start();
// time.Enabled = true;
time1.Interval = 10;
time1.Tick += Time1_Tick;
time2.Interval = 10;
time2.Tick += Time2_Tick;
time3.Interval = 10;
time3.Tick += Time3_Tick;
}
private void Time3_Tick(object sender, EventArgs e)
{
lb.Top -= 2;
if (lb.Top==0)
{
time3.Stop();
time.Start();
}
}
private void Time2_Tick(object sender, EventArgs e)
{
lb.Left -= 2;
if (lb.Left==0)
{
time2.Stop();
time3.Start();
}
}
private void Time1_Tick(object sender, EventArgs e)
{
lb.Top += 2;
if (lb.Top+lb.Height>=this.Height)
//if(lb.Top+lb.Height>=Screen.PrimaryScreen.Bounds.Height)
{
time1.Stop();
time2.Start();
}
}
private void Time_Tick(object sender, EventArgs e)
{
lb.Left = lb.Left + 2;
//两种判断方法
//if (lb.Left+lb.Width>=this.Width)
if (lb.Left + lb.Width >= Screen.PrimaryScreen.Bounds.Width)
{
time.Stop();
time1.Start();
}
}
}
}
一个timer实现:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _08气泡屏保一个timer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Label lb = new Label();
Timer time = new Timer();
private void Form1_Load(object sender, EventArgs e)
{
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
lb.Text = "不许碰";
lb.Font = new Font("宋体", 40);
lb.AutoSize = true;
this.Controls.Add(lb);
time.Interval = 10;
time.Tick += Time_Tick;
time.Start();
}
int x=5;
int y=0;
private void Time_Tick(object sender, EventArgs e)
{
if (lb.Top<=0)
{
lb.Left += 5;
}
if (lb.Left+lb.Width>=this.Width)
{
lb.Top += 5;
}
if (lb.Top+lb.Height>=this.Height)
{
lb.Left -= 5;
}
if (lb.Left<=0)
{
lb.Top -= 5;
}
}
}
}