Writing a screen saver in Visual C #

Source: Internet
Author: User
Tags abs exit visual studio
visual| program Visual C # is Microsoft's new generation of program development language, Microsoft. NET Framework, an important part of the. The screen saver is a standard Windows executable that has an SCR extension. Screen savers can not only prolong the life of the display, but also protect private information. This article introduces you to one. NET platform in C # written in a dynamic text and graphics screen saver.

First, the concrete realization steps:

(1) Create a new C # Windows Application project under Visual Studio.NET, which may be named Screen_saver.

(2) Now we're going to design the main interface of the program:

Set the form's Name property to the screen, the Text property set to NULL, the BackColor property to black, the Size property set to (800, 600), ControlBox, MaximizeBox, MinimizeBox, The ShowInTaskbar property setting is false and the FormBorderStyle property is set to none. Add label controls, PictureBox controls, and one timer control to the form. Set the name of the label control to NULL for Word, the Text property, set the name of the PictureBox control to Picture1, set the image to a predictable picture, and set the timer control's name to Timersaver, The Enabled property is set to True and the Interval property is set to 5.

(3) Now we start writing the complete program Code section:

 

Namespaces used by the import

Using System;

Using System.Drawing;

Using System.Collections;

Using System.ComponentModel;

Using System.Windows.Forms;

Using System.Data;

file://

Namespace Screen_saver

{

///

Summary description of the Form1.

///

public class Screen:System.Windows.Forms.Form

{

file://Join Private Member variable

Private System.ComponentModel.IContainer components;

private int ispeed = 2;

private string str= "Fujian South Textile Joint stock company Computer Center";

file://defines text font and size

Private System.Drawing.Font Textstringfont = new System.Drawing.Font ("Song Body", 10,system.drawing.fontstyle.bold);

Private Color Textstringcolor =system.drawing.color.yellow; file://Text Font Color

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://Timer Control

Private System.Windows.Forms.PictureBox Picture1; file://Graphics controls

Private System.Windows.Forms.Label word; file://Text Display control

///

The required designer variable.

///



Public screen ()

{

file://

Required for Windows Forms Designer support

file://

InitializeComponent ();

Word. Font=textstringfont;

Word. Forecolor=textstringcolor;

System.Windows.Forms.Cursor.Hide (); file://Hide Cursor

file://

TODO: Add any constructor code after the InitializeComponent call

file://

}

///

Clean up all resources that are in use.

///

protected override void Dispose (bool disposing)

{

if (disposing)

{

if (Components!= null)

{

Components. Dispose ();

}

}

Base. Dispose (disposing);

}

#region Windows Form Designer generated code

///

Designer supports required methods-do not use the Code editor to modify

The contents of this method.

///

The component used in the private void InitializeComponent () file://initialization program

{

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 ();

//

Set text display control (word) Properties

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;

//

Set Timer control (Timersaver) properties

This.timerSaver.Enabled = true;

This.timerSaver.Interval = 5;

This.timerSaver.Tick + = new System.EventHandler (This.timersaver_tick);

//

Set picture control (Picture1) properties

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;

//

Set form (screen) property

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://the keyboard presses the response event

This. KeyDown + = new System.Windows.Forms.KeyEventHandler (This.screen_keydown);

file://Mouse Down Response event
 
This. MouseDown + = new System.Windows.Forms.MouseEventHandler (This.screen_mousedown);

file://Form Start Call event

This. Load + = new System.EventHandler (this. Form1_Load);

file://Mouse Move Response event

This. MouseMove + = new System.Windows.Forms.MouseEventHandler (this.screen_mousemove);

This. ResumeLayout (FALSE);

}

#endregion

///

The main entry point for the application.

///

[STAThread]

static void Main (string[] args)

{

if (args. Length==1)

if (args[0). Substring (0,2). Equals ("/C"))

{

MessageBox.Show ("No Settings feature", "C # screen Saver");

Application.exit ();

}

else if (args[0]== "/S")

Application.Run (New screen ());

else if (args[0]== "/A")

{

MessageBox.Show ("No password function", "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://Screen Display Area

Width1=ssworkarea.width; file://screen width

Height1=ssworkarea.height; file://screen Height

}


private void Timersaver_tick (object sender, System.EventArgs e) file://Timer response Event

{

Word. Visible=true;

Word. TEXT=STR;

Word. Height=word. Font.height; file://set the height of the text

Word. Width=word. text.length* (int) word. font.size*2; file://set the width of the text

Playscreensaver ();

}

private void Playscreensaver () file://Custom function

{

file://set the position coordinates of the text display box below

Word. Location =new System.Drawing.Point (Width1-idistance,word. LOCATION.Y);

Word. Visible=true; file://set to visible

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://below is the calculated picture frame move coordinates

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://Stop screen saver from running

{

System.Windows.Forms.Cursor.Show ();

Timersaver.enabled=false;

Application.exit ();

}


private void Screen_mousemove (object sender, System.Windows.Forms.MouseEventArgs e)
file://Mouse Movement Events

{

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://Mouse Down Event

{

Stopscreensaver (); file://Stop running Screen saver

}

private void Screen_keydown (object sender, System.Windows.Forms.KeyEventArgs e)
file://the keyboard press event

{

Stopscreensaver (); file://Stop running Screen saver

}

}

}


Finally run the program, change the Screen_saver.exe to SCREEN_SAVER.SCR, and copy it into the Windows system directory so that you can run the screen saver.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.