C# 屏幕保护程序设计:简单示例代码
下面是一个使用 C# 设计的简单屏幕保护程序的示例代码:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace ScreenSaver
{
public class ScreenSaverForm : Form
{
private Timer timer;
private Random random;
private Point position;
private Brush brush;
public ScreenSaverForm()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.timer = new Timer();
this.random = new Random();
this.position = new Point();
this.brush = new SolidBrush(Color.White);
this.timer.Interval = 1000;
this.timer.Tick += new EventHandler(this.timer_Tick);
this.FormBorderStyle = FormBorderStyle.None;
this.BackColor = Color.Black;
this.TopMost = true;
this.StartPosition = FormStartPosition.Manual;
this.Bounds = Screen.PrimaryScreen.Bounds;
}
private void timer_Tick(object sender, EventArgs e)
{
this.position.X = this.random.Next(this.Bounds.Width);
this.position.Y = this.random.Next(this.Bounds.Height);
this.Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics graphics = e.Graphics;
graphics.FillEllipse(this.brush, this.position.X, this.position.Y, 50, 50);
}
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
// 退出屏幕保护程序
Application.Exit();
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
// 退出屏幕保护程序
Application.Exit();
}
protected override void OnMouseClick(MouseEventArgs e)
{
base.OnMouseClick(e);
// 退出屏幕保护程序
Application.Exit();
}
[STAThread]
public static void Main()
{
Application.Run(new ScreenSaverForm());
}
}
}
这个屏幕保护程序会在屏幕上随机绘制一个白色的圆形。当鼠标移动或点击时,程序会退出。按任意键也可以退出程序。你可以根据需要自定义屏幕保护程序的绘制内容和行为。
原文地址: https://www.cveoy.top/t/topic/fMaZ 著作权归作者所有。请勿转载和采集!