屏保程序
样子如下:(动态的字会随时间改变并移动.中央偏上有时间显示)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace screensaver
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
lblShowtime.Text = DateTime.Now.ToString();//lblShowtime显示现在的时间
//让时间显示在中央偏上
lblShowtime.Top = (this.Height-lblShowtime.Height) / 2-lblShowtime.Height;
lblShowtime.Left = (this.Width - lblShowtime.Width) / 2;
}
int moveX = 10;//作为左右移动量的变量
int moveY = 8;//作为上下移动量的变量
int t= 1;//作为label文本变化的判断
private void timer1_Tick(object sender, EventArgs e)
{
Random rnd = new Random();
this.label1.Left += moveX;//向右移动
this.label1.Top += moveY;//向下移动
if (this.Width < this.label1.Left + this.label1.Width || this.label1.Left < 0)//如果label标签到form窗体左右边界就反向,且t值改变
{
moveX = -moveX;
t = -t;
}
if ( this.Height<this.label1.Top + this.label1.Height || this.label1.Top < 0)//如果label标签到form窗体上下边界就反向,且t值改变
{
moveY = -moveY;
t = -t;
}
//label到边界,就改变文本
if (t == 1)
{ this.label1.Text = "Windows2020"; }
else
{ this.label1.Text = "张悦张悦我爱你"; }
this.label1.ForeColor = Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255));//设置字体颜色随时间随机改变
}
private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
// Application.Exit();//鼠标移动则程序退出
}
private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
Application.Exit();//键盘按下则程序退出
}
private void timerShowtime_Tick(object sender, EventArgs e)
{
lblShowtime.Text = DateTime.Now.ToString();//lblShowtime显示现在的时间,并每秒进行时间的更新(1000ms=1s)
}
}
}
///
//步骤: //
//BackColor背景设为黑色 //
//WindowState窗口状态最大化Maximized /
//FromBorderStyle窗口边框设为无边框None /
//标签设为白色
//标签设timeTicket事件,让它随时间移动 ///
//设置鼠标移动和键盘按下则退出(Exit) ///
//将生成的exe文件复制到C:\Windows\System32里,并重命名为xxx.scr文件,并安装 ///