用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收集整理,歡迎訪問。