代碼:
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 四個timer隨便轉 //名稱
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) //當form1加載時
{
this.FormBorderStyle = FormBorderStyle.None; //設置其窗體邊框爲空
this.Width = 300; //窗體寬度爲300
this.Height = 300; //窗體高度爲300
this.Location = new Point(0, 0); //給窗體左上角定位
this.Opacity = (0.8); //設置窗體的不透明度爲0.8
timer1.Start(); //啓動timer1
}
private void timer1_Tick(object sender, EventArgs e)
{
this.Left += 3; //當timer1啓動時窗體left每一個時間段增加3
this.Top += 3; //當timer1啓動時窗體top每一個時間段增加3
if (this.Top+this.Height>=Screen.PrimaryScreen.WorkingArea.Height)
//如果此時窗體移動的位置爲其top加上其height大於等於工作區的height時則執行
{
timer1.Stop(); //停止timer1
timer2.Start();//當timer1停止時則需要啓動timer2來控其移動位置
}
if (this.Left+this.Width>=Screen.PrimaryScreen.WorkingArea.Width)
//如果此時窗體的位置爲其left加上其width大於等於顯示區的寬度則執行
{
timer1.Stop(); //這裏爲什麼停止timer1是因爲下面有啓動timer1
//此時位置條件符合if判斷
//所以timer1停止 暫且先不管 依照預測位移順序按部就班
timer3.Start(); //此時則又需要重新啓動timer3 來控制其位移 因爲timer3的位移
//控制公式 正好符合其位移到指定的位置公式
}
}
private void timer2_Tick(object sender, EventArgs e) //timer2啓動時
{
this.Top -= 3; //其top每一個時間段減少3的距離
this.Left += 3; //其left每一個時間段增加3的距離
if (this.Top <= 0) //如果這時窗體的位置爲其top小於等於零時則執行
{
timer2.Stop(); //停止timer2 因爲要給他新的位移方式和方向啊
timer1.Start(); //而此時timer1就符合其位移的方式和方向 所以咱啓動timer1
//至於什麼時候停止timer1 則需要在timer1中增加新的判斷條件
}
}
private void timer3_Tick(object sender, EventArgs e) //當timer3啓動時
{
this.Top += 3; //其top每隔間隔時間增加3的距離
this.Left -= 3; //其left每隔間隔時間減少3的距離你
if (this.Top+this.Height>=Screen.PrimaryScreen.WorkingArea.Height )
{ //按照timer3的位移方式 當窗體的移動位置符合此條件時則執行
timer3.Stop(); //停止timer3 讓其尋找新的移動方式重新位移
timer4.Start();//此時啓動timer4來進行位移 啓動後則需要停止那就去找停止條件
}
if (this.Left<=0) //此時的情況與上面重新啓動timer1一樣
{ //當timer3 啓動後 其位移位置符合left小於等於零則執行
timer3.Stop(); //(先不管按照預想位移去執行) 此時則停止timer3 尋找新的位移
timer1.Start(); //那 此時timer1又符合條件了 所以啓動timer1 實現循環
}
}
private void timer4_Tick(object sender, EventArgs e)
{ //當timer4啓動時
this.Top -= 3; //其top每隔間隔時間減少3
this.Left -= 3; //其left每隔間隔時間減少3
if (this.Top <=0)
{ //當位移的位置其top小於等於零 則執行
timer4.Stop(); //此時停止timer4 來尋找新的位移方式
timer3.Start(); //而此時timer3就符合新的位移方式 則啓動timer3
//要想停止timer3 則需要在timer3中添加新的判斷條件
}
}
}
}
入坑不久 歡迎諸位指正 !
可能有的順序表達的有點混亂 其實就是尋找符合下一步的移動公式然後啓動其timer 然後在一定的位置停止
然後尋找新的移動方式 進行位移 至於重複啓動的timer 就是因爲他符合新的移動條件~~~
至於怎麼變爲圓形 另一篇有講 這個地方有待加強!