c#屏保1(繞著螢幕跑)
阿新 • • 發佈:2019-01-01
運動軌跡
程式碼:四個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; } } } }