用Visual C#编写屏幕保护程序

2016-02-19 16:56 0 1 收藏

图老师小编精心整理的用Visual C#编写屏幕保护程序希望大家喜欢,觉得好的亲们记得收藏起来哦!您的支持就是小编更新的动力~

【 tulaoshi.com - 编程语言 】

  Visual C#是微软公司推出的新一代程序开发语言,是微软.Net框架中的一个重要组成部分。屏幕保护程序是以scr为扩展名的标准Windows可执行程序。屏幕保护程序不仅可以延长显示器的使用寿命,还可以保护私人信息。本文向大家介绍一个.Net平台上用C#编写的一个动态文本及图形的屏幕保护程序。

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  一、具体实现步骤:

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  (1)在Visual Studio.Net下新建一个C#的Windows应用程序工程,不妨命名为screen_saver。

  (2)现在我们来设计程序的主界面:

  先将窗体的Name属性设置为screen、Text属性设置为空,BackColor属性设置为Black、Size属性设置为(800, 600)、 ControlBox、MaximizeBox、MinimizeBox、ShowInTaskbar属性设置均为false、FormBorderStyle属性设置为None。再往窗体上添加Label控件、PictureBox控件、Timer控件各一个。将Label控件的Name设置为word、Text属性设置为空;将PictureBox控件的Name设置为picture1、Image设置为一个预知图片;将Timer控件的Name设置为timerSaver、Enabled 属性设为true、Interval属性设为5。

  (3)现在我们开始编写完整程序代码部分:

  

//导入使用到的名称空间using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;file://   namespace screen_saver   {////// Form1 的摘要说明。///public class screen : System.Windows.Forms.Form{ file://加入私有成员变量 private System.ComponentModel.IContainer components; private int iSpeed = 2; private string str="福建南纺股份公司计算机中心"; file://定义文本字体及大小 private System.Drawing.Font TextStringFont = new System.Drawing.Font ("宋体, 10,System.Drawing.FontStyle.Bold); private Color TextStringcolor =System.Drawing.Color.Yellow; file://文本字体颜色 private int iDistance; private int ixStart= 0; private int iyStart= 0; private int speed; private int x1,y1; int width1,height1; private System.Windows.Forms.Timer timerSaver;  file://计时器控件 private System.Windows.Forms.PictureBox picture1; file://图形控件 private System.Windows.Forms.Label word; file://文本显示控件////// 必需的设计器变量。/// public screen() { file:// // Windows 窗体设计器支持所必需的 file://  InitializeComponent();  word.Font=TextStringFont;  word.ForeColor=TextStringcolor;  System.Windows.Forms.Cursor.Hide(); file://隐藏光标  file://  // TODO: 在 InitializeComponent 调用后添加任何构造函数代码  file:// }  /// /// 清理所有正在使用的资源。 /// protected override void Dispose( bool disposing ) {  if( disposing )  {   if (components != null)   {    components.Dispose();   }  }  base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// private void InitializeComponent() file://初始化程序中使用到的组件 {  this.components = new System.ComponentModel.Container();  System.Resources.ResourceManager resources = new   system.Resources.ResourceManger(typeof(screen));        this.word = new System.Windows.Forms.Label();  this.timerSaver = new System.Windows.Forms.Timer(this.components);  this.picture1 = new System.Windows.Forms.PictureBox();  this.SuspendLayout();  //  // 设置文本显示控件(word)属性  this.word.ForeColor = System.Drawing.Color.Yellow;  this.word.Location = new System.Drawing.Point(624, 8);  this.word.Name = "word";  this.word.Size = new System.Drawing.Size(168, 16);  this.word.TabIndex = 0;  this.word.Visible = false;  //  // 设置计时器控件(timerSaver)属性  this.timerSaver.Enabled = true;  this.timerSaver.Interval = 5;  this.timerSaver.Tick += new System.EventHandler(this.timerSaver_Tick);  //  // 设置图片控件(picture1)属性  this.picture1.Image = ((System.Drawing.Bitmap)(resources.GetObject("picture1.Image")));  this.picture1.Location = new System.Drawing.Point(800, 600);  this.picture1.Name = "picture1";  this.picture1.Size = new System.Drawing.Size(304, 224);  this.picture1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;  this.picture1.TabIndex = 1;  this.picture1.TabStop = false;  //  // 设置窗体(screen)属性  this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);  this.BackColor = System.Drawing.Color.Black;  this.ClientSize = new System.Drawing.Size(800, 600);  this.ControlBox = false;  this.Controls.AddRange(new System.Windows.Forms.Control[] {this.picture1,this.word});  this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;  this.KeyPreview = true;  this.MaximizeBox = false;  this.MinimizeBox = false;  this.Name = "screen";  this.ShowInTaskbar = false;  this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;  this.WindowState = System.Windows.Forms.FormWindowState.Maximized;  file://键盘按下响应事件  this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.screen_KeyDown);            file://鼠标按下响应事件   this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.screen_MouseDown);  file://窗体启动调用事件  this.Load += new System.EventHandler(this.Form1_Load);            file://鼠标移动响应事件  this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.screen_MouseMove);  this.ResumeLayout(false); } #endregion /// /// 应用程序的主入口点。 /// [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()); } private void Form1_Load(object sender, System.EventArgs e) {  speed=0;  System.Drawing.Rectangle ssWorkArea=System.Windows.Forms.Screen.GetWorkingArea(this);  file://屏幕显示区域  width1=ssWorkArea.Width; file://屏幕宽度  height1=ssWorkArea.Height; file://屏幕高度 } private void timerSaver_Tick(object sender, System.EventArgs e) file://计时器响应事件 {  word.Visible=true;  word.Text=str;  word.Height=word.Font.Height; file://设置文本的高度  word.Width=word.Text.Length*(int)word.Font.Size*2; file://设置文本的宽度  PlayScreenSaver(); } private void PlayScreenSaver() file://自定义函数 {  file://下面设置文本显示框的位置坐标  word.Location =new System.Drawing.Point(width1-iDistance,word.Location.Y);  word.Visible=true; file://设置为可见  iDistance+=iSpeed;  if(word.Location.X=-(word.Width))  {   iDistance=0;   if(word.Location.Y==0)    word.Location=new System.Drawing.Point(word.Location.X,height1/2);   else if(word.Location.Y==height1/2)    word.Location=new System.Drawing.Point(word.Location.X,height1-word.Height);   else    word.Location=new System.Drawing.Point(word.Location.X,0);  }  file://下面是计算图片框移动坐标  speed++;  if(speed=2*height1)  {   x1=System.Math.Abs(width1-speed);   y1=System.Math.Abs(height1-speed);  }  else if(speed2*height1 && speed=2*width1)  {   x1=System.Math.Abs(width1-speed);   y1=System.Math.Abs(height1-(speed-speed/height1*height1));  }  else if(speed2*width1 &&speed=3*height1)  {   x1=System.Math.Abs(width1-(speed-speed/width1*width1));   y1=System.Math.Abs(height1-(speed-speed/height1*height1));  }  else if(speed3*height1 && speed4*height1)  {   x1=System.Math.Abs(width1-(speed-speed/width1*width1));   y1=System.Math.Abs(speed-speed/height1*height1);  }  else if(speed=4*height1 && speed5*height1)  {   x1=System.Math.Abs(speed-speed/width1*width1);   y1=System.Math.Abs(height1-(speed-speed/height1*height1));  }  else if(speed=5*height1 && speed4*width1)  {   x1=System.Math.Abs(speed-speed/width1*width1);   y1=System.Math.Abs(speed-speed/height1*height1);  }  else if(speed=4*width1 && speed6*height1)  {   x1=System.Math.Abs(width1-(speed-speed/width1*width1));   y1=System.Math.Abs(speed-speed/height1*height1);  }  else if(speed=6*height1 && speed5*width1)  {   x1=System.Math.Abs(width1-(speed-speed/width1*width1));   y1=System.Math.Abs(height1-(speed-speed/height1*height1));  }  else if(speed=5*width1 && speed7*height1)  {   x1=System.Math.Abs(speed-speed/width1*width1);   y1=System.Math.Abs(height1-(speed-speed/height1*height1));  }  else if(speed=7*height1 && speed6*width1)  {   x1=System.Math.Abs(speed-speed/width1*width1);   y1=System.Math.Abs(speed-speed/height1*height1);  }  if(speed==6*width1)  speed=0;  picture1.Location=new System.Drawing.Point(x1,y1); } private void StopScreenSaver() file://停止屏幕保护程序运行 {  System.Windows.Forms.Cursor.Show();  timerSaver.Enabled=false;  Application.Exit(); } private void screen_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)  file://鼠标移动事件 {  if(ixStart==0 && iyStart==0)  {   ixStart=e.X;   iyStart=e.Y;   return;  }  else if(e.X!=ixStart||e.Y!=iyStart)  StopScreenSaver(); } private void screen_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) file://鼠标按下事件 {  StopScreenSaver(); file://停止运行屏幕保护程序 } private void screen_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)  file://键盘按下事件 {  StopScreenSaver(); file://停止运行屏幕保护程序 }}}

  最后运行该程序,把screen_saver.exe改为screen_saver.scr,拷入Windows系统目录中,这样就可以运行该屏幕保护程序。

来源:https://www.tulaoshi.com/n/20160219/1613598.html

延伸阅读
C#中程序结构的关键概念为程序、命名空间、类型、成员和程序集。C#程序包括一个或多个源文件。程序中声明类型,类型包含成员并能够被组织到命名空间中。类和接口是类型的例子。字段、方法、属性和事件则是成员的例子。当C#程序被编译时,它们被物理地打包到程序集中。程序集的文件扩展名一般为.exe或者.dll,这取决于它们是实现为应用程序...
在下面的练习中,将创建一个应用程序,它包含的一个方法能够计算一名顾问的收费金额假定该顾问每天收取固定的费用,将根据工作了多少天来收费。首先要开发应用程序的逻辑,然后利用生成方法存根向导来写出这个逻辑使用的方法。接着,我们将在一个控制台应用程序中运行方法,以获得对该程序的最终印象。最后,我们将使用Visual Studio 2005...
本文通过运用C#来实现一个基于POP3协议的邮件接收程序来向大家展示C#网络编程的功能强大,同时也向大家介绍一下基于POP3协议的电子邮件接收原理。 首先我向大家介绍邮件接收的基本原理: 一开始便是客户端与服务器的连接。不过,在客户端连接到服务器之前,注意把端口设为POP3协议默认的110号。 客户端连接服务器成功后,服...
网络代理程序的种类非常多,根据代理服务程序代理的协议不同,分成HTTP代理服务程序、FTP代理服务程序等,运行代理服务程序的服务器也就称为HTTP代理服务器和FTP代理服务器。在本节中介绍的Web代理服务程序代理的就是HTTP协议。 一.网络代理的类型及实现原理: 网络代理服务根据工作层次,一般可分为应用层代理、传输层代理和SO...
获取文件的版本信息: FileVersionInfo myFileVersionInfo1 = FileVersionInfo.GetVersionInfo("D:\\TEST.DLL"); textBox1.Text="版本号: " + myFileVersionInfo1.FileVersion; 更改文件属性,删除只读文件: 下例欲将E:\test.txt文件拷贝至D:\tmp\test.txt,但D:\tmp\test.txt已经存在。 //File.Copy(sourceFile,destinationFile,tr...

经验教程

431

收藏

87
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部