C#屏幕保護程序步驟
Visual C#是微軟公司推出的新一代程序開發(fā)語言,是微軟.Net框架中的一個重要組成部分。屏幕保護程序是以scr為擴展名的標準Windows可執(zhí)行程序。C#屏幕保護程序不僅可以延長顯示器的使用壽命,還可以保護私人信息。本文向大家介紹一個.Net平臺上用C#編寫的一個動態(tài)文本及圖形的C#屏幕保護程序。
具體實現(xiàn)步驟:
1.在Visual Studio.Net下新建一個C#的Windows應用程序工程,不妨命名為screen_saver。
2.現(xiàn)在我們來設計程序的主界面:
先將窗體的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.現(xiàn)在我們開始編寫完整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;
- intwidth1,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調(diào)用后添加任何構(gòu)造函數(shù)代碼
- file://
- }
- protectedoverridevoidDispose(booldisposing)
- {
- if(disposing)
- {
- if(components!=null)
- {
- components.Dispose();
- }
- }
- base.Dispose(disposing);
- }
- #regionWindowsFormDesignergeneratedcode
- ///
- ///設計器支持所需的方法-不要使用代碼編輯器修改
- ///此方法的內(nèi)容。
- 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://窗體啟動調(diào)用事件
- this.Load+=newSystem.EventHandler(this.Form1_Load);
- file://鼠標移動響應事件
- this.MouseMove+=newSystem.Windows.Forms.MouseEventHandler(this.screen_MouseMove);
- this.ResumeLayout(false);
- }
【編輯推薦】