首页 > 开发 > 综合 > 正文

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

2024-07-21 02:20:07
字体:
来源:转载
供稿:网友
visual c#是微软公司推出的新一代程序开发语言,是微软.net框架中的一个重要组成部分。屏幕保护程序是以scr为扩展名的标准windows可执行程序。屏幕保护程序不仅可以延长显示器的使用寿命,还可以保护私人信息。本文向大家介绍一个.net平台上用c#编写的一个动态文本及图形的屏幕保护程序。

  一、具体实现步骤:

  (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(speed>2*height1 && speed<=2*width1)

  {

   x1=system.math.abs(width1-speed);

   y1=system.math.abs(height1-(speed-speed/height1*height1));

  }

  else if(speed>2*width1 &&speed<=3*height1)

  {

   x1=system.math.abs(width1-(speed-speed/width1*width1));

   y1=system.math.abs(height1-(speed-speed/height1*height1));

  }

  else if(speed>3*height1 && speed<4*height1)

  {

   x1=system.math.abs(width1-(speed-speed/width1*width1));

   y1=system.math.abs(speed-speed/height1*height1);

  }

  else if(speed>=4*height1 && speed<5*height1)

  {

   x1=system.math.abs(speed-speed/width1*width1);

   y1=system.math.abs(height1-(speed-speed/height1*height1));

  }

  else if(speed>=5*height1 && speed<4*width1)

  {

   x1=system.math.abs(speed-speed/width1*width1);

   y1=system.math.abs(speed-speed/height1*height1);

  }

  else if(speed>=4*width1 && speed<6*height1)

  {

   x1=system.math.abs(width1-(speed-speed/width1*width1));

   y1=system.math.abs(speed-speed/height1*height1);

  }

  else if(speed>=6*height1 && speed<5*width1)

  {

   x1=system.math.abs(width1-(speed-speed/width1*width1));

   y1=system.math.abs(height1-(speed-speed/height1*height1));

  }

  else if(speed>=5*width1 && speed<7*height1)

  {

   x1=system.math.abs(speed-speed/width1*width1);

   y1=system.math.abs(height1-(speed-speed/height1*height1));

  }

  else if(speed>=7*height1 && speed<6*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系统目录中,这样就可以运行该屏幕保护程序。
  • 本文来源于网页设计爱好者web开发社区http://www.html.org.cn收集整理,欢迎访问。
  • 发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表