• 网络学院
  • IT资讯
  • 操作系统
  • 网络技术
  • 软件应用
  • 办公软件
  • 编程技术
  • 网站架设
  • 数据库类
  • 平面设计
  • 多媒体类
  • 游戏资讯
  • 教学论文
  • 认证考试
用Visual
  站点:
  • 首 页
  • 最新软件
  • 文章教程
  • 国内软件
  • 国外软件
  • 绿色软件
  • 源码下载
  • 字体下载
用Visual C#编写屏幕保护程序
软件发布 用Visual C#编写屏幕保护程序
网络软件 系统工具 应用软件 联络聊天 图形图像 多媒体类 行业软件 游戏娱乐 编程开发 安全相关 教育教学 数码软件 绿软下载
热门软件: QQ 瑞星 pplive e话通 木马克星 千千静听 office2000 五笔字根 Photoshop 视频分割
返回文章教程首页 >> 文章首页 >> 编程技术 >> C#教程 >> 用Visual C#编写屏幕保护程序

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

添加时间: 2007-7-12 0:04:30  作者: C#教程  阅读次数:24   来源: http://www.d9soft.com

        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!=ixStarte.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系统目录中,这样就可以运行该屏幕保护程序。

 

上下文章:

 

上一篇文章: 使用C#进行Word 2002和Excel 2002编程 下一篇文章: C#中调用Windows API的要点

相关文章:

  • AV杀手变种强行关闭杀度软件与下载恶意程序
  • 保护数据库安全关键在于加密
  • Oracle数据库性能保护(下)
  • 编写一个接受变量的存储过程
  • Oracle外部程序的触发小结

相关软件:

  • 雨过天晴电脑保护系统 V1.0.060612
  • Visual Studio 2005 Express Editions (Visual Basic)
  • Visual Graph专业图形引擎 V4.6
  • 屏幕画笔 2.68
  • Visual Basic V5.0 Runtime Files
  • 全国计算机等级考试模拟软件(2006年全年使用)二级Visual Basic V9.0

 

快速导航

  • 网络学院
  • 精品汇聚
  • 字体下载
  • 教程下载
  • ASP源码
  • PHP源码
  • Net源码
  • JSP 源码

编程技术分类导航

  • ASP & ASP.NET教程
  • PHP教程
  • JSP教程
  • C/C++教程
  • VB & VB.NET教程
  • VC教程
  • Delphi教程
  • BCB教程
  • VFP教程
  • PB教程
  • JAVA教程
  • XML教程
  • C#教程
  • CGI教程

本类经典文章推荐

  • ADO.NET的开发场景及传统ADO的处理
  • 利用Visual C#实现Window管道技术
  • C#取得汉字的拼音的首字母
  • 使用C#编写DES加密程序的framework
  • Visual C#编写3D游戏框架示例
  • 用C#和本地Windows API操纵系统菜...
  • 在C#程序设计中使用Win32类库
  • Visual C#中调用Windows服务初探
  • 如何在C#的WinForm中制作饼状图和...
  • C#中实现DataGrid双向排序

C#教程阅读排行

  • 如何在C#的WinForm中制作饼状图和...
  • 浅析C#中图形编程
  • 用C#和本地Windows API操纵系统菜...
  • 彻底剖析C# 2.0泛型类的创建和使用
  • 使用C#编写DES加密程序的framework
  • Visual C#编写3D游戏框架示例
  • Visual C#中调用Windows服务初探
  • C#如何在Form中嵌入并且操作Excel...
  • Visual C# 2005实现控件中捕获按键
  • C#中利用mediaplayer打造mp3播放器

编程技术阅读总排行

  • VB入门教程之一
  • Java连接数据库实例
  • 第二章 PowerBuilder 入门之创建新...
  • VC++之List Box/Check List Box控...
  • 第一章 什么是PowerBuilder
  • VC++ List Ctrl控件
  • VC++ Combo Box/Combo Box Ex控件
  • 学C++不得不看的一篇文章
  • VB入门教程之二
  • VC++之Button控件

广告位置

字母检索 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 回到顶部

关于我们 | 版权声明 | 免责条款 | 广告联系 | 软件发布 | 下载帮助 | 下载排行 | 网站地图 | 特别鸣谢 | 友情连接

copyright; 2005-2008 D9soft.com 第九软件网 版权所有