C # Make simple screen saver

Source: Internet
Author: User

Download source code: screensaver.rar
I used to write screen protection when I was a beginner at C #. At that time, I thought it was very profound and I couldn't find a clue, so I gave up. Today, I picked it up again and suddenly thought it was the same thing. It was very easy to know. Okay. Next we will introduce how to use C # To create screen protection.
Open Visual Studio. net2005 (similar to other versions) to create a Windows project. This screen saver imitates a screen saver provided by Windows XP.

Set the properties of the window:
1. First, set windowstate to formwindowstate. maximized so that the window can be displayed in the maximized state after being opened.
2. Set topmost to true, so that the window is at the top layer. This is called "always in the front". I believe that friends who have used jingting and Kingsoft and other software should be familiar with it.
3. Set formborderstyle to formborderstyle. None, so that the window has no border, that is, there is no title bar, and a circle around the border. This eliminates the need to remove maximizebox and minimizebox one by one.
4. Set backcolor to color. Black so that the background color of the window turns black.
5. Set showintaskbar to false (this step can be omitted, and the taskbar will not be visible if it is maximized)
Modifying other attributes is unnecessary. Of course, you can change the name and so on. In short, the above five (strictly speaking, four) have been completed, and the UI has been designed.

Drag a timer control to set its enabled attribute to true and interval to 10000 (because milliseconds is used as the unit, so it is set to 10,000, that is, 10 s ).
The timer control is used to trigger an event every certain event. Therefore, add an event to timer and click the lightning icon on Solution Explorer to add a unique tick event.
Now we can design the algorithm. It's actually a little simpler. We will randomly generate a vertex and then draw the prepared image. (For better viewing, you can set the relationship between coordinates)

Private readonly int screenwidth;
Private readonly int screenheight;
Private Bitmap bitmap = new Bitmap ("ms.bmp ");
Private random = new random ();
Private int x = 0;
Private int y = 0;

Adding a number of member variables is quite self-documenting. I just want to explain more. The first two are the width and height of the screen,
Then a bitmap object stores the prepared image. Here is a bitmap. Random is a random class object used to generate random points. The following are the X and Y coordinates of the generated random points respectively.
Add in Constructor

Rectangle rect = screen. primaryscreen. bounds;
Screenwidth = rect. width;
Screenheight = rect. height;

The first sentence is used to obtain the boundary range of the entire display. Assign the width and height to the member variables respectively.
Note: The Screen class provides some display-related attributes. If you are interested, refer to the msdn
Fill in the tick event below

Private void timereffectick (Object sender, eventargs E)
{
X = random. Next (screenwidth );
Y = random. Next (screenheight );
If (x + bitmap. width> screenwidth)
{
X = screenwidth-bitmap. width;
}
If (Y + bitmap. Height> screenheight)
{
Y = screenheight-bitmap. height;
}
This. invalidate ();
}

The preceding figure shows a point where the X and Y coordinates are not greater than the width and height of the display. To make the image more beautiful, it is not difficult to determine whether the image exceeds the screen boundary and process X and Y. I believe everyone can understand it. Finally, refresh the screen and call the paint event.
I'm sure you know that, right, there is a drawing statement in the form paint event, and the answer is correct!

E. Graphics. drawimage (bitmap, X, Y, bitmap. Width, bitmap. Height );

Note: During programming and testing, I found a very subtle and important place. It is precisely because of it that it aroused my desire to write this article. It should be noted that there are a series of drawimage overload functions under the graphics class.
Pay special attention to the two (I will just give an example, and others will understand it)

Public void drawimage (image, point );

And

Public void drawimage (image, int X, int y, int width, int height );

The former is to draw a physical image at a specified point, while the latter is to draw an image with a specified zoom size at a specified point.
What is the difference between the two? Let's take a look!

The image object stores the pixel width value and horizontal resolution value (points per inch ). The physical width (in inches) of the image is equal to the pixel width divided by the horizontal resolution. For example, the physical width of an image with a pixel width of 216 and a horizontal resolution of 72 points/inch is 3 inch. This description also applies to pixel height and physical height.

This method uses the physical size of the image to draw the image. Therefore, the image size (in inches) is correct regardless of the Resolution of the display device (points per inch. For example, assume that the pixel width of the image is 216 and the horizontal resolution is 72 points/inch. If you call this method to draw the image on a device with a resolution of 96 points/inch, the pixel width of the displayed image is (216/72) * 96 = 288.

Haha, that is, the former uses the physical size of the image, and the latter uses the specified size. The physical size is always the true size of the image displayed on a display with different horizontal resolutions. (Note that the width and height returned by bitmap are in pixels ). The suspect cloud was suddenly unlocked!

Add a program exit operation. Here I use the keyboard press or mouse focus change.
This is not difficult.

So far, you can see the expected results after executing the program. Finally, bind/debug or
Change the executable program suffix under bin/release to SCR, and copy it to system32 on the System Disk windows (the image is also copied in !)
In this way, the screen saver of desktop properties can be set to be ready for use. Isn't it amazing! Try it!

The program is compiled in Windows XP SP2 + visual studio.net 2005.

Postscript:
1. I found that there was a project C # under Visual Studio. net2005. The RSS content is displayed in the screen saver. I will report to you after a while.
2. it is inconvenient to copy images to system32. We can use embedded resources to compile them into the EXE file.

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.