首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

使用 Visual C# 開發動態螢幕保護程式完整教學

     編輯:關於C語言

專案建立與表單設計

在 Visual Studio .NET 中建立 C# Windows 應用程式專案(命名為 screen_saver),設計主表單屬性:

  • Name:screen
  • BackColor:Black
  • Size:800×600
  • FormBorderStyle:None
  • ControlBox/MaximizeBox/MinimizeBox/ShowInTaskbar:全部設為 false

加入三個控制項:

  • Label(Name: Word,Text 空白,可見性 false)
  • PictureBox(Name: picture1,指定圖片,SizeMode: StretchImage)
  • Timer(Name: timerSaver,Enabled: true,Interval: 5ms)

核心程式碼實作

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();
    }
}

部署與測試

  1. 建置專案產生 screen_saver.exe
  2. 重新命名為 screen_saver.scr
  3. 複製至 Windows 系統目錄(通常為 C:\Windows\System32)
  4. 在螢幕保護程式設定中即可選用

關鍵技術點總結

  • 命令列參數處理(/s 執行、/c 設定、/a 密碼)
  • 全螢幕無邊框視窗配合游標隱藏
  • Timer 高頻率觸發實現動畫
  • 滑鼠移動/點擊與鍵盤輸入偵測退出機制
  • 多執行緒安全的座標計算邏輯
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved