在 Visual Studio .NET 中建立 C# Windows 應用程式專案(命名為 screen_saver),設計主表單屬性:
加入三個控制項:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace screen_saver {
public class screen : Form {
private int iSpeed = 2;
private string str = "福建南紡股份公司計算機中心";
private Font TextStringFont = new Font("宋體", 10, FontStyle.Bold);
private Color TextStringColor = Color.Yellow;
private int iDistance, ixStart, iyStart, speed, x1, y1, width1, height1;
private Timer timerSaver;
private PictureBox picture1;
private Label Word;
public screen() {
InitializeComponent();
Word.Font = TextStringFont;
Word.ForeColor = TextStringColor;
Cursor.Hide();
}
// 設計器生成的 InitializeComponent 略...
[STAThread]
static void Main(string[] args) {
if (args.Length == 1) {
if (args[0].Substring(0, 2).Equals("/c")) {
MessageBox.Show("無設定功能", "C# Screen Saver");
Application.Exit();
} else if (args[0] == "/s") {
Application.Run(new screen());
} else if (args[0] == "/a") {
MessageBox.Show("無密碼功能", "C# Screen Saver");
Application.Exit();
} else {
Application.Run(new screen());
}
} else {
Application.Run(new screen());
}
}
private void Form1_Load(object sender, EventArgs e) {
speed = 0;
Rectangle ssWorkArea = Screen.GetWorkingArea(this);
width1 = ssWorkArea.Width;
height1 = ssWorkArea.Height;
}
private void timerSaver_Tick(object sender, EventArgs e) {
Word.Visible = true;
Word.Text = str;
Word.Height = Word.Font.Height;
Word.Width = Word.Text.Length * (int)Word.Font.Size * 2;
PlayScreenSaver();
}
private void PlayScreenSaver() {
// 文字跑馬燈邏輯
Word.Location = new Point(width1 - iDistance, Word.Location.Y);
Word.Visible = true;
iDistance += iSpeed;
if (Word.Location.X <= -Word.Width) {
iDistance = 0;
if (Word.Location.Y == 0)
Word.Location = new Point(Word.Location.X, height1 / 2);
else if (Word.Location.Y == height1 / 2)
Word.Location = new Point(Word.Location.X, height1 - Word.Height);
else
Word.Location = new Point(Word.Location.X, 0);
}
// 圖片移動軌跡計算(複雜幾何路徑)
speed++;
// ... 多段式座標計算邏輯 ...
if (speed == 6 * width1) speed = 0;
picture1.Location = new Point(x1, y1);
}
private void StopScreenSaver() {
Cursor.Show();
timerSaver.Enabled = false;
Application.Exit();
}
// 滑鼠/鍵盤事件:任何輸入即停止螢幕保護
private void screen_MouseMove(object sender, MouseEventArgs e) {
if (ixStart == 0 && iyStart == 0) { ixStart = e.X; iyStart = e.Y; return; }
if (e.X != ixStart || e.Y != iyStart) StopScreenSaver();
}
private void screen_MouseDown(object sender, MouseEventArgs e) => StopScreenSaver();
private void screen_KeyDown(object sender, KeyEventArgs e) => StopScreenSaver();
}
}
screen_saver.exescreen_saver.scrC:\Windows\System32)/s 執行、/c 設定、/a 密碼)