温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C#中怎么实现一个屏幕保护程序

发布时间:2021-07-07 16:26:59 来源:亿速云 阅读:354 作者:Leah 栏目:编程语言

C#中怎么实现一个屏幕保护程序,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

C#屏幕保护程序不仅可以延长显示器的使用寿命,还可以保护私人信息。本文向大家介绍一个.Net平台上用C#编写的一个动态文本及图形的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.现在我们开始编写完整C#屏幕保护程序代码部分:

usingSystem;  usingSystem.Drawing;  usingSystem.Collections;  usingSystem.ComponentModel;  usingSystem.Windows.Forms;  usingSystem.Data;  file://   namespacescreen_saver  {  ///  ///Form1的摘要说明。  ///   publicclassscreen:System.Windows.Forms.Form  {  file://加入私有成员变量   privateSystem.ComponentModel.IContainercomponents;  privateintiSpeed=2;  privatestringstr="福建南纺股份公司计算机中心";  file://定义文本字体及大小   privateSystem.Drawing.FontTextStringFont=newSystem.Drawing.Font("宋体”,10,System.Drawing.FontStyle.Bold);   privateColorTextStringcolor=System.Drawing.Color.Yellow;file://文本字体颜色   privateintiDistance;  privateintixStart=0;  privateintiyStart=0;  privateintspeed;  privateintx1,y1;  intwidth2,height1;  privateSystem.Windows.Forms.TimertimerSaver;file://计时器控件   privateSystem.Windows.Forms.PictureBoxpicture1;file://图形控件   privateSystem.Windows.Forms.Labelword;file://文本显示控件   ///  ///必需的设计器变量。  ///   publicscreen()  {  file://  //Windows窗体设计器支持所必需的   file://   InitializeComponent();  word.Font=TextStringFont;  word.ForeColor=TextStringcolor;  System.Windows.Forms.Cursor.Hide();file://隐藏光标   file://  //TODO:在InitializeComponent调用后添加任何构造函数代码   file://   }   protectedoverridevoidDispose(booldisposing)  {  if(disposing)  {  if(components!=null)  {  components.Dispose();  }  }  base.Dispose(disposing);  }  #regionWindowsFormDesignergeneratedcode  ///  ///设计器支持所需的方法-不要使用代码编辑器修改  ///此方法的内容。  privatevoidInitializeComponent()file://初始化程序中使用到的组件   {  this.components=newSystem.ComponentModel.Container();  System.Resources.ResourceManagerresources=newsystem.Resources.ResourceManger(typeof(screen));  this.word=newSystem.Windows.Forms.Label();  this.timerSaver=newSystem.Windows.Forms.Timer(this.components);  this.picture1=newSystem.Windows.Forms.PictureBox();  this.SuspendLayout();  //  //设置文本显示控件(word)属性   this.word.ForeColor=System.Drawing.Color.Yellow;  this.word.Location=newSystem.Drawing.Point(624,8);  this.word.Name="word";  this.word.Size=newSystem.Drawing.Size(168,16);  this.word.TabIndex=0;  this.word.Visible=false;  //  //设置计时器控件(timerSaver)属性   this.timerSaver.Enabled=true;  this.timerSaver.Interval=5;  this.timerSaver.Tick+=newSystem.EventHandler(this.timerSaver_Tick);  //  //设置图片控件(picture1)属性   this.picture1.Image=((System.Drawing.Bitmap)(resources.GetObject("picture1.Image")));  this.picture1.Location=newSystem.Drawing.Point(800,600);  this.picture1.Name="picture1";  this.picture1.Size=newSystem.Drawing.Size(304,224);  this.picture1.SizeMode=System.Windows.Forms.PictureBoxSizeMode.StretchImage;  this.picture1.TabIndex=1;  this.picture1.TabStop=false;  //  //设置窗体(screen)属性   this.AutoScaleBaseSize=newSystem.Drawing.Size(6,14);  this.BackColor=System.Drawing.Color.Black;  this.ClientSize=newSystem.Drawing.Size(800,600);  this.ControlBox=false;  this.Controls.AddRange(newSystem.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+=newSystem.Windows.Forms.KeyEventHandler(this.screen_KeyDown);  file://鼠标按下响应事件   this.MouseDown+=newSystem.Windows.Forms.MouseEventHandler(this.screen_MouseDown);  file://窗体启动调用事件   this.Load+=newSystem.EventHandler(this.Form1_Load);  file://鼠标移动响应事件   this.MouseMove+=newSystem.Windows.Forms.MouseEventHandler(this.screen_MouseMove);  this.ResumeLayout(false);  }

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI