Screen Saver Using C#

Introduction: 

In this article I will show you how to make a screen saver by using c# windows form. It is not perfect screen sever as windows gives us but yes, it is little bit like that.

Steps:

1) Add a form in your application and make it form border style none. Set transparency key as form background color so it will make your form transparent.

1.gif

2) Put a timer control on your form and set it's interval as you want to display picture per millisecond on the computer screen.

3) Code behind

int x, y;
private void timer1_Tick(object sender, EventArgs e)
{          
    /*
    * here x & y are the variables  and below two line will put desktop's different
    * screen location in it.
    */
    x = Convert.ToInt32((Microsoft.VisualBasic.VBMath.Rnd()) * (this.ClientSize.Width));
    y =Convert.ToInt32((Microsoft.VisualBasic.VBMath.Rnd())*this.ClientSize.Height));
    /*
    * screenStart() is the function which works to display image at the different screen
    * location of the desktop      
    */
    screenStart();
}

//This is the function which is called in the time1_Tick even
private void screenStart()
{
    //Picturebox is created dyanamically
    PictureBox pbox = new PictureBox();
    //and then after it will display at different location
    pbox.Location = new Point(x, y);
    pbox.Height = 100;
    pbox.Width = 100;
    //picture is set to the picturebox from the Resources.
    pbox.Image = global::screensever.Properties.Resources.thank_you_blue_rose;
    pbox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
    //and at last control will added to the form
    Controls.Add(pbox);
}

//This the Form1_Keyup even from where you will exit from the application while its running.
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
    if (Keys.Escape == e.KeyCode)
    {
        this.Close();
    }
}


Similar Articles