System.Windows.FormsInterval) 觸發 Tick 事件Enabled、Interval、Tick 事件Form_Load 設定 DesktopLocation 為螢幕座標 (0, 240)
private void Form1_Load(object sender, EventArgs e)
{
Point p = new Point(0, 240); // 螢幕左側、垂直置中約略位置
this.DesktopLocation = p;
}
DesktopLocation 為視窗左上角在螢幕座標系的位置。
// Interval = 10 (0.01 秒)
private void Timer1_Tick(object sender, EventArgs e)
{
Point p = new Point(this.DesktopLocation.X + 1, this.DesktopLocation.Y);
this.DesktopLocation = p;
if (p.X == 550) // 到達右側邊界
{
timer1.Enabled = false;
timer2.Enabled = true;
}
}
private void Timer2_Tick(object sender, EventArgs e)
{
Point p = new Point(this.DesktopLocation.X - 1, this.DesktopLocation.Y);
this.DesktopLocation = p;
if (p.X == -150) // 到達左側邊界 (超出螢幕左側形成視覺循環)
{
timer1.Enabled = true;
timer2.Enabled = false;
}
}
namespace FloatingForm
{
public class Form1 : Form
{
private Timer timer1, timer2;
private Label label1;
private Button button1;
private Container components;
// InitializeComponent: 建構所有控制項、設定 Timer、事件綁定
// Form1_Load: 設定初始位置
// Timer1_Tick: 向右移動 + 邊界切換
// Timer2_Tick: 向左移動 + 邊界切換
// Button1_Click: timer1.Stop(); timer2.Stop();
}
}
下一篇將提供完整可編譯執行的 Form1.cs 原始碼、執行效果說明,以及擴充方向建議 (垂直飄動、隨機軌跡、無邊框圖片模式等)。