Write screen saver with Visual C #

zhaozj2021-02-17  63

Huang Jianzhi Yesky

Visual C # is a new generation of procedures issued by Microsoft, an important part of the Microsoft .NET framework. The screen saver is a standard Windows executor with SCR's extension. The screen saver can not only extend the life of the display, but also protect private information. This article introduces you a dynamic text and graphic screen saver written in C # on a .NET platform. 1. Specific implementation steps: (1) New C #'s Windows application project under Visual Studio.Net, you may be named Screen_Saver. (2) Now we will design the main interface of the program: Set the name property of the form to Screen, the text property setting is empty, the BackColor property is set to Black, the size property is set to (800, 600), Controlbox, MaximizeBox, MinimizeBox The showintaskbar property settings are false, and the FormBorderstyle property is set to None. Add the Label control, the PictureBox control, and each of the Timer controls. Set the Name of the Label control to Word, the Text property is set to Picture1, the image is set to a predictive picture; set the Name of the Timer control to True, the interval property is set to 5. (3) Now let's start writing the full program code section:

// used to import the namespace using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; file: // namespace screen_saver {/// // Abstract illustration of / Form1. /// public class screen: system.windows.forms.form {file: // Add private member variable private system.componentmodel.icontainer Components; private int ispeed = 2; private string str = "Fujian Nanjing Co., Ltd. Computer Center" ; file: // define the text font and size of private System.Drawing.Font TextStringFont = new System.Drawing.Font ( "Arial", 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: // Graphic Control Private System.Windows.Forms.Label Word; File: // Text Display Control / / / / / Required Designer variable. /// public screen () {file: // // Windows File Designer Support for File: // InitializationComponent (); word.font = textstringFont; Word.ForeColor = TextStringColor; System.Windows.Forms.cursor .Hide (); file: // Hide Cursor File: // // Todo: Add any constructor code file: //} // / // / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /// protected {if (disponents! = Null) {components.dispose ();}} Base.dispose ();} #region Windows form designer generated code //////////////> Disposing; / /// Designer Support the required method - Do not use the code editor to modify the // / this method.

/// private void InitializeComponent () file: // initialization routine using the components {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 (); // // Settings text display control (Word) property this.drawing.color = 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; // // setting Timer Control (Timersaver) THIS.TIMERSAVER.Enabled = true; this.timersaver.interval = 5; this.timersaver.tick = new system.eventhandler (this.timersaver_tick); // // Set picture control (Picture1) Attribute this.picture1.image = ((System.drawing.bitmap) ("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.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: // event in response to key presses this.KeyDown = new System.Windows.Forms.KeyEventHandler (this.screen_KeyDown); file: // Mouse Press Response Event this.mousedown = New System.windows.Forms.MouseEventHandler (this.Screen_MouseDown); File: // Form Startup Call Event THISLOAD = New System.EventHandler (this.form1_load); File: // Mouse Mobile Response Event this.mousemove = New System.Windows.Forms.MouseEventHandler (this.screen_mousemove); this.ResumeLayout (false);} #ENDREGION / / / / The primary entry point of the application.

/// [stathread] static void main (string [] args) {if (args.length == 1) IF (args [0] .substring (0, 2). Equals ("/ c")) {MessageBox. SHOW ("No Set Item Features", "C # Screen Saver"); Application.exit ();} else if (args [0] == "/ s") Application.Run (new screen ()); Else IF Args [0] == "/ a") {MessageBox.show ("No Password Features", "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 region width1 = ssWorkArea.Width; file: // Screen Width Height1 = SSWORKARE.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: // Setting the height of the text word.width = word.text.Length * 2; file: // Setting the width of the text Playscreensaver () } Private void playscreensaver () file: // Custom Function {file: // The following settings Word.location = New System.drawin g.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 calculating the picture frame mobile coordinate SPEED ; if (speted <= 2 * height1) {x1 = system.math.abs (width1-speted); Y1 =

System.math.abs (HEIGHT1-Speed);} else if (Speed> 2 * Height1 && Speed ​​<= 2 * width1) {x1 = system.math.abs (width1-speted); Y1 = system.math.abs ( HEIGHT1- (speted-speted / height1 * height1))));} else if (speted> 2 * width1 && speted <= 3 * height1) {x1 = system.math.abs (Width1- (speed-speted / width1 * width1))) ; Y1 = system.math.abs (Height1- (speted-speted / height1 * height1));} else if (speted> 3 * height1 && speted <4 * height1) {x1 = system.math.abs (width1- SPEED-speed / width1 * width1)); y1 = system.math.abs (speed-speted / height1 * height1);} else if (speted> = 4 * height1 && speted <5 * height1) {x1 = system.math .Abs (speted-speted / width1 * width1); y1 = system.math.abs (Height1- (speted-speted / height1 * height1));} else if (speted> = 5 * Height1 && speed <4 * width1) {X1 = system.math.abs (speed-speted / width1 * width1); y1 = system.math.abs (speted-speted / height1 * height1);} else if (speted> = 4 * Width1 && speed <6 * Height1) {x1 = system.math.abs (Width1- (speted-speted / width1 * width1)))); Y1 = system.math.abs (speed-speted / height1 * height1);} else} (speted> = 6 * Height1 && speted <5 * width1) {x1 = system.math.abs (Width1- (speted-speted / width1 * width1)); Y1 = system.math.abs (Height1- (speted-speted / height1 * HEIGHT1));} else if (speted> = 5 * width1 && speted <7 * height1) {x1 = system.math.abs (speted-speted / width1 * width1); y1 = system.math.abs (Height1-) SPEED-SPEED / Height1 * Height1));} else if (speed> = 7 * height1 && speted <6 * width1) {x1 = system.math.abs (speed-speted / width1 * width1); y1 = system.math .Abs (speted-speted / height1 * height1);} if (speted == 6 * width1) speed = 0;

Picture1.location = new system.drawing.point (x1, y1);} private void stopScreensaver () file: // Stop screen saver runs {system.windows.forms.cursor.show (); Timersaver.enabled = false; Application.exit ();} private void screen_mousemove (Object sender, system.windows e) file: // mouse mobile event {if (ixstart == 0 &&iction == 0) {ixstart = EX; iYstart = eY; return;} else if (eX = ixStart || eY = 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: // keyboard Press Event {StopScreensaver (); file: // Stop running screen protection The program}}} finally runs the program, change Screen_Saver.exe to Screen_Saver.scr, copy it into the Windows system directory so that the screen saver can be run.

转载请注明原文地址:https://www.9cbs.com/read-29052.html

New Post(0)