.scr 檔本質為 .exe,但具獨特行為:全螢幕無邊框、隱藏工作列。本文示範仿 Windows 內建字幕螢幕保護程式的完整開發流程。
this.FormBorderStyle = FormBorderStyle.None; // 無邊框
this.ShowInTaskbar = false; // 不顯示工作列
this.WindowState = FormWindowState.Maximized; // 最大化佔滿螢幕
使用 Timer 驅動 Label (顯示字幕) 移動。每 Tick 遞減 X 座標;移出螢幕左側時重置 X 至右邊,並依當前 Y 位置 (頂/中/底) 循環切換 Y 座標。需先取得 Screen.GetWorkingArea(this) 計算工作區尺寸。
Rectangle ssWorkArea = Screen.GetWorkingArea(this);
lblMarquee.Location = new Point(ssWorkArea.Width - iDistance, lblMarquee.Location.Y);
lblMarquee.Visible = true;
iDistance += speed;
if (lblMarquee.Location.X <= -lblMarquee.Width)
{
iDistance = 0;
if (lblMarquee.Location.Y == 0) lblMarquee.Location = new Point(lblMarquee.Location.X, ssWorkArea.Height / 2);
else if (lblMarquee.Location.Y == ssWorkArea.Height / 2) lblMarquee.Location = new Point(lblMarquee.Location.X, ssWorkArea.Height - lblMarquee.Height);
else lblMarquee.Location = new Point(lblMarquee.Location.X, 0);
}
KeyDown 事件直接結束。ixStart, iyStart;MouseMove 時比較目前座標,有差異即結束。if (ixStart == 0 && iyStart == 0) { ixStart = e.X; iyStart = e.Y; return; }
if (e.X != ixStart || e.Y != iyStart) { Cursor.Show(); timerSaver.Enabled = false; Application.Exit(); }
系統傳遞 /c (設定)、/a (密碼) 參數。本範例無選項,僅彈出訊息並結束。
if (args[0].Substring(0,2).Equals("/c")) { MessageBox.Show("無設定選項"); Application.Exit(); }
else if (args[0] == "/a") { MessageBox.Show("無密碼選項"); Application.Exit(); }
csc /t:winexe screen.cs
產生 Screen.exe 改名 Screen.scr 複製至 System32 目錄即可在螢幕保護程式設定中選用。