C#屏幕保護(hù)程序步驟
Visual C#是微軟公司推出的新一代程序開發(fā)語(yǔ)言,是微軟.Net框架中的一個(gè)重要組成部分。屏幕保護(hù)程序是以scr為擴(kuò)展名的標(biāo)準(zhǔn)Windows可執(zhí)行程序。C#屏幕保護(hù)程序不僅可以延長(zhǎng)顯示器的使用壽命,還可以保護(hù)私人信息。本文向大家介紹一個(gè).Net平臺(tái)上用C#編寫的一個(gè)動(dòng)態(tài)文本及圖形的C#屏幕保護(hù)程序。
具體實(shí)現(xiàn)步驟:
1.在Visual Studio.Net下新建一個(gè)C#的Windows應(yīng)用程序工程,不妨命名為screen_saver。
2.現(xiàn)在我們來設(shè)計(jì)程序的主界面:
先將窗體的Name屬性設(shè)置為screen、Text屬性設(shè)置為空,BackColor屬性設(shè)置為Black、Size屬性設(shè)置為(800, 600)、 ControlBox、MaximizeBox、MinimizeBox、ShowInTaskbar屬性設(shè)置均為false、 FormBorderStyle屬性設(shè)置為None。再往窗體上添加Label控件、PictureBox控件、Timer控件各一個(gè)。將Label控件的Name設(shè)置為word、Text屬性設(shè)置為空;將PictureBox控件的Name設(shè)置為picture1、Image設(shè)置為一個(gè)預(yù)知圖片;將 Timer控件的Name設(shè)置為timerSaver、Enabled 屬性設(shè)為true、Interval屬性設(shè)為5。
3.現(xiàn)在我們開始編寫完整C#屏幕保護(hù)程序代碼部分:
- 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="福建南紡股份公司計(jì)算機(jī)中心";
- 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://計(jì)時(shí)器控件
- privateSystem.Windows.Forms.PictureBoxpicture1;file://圖形控件
- privateSystem.Windows.Forms.Labelword;file://文本顯示控件
- ///
- ///必需的設(shè)計(jì)器變量。
- ///
- publicscreen()
- {
- file://
- //Windows窗體設(shè)計(jì)器支持所必需的
- file://
- InitializeComponent();
- word.Font=TextStringFont;
- word.ForeColor=TextStringcolor;
- System.Windows.Forms.Cursor.Hide();file://隱藏光標(biāo)
- file://
- //TODO:在InitializeComponent調(diào)用后添加任何構(gòu)造函數(shù)代碼
- file://
- }
- protectedoverridevoidDispose(booldisposing)
- {
- if(disposing)
- {
- if(components!=null)
- {
- components.Dispose();
- }
- }
- base.Dispose(disposing);
- }
- #regionWindowsFormDesignergeneratedcode
- ///
- ///設(shè)計(jì)器支持所需的方法-不要使用代碼編輯器修改
- ///此方法的內(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();
- //
- //設(shè)置文本顯示控件(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;
- //
- //設(shè)置計(jì)時(shí)器控件(timerSaver)屬性
- this.timerSaver.Enabled=true;
- this.timerSaver.Interval=5;
- this.timerSaver.Tick+=newSystem.EventHandler(this.timerSaver_Tick);
- //
- //設(shè)置圖片控件(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;
- //
- //設(shè)置窗體(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://鍵盤按下響應(yīng)事件
- this.KeyDown+=newSystem.Windows.Forms.KeyEventHandler(this.screen_KeyDown);
- file://鼠標(biāo)按下響應(yīng)事件
- this.MouseDown+=newSystem.Windows.Forms.MouseEventHandler(this.screen_MouseDown);
- file://窗體啟動(dòng)調(diào)用事件
- this.Load+=newSystem.EventHandler(this.Form1_Load);
- file://鼠標(biāo)移動(dòng)響應(yīng)事件
- this.MouseMove+=newSystem.Windows.Forms.MouseEventHandler(this.screen_MouseMove);
- this.ResumeLayout(false);
- }
【編輯推薦】