Creating custom screensaver in Windows

Tired of the normal screensavers running in your machine?? Want to create your own screensaver?? If you are still thinking of how to do it. This post will help you in meeting the requirement.

Requirements:

Visual Studio 2010

Windows Operating System.

 

1) Open your visual studio and create a new project. In your installed templates look for Windows Form Application and click on OK.

2) Rename the form as per your need let us say screensaver

3) In the form designer you can go ahead and add the controls as per your requirement. In this sample we will just add one label, timer control.

4) In the main method you should ensure that we get parameters for the main function as shown in the below code.

 static void Main(string[] args) 
 {
 if (args.Length > 0)
 {
 if (args[0].ToLower().Trim().Substring(0,2) == "/c")
 {
 MessageBox.Show("No parameters are passed");
 }
 else if (args[0].ToLower() == "/s")
 {
 for (int i = Screen.AllScreens.GetLowerBound(0); i <= Screen.AllScreens.GetUpperBound(0); i++) 
 System.Windows.Forms.Application.Run(new ScreenSaverForm(i)); 
 }
 }
 else
 {
 for (int i = Screen.AllScreens.GetLowerBound(0); i <= Screen.AllScreens.GetUpperBound(0); i++) 
 System.Windows.Forms.Application.Run(new ScreenSaverForm(i)); 
 }
 }
 5) After making modification in the main method. We should copy the initialize component in the designer.cs file. 
 6) Now delete the designer.cs file and paste the initialize component method form1.cs/screensaver.cs file which you have used. 
 7) In the screensaver.cs/form1.cs file you can paste the following code.
 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Windows.Forms;
 
 namespace NewScreenSaver
 {
 public partial class ScreenSaver : Form
 {
 
 private System.ComponentModel.IContainer components;
 private Label label1;
 private Timer timer2;
 
 private Point MouseXY; 
 private int ScreenNumber;
 private int xPos = 0, YPos = 0;
 private ImageList imageList1;
 private Button button1;
 public string mode = "LR";
 
 
 public ScreenSaver(int i)
 {
 InitializeComponent();
 ScreenNumber = i;
 
 }
 
 protected override void Dispose(bool disposing)
 {
 if (disposing)
 {
 if (components != null)
 {
 components.Dispose();
 }
 }
 base.Dispose(disposing);
 }
 
 
 
 private void ScreenSaver_Load(object sender, EventArgs e)
 { 
 xPos = label1.Location.X;
 YPos = label1.Location.Y;
 mode = "LR";
 timer2.Start();
 
 this.Bounds = Screen.AllScreens[ScreenNumber].Bounds;
 Cursor.Hide();
 TopMost = true;
 }
 
 private void OnMouseEvent(object sender, System.Windows.Forms.MouseEventArgs e)
 {
 if (!MouseXY.IsEmpty)
 {
 if (MouseXY != new Point(e.X, e.Y))
 Close();
 if (e.Clicks > 0)
 Close();
 }
 MouseXY = new Point(e.X, e.Y);
 }
 
 private void ScreenSaver_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
 {
 Close();
 }
 
 #region Windows Form Designer generated code
 /// <summary>
 /// Required method for Designer support - do not modify
 /// the contents of this method with the code editor.
 /// </summary>
 private void InitializeComponent()
 {
 this.components = new System.ComponentModel.Container();
 this.label1 = new System.Windows.Forms.Label();
 this.timer2 = new System.Windows.Forms.Timer(this.components);
 this.imageList1 = new System.Windows.Forms.ImageList(this.components);
 this.button1 = new System.Windows.Forms.Button();
 this.SuspendLayout();
 // 
 // label1
 // 
 this.label1.AutoSize = true;
 this.label1.BackColor = System.Drawing.Color.Silver;
 this.label1.Font = new System.Drawing.Font("Vivaldi", 100F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic))), System.Drawing.GraphicsUnit.Point, ((byte)(0)));
 this.label1.ForeColor = System.Drawing.SystemColors.MenuHighlight;
 this.label1.Location = new System.Drawing.Point(31, 9);
 this.label1.Name = "label1";
 this.label1.Size = new System.Drawing.Size(894, 159);
 this.label1.TabIndex = 0;
 this.label1.Text = "My Screensaver.";
 // 
 // timer2
 // 
 this.timer2.Tick += new System.EventHandler(this.timer2_Tick);
 // 
 // imageList1
 // 
 this.imageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
 this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
 this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
 // 
 // button1
 // 
 this.button1.Location = new System.Drawing.Point(39, 17);
 this.button1.Name = "button1";
 this.button1.Size = new System.Drawing.Size(75, 23);
 this.button1.TabIndex = 1;
 this.button1.Text = "button1";
 this.button1.UseVisualStyleBackColor = true;
 // 
 // ScreenSaver
 // 
 this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
 this.BackColor = System.Drawing.Color.HotPink;
 this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
 this.ClientSize = new System.Drawing.Size(995, 551);
 this.Controls.Add(this.button1);
 this.Controls.Add(this.label1);
 this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
 this.Name = "ScreenSaver";
 this.Text = "ScreenSaver";
 this.TransparencyKey = System.Drawing.Color.Red;
 this.Load += new System.EventHandler(this.ScreenSaver_Load);
 this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.ScreenSaver_KeyDown);
 this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.OnMouseEvent);
 this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.OnMouseEvent);
 this.ResumeLayout(false);
 this.PerformLayout();
 
 }
 #endregion
 
 
 private void timer2_Tick(object sender, EventArgs e)
 {
 if (this.Width == xPos)
 {
 //repeat marquee
 this.label1.Location = new System.Drawing.Point(0, YPos);
 xPos = 0;
 }
 else
 {
 this.label1.Location = new System.Drawing.Point(xPos, YPos);
 xPos += 2;
 } 
 } 
 }
 }
 
 8) In the above code the initialize component will be the same as the one which you have copied from your designer.cs file. 
 9) After making all the modifications build your application. After building your application you can go to the exe file located in the debug/release folder. 
 10) Rename the exe file to scr. 
 11) After renaming the file you can go ahead and right click the file and click on install.
  
 Cheers!! now you will be able to see your screensaver file.
  
 Note: This code is just for testing purpose and should be not be used in your normal working environment.