A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
How to make a screen saverHow to make a screen saver in C#, one very similar to the Windows XP screen saver. Tutorial and source code. |
On Wednesday, June 16th 2004 at 12:44 PM By Andrew Pociu (View Profile) ![]() ![]() ![]() ![]() (Rated 4.6 with 20 votes) |
|||||
|
Making a screen saver in C# is rather simple, but only if you know the 'secret'.
The screen saver is actually a form. So let's open it in Design mode and add a PictureBox to it named 'pictureBox1'. Set the BackColor property of the PictureBox to any color you wish. Now also change the BackColor property of the form to black, the ShowInTaskbar property to False and the FormBorderStyle property to None. The form should look similar to this one now: ![]() Now it's time to add a timer to our form for the animation to work. So add a timer named timer1 and set the Enabled property to True and the Interval property to 7000 (7 seconds). Let's get into the code of Form1.cs now. This is the code we use in Form1 class. So select all the code created by Visual Studio and delete it, then paste the code below, we will review it after.
From the above code let's first review the following piece:
What happens here? You ask yourself. Sure, when the mouse moves we want the screen saver to exit - that makes sense - but that looks a bit complicated, a simple Close() should do it, you cry. Well, for this piece of code I'm indebted to Rakesh Rajan with his tutorial that you might also want to consider. If you simply use Close(), when the screen saver starts it gets closed immediatly, that's because the mouse coordinates are passed to the application immediatly and the event fires. The Point variable saves the day because it is used to check the changes in mouse coordinates, therefore not considering the mouse coordinates passed to our screen saver, because at that time the if conditions will return false. A screen saver should usually be animated, that is actually the original purpose of a screen saver, to save your display from developing permanent etching of a pattern. This is why we change the position of the PictureBox every 7 seconds (using the timer) to a random place on the screen:
The transparent screen saverSomething pretty interesting is a transparent screen saver. Just set the opacity property of the form to 85% perhaps and check out the results. Geekpedia screen saverWe have a screen saver with the Geekpedia logo using this source code. There are two versions of this screen saver. The standard version:
And the transparent version: ![]() Download both screensavers (14.5 KB). Extract them to your System32 directory and open from Display Properties. Good luck |
||||||
Digg It!
Del.icio.us
Reddit
StumbleIt
Newsvine
Furl
BlinkList
|
||||||
|
||||||
Current CommentsNice :)
Nice one!
I like the transparency, but I need the desktop to show through when the screen saver kicks in. Any ideas if that is possible? Basically, I'm trying to develop a .scr that will allow us to lock the machine, but still see the desktop.
excellent tutorial well done guys...
1 question tho how to you make it show in the little monitor window like the others do? when you select from the drop down list of screen savers.
thanks
I haven't tried that yet, but I know you need to use the <i>/p</i> argument, like:
<b>if (args[0].ToLower().Trim().Substring(0,2) == "/p")</b>
You will need to make it at a smaller scale to fit in the preview window.
Sorry, I can't help you further (no time).
when compile this project, it general file .src?
No, but you can change the exe extension to scr, like I said in the tutorial:
"[...] compile the program, change the extension from .exe to .scr and copy it to your System32 directory where the other screensavers reside."
To use the "/p" parameter correctly you need to hit the API. When the p parameter is passed the HWND of the little display window is passed aswell as a second parameter. All you need to do is to make the HWND of the second window the parent of your window and resize your window.
You will also need to make sure that only one instance of your application is running at any time. This can be done in many ways, the easiest being to use the System.Diagnostics.Process.GetProcesses() method and enumerating through them, checking to make sure the title of the window (Process.MainWindowTitle) is not the same as yours.
am i right in saying that the project file opens with Visual Studio C++ ?
it won`t open for me. Some minor tips would be apprecieted
Not really, you need Visual Studio .NET. More exactly it's a C# project.
the .net you are talking bout is Visual Basic, Visual c++, Visual foxpro, Visual Interdev?? is that it??
i thought that was C++ only..
do i need to install all of em????
No, you need Visual Studio .NET, which is different and has nothing to do with Visual Basic, Visual C++, Visual FoxPro or Visual InterDev. You cannot develop applications with .NET Framework using any of the development environments you mentioned.
You can buy either Visual Studio 2003 or (even better) Visual Studio 2005.
Another solution would be to use Visual C# Express: http://msdn.microsoft.com/vstudio/express/visualcsharp/
In the worst case, you can use the free command line compiler for .NET 1.1 or .NET 2.0, if you're comfortable with compiling without an IDE.
Seeing as you are not familiar with .NET, I suggest you document more on this framework, you will find it very exciting.
ohhh tnx Andrei.... ima have to buy myself a copy of it first.. :D
Hey, this code is awesome! I am just learning C#...I would like to know how to modify this screensaver such that the opacity property changes with time when the system is undisturbed. How do I do that? Please help.
Darkening the background as time passes? That sounds like an interesting idea :)
What you need to do is implement a timer, and every 5 seconds you would increase the opacity property of the form, until it reaches 100%. Of course, you can make it every 1 second and then the screensaver would get solid black pretty soon, or you can even use the same timer that's used to move the logo every 7 seconds.
It is very informative and useful for C# program development
I would like to create .scr files but for PocketPC using C#. Does anybody know what is the coding format?
I know that the windows XP ones do not work on the PocketPC, even if they are the same file extension.
Thanks for your help.
So, being based on .Net, this screen saver will only work if the .Net environment is installed?
or will it work on all Windows systems 95 up to XP?
Just like any other .NET application, it will only work on systems that have the .NET Framework installed.
I have two monitors. When I run this example, it first displays on my primary monitor, leaving the secondary monitor displaying my desktop, and then on mouse move / key press, it displays on my secondary monitor, leaving my primary monitor displaying my desktop. Any ideas on how to merge the two?
Tom:
I had the same problem. The method used here is what I\'ve seen all over the net as the way to do multiple monitors, but here\'s what I\'ve done instead (which works well enough):
private void SaverForm_Load(object sender, System.EventArgs e)
{
int width = 0;
// Get total width for all monitors
foreach (Screen s in Screen.AllScreens)
width += s.Bounds.Width;
// Set bounds for the form so it covers the screen
// Assume (bad, yes, I know) that monitors are in a line and
// not stacked but this could be changed to include vertical configurations
this.Width = width;
this.Height = Screen.AllScreens[0].Bounds.Height;
this.Location = new Point(0, 0);
//... rest of code
Hope that helps someone!
Great code. Love it. I was actually looking for a program that would do this (allow transparency). The last step is figuring out how to allow the desktop to show through.
Other programs do this so there has to be a way. Any ideas?
Basically I need to program this scr to allow the screen to still show through but prevent mouse and keyboard activity, unless the user unlocks the screensaver.
I would imagine, that instead of opening the form to full size you open it to the size of your image block, and instead of positioning your image on the form you position the the form itsself.?
Only guessing though..
@Tom: This should deal with multi monitors in odd positions.
// Make form full screen and on top of all other forms
int minY = 0;
int maxY = 0;
int maxX = 0;
int minX = 0;
foreach (Screen screen in Screen.AllScreens)
{
// Find the bounds of all screens attached to the system
if (screen.Bounds.Left < minX)
minX = screen.Bounds.Left;
if (screen.Bounds.Width > maxX)
maxX = screen.Bounds.Width;
if (screen.Bounds.Bottom < minY)
minY = screen.Bounds.Bottom;
if (screen.Bounds.Height > maxY)
maxY = screen.Bounds.Height;
}
// Set the location and size of the form, so that it fills the bounds of all screens attached to the system
Location = new Point(minX, minY);
Height = maxY - minY;
Width = maxX - minX;
Cursor.Hide();
TopMost = true;
Note that you need to ensure the StartPosition is set to manual, otherwise the screen saver will start on the parent screen and not the left most screen
@Ryan: Set the Opacity property of the form to achieve this.
Nice article. I am having a problem with my screensaver though - the KeyDown event never fires. Any ideas? Thanks?
@Bry: I don\'t think Width and Height by themselves were what you were looking for. Perhaps Left+Width and Top+Height? But it looks like .net exposes a .Right and .Bottom property, so I\'ll use those.
(Note: I\'m using VB.net, but it should be adaptable)
\' Adapted from http://www.geekpedia.com/tutorial65_How-to-make-a-screen-saver.html
\' Also have to set me.StartPosition = Manual so that we start on the leftmost monitor
Dim minY As Integer = 0
Dim minX As Integer = 0
Dim maxY As Integer = 0
Dim maxX As Integer = 0
Dim curScreen As Screen
For Each curScreen In Screen.AllScreens
\' Leftmost edge
If curScreen.Bounds.Left < minX Then
minX = curScreen.Bounds.Left
End If
\' Rightmost edge
If curScreen.Bounds.Right > maxX Then
maxX = curScreen.Bounds.Right
End If
\' Topmost edge
If curScreen.Bounds.Top < minY Then
minY = curScreen.Bounds.Top
End If
\' Bottommost edge
If curScreen.Bounds.Bottom > maxY Then
maxY = curScreen.Bounds.Bottom
End If
Next
\'// Set the location and size of the form, so that it fills the bounds of all screens attached to the system
Me.Location = New Point(minX, minY)
Me.Height = maxY - minY
Me.Width = maxX - minX
\'Cursor.Hide();\'done elsewhere in my app
Me.TopMost = True
I am using Visual Studio 2005 and I'm running into trouble when it comes to compiling the code to get the .exe. When I publish the project, it doesn't create an .exe, it only creates a setup .exe in which the program gets installed...
So, I'm unable to rename it to .scr since I don't get an .exe, or am I missing something...
Sorry for the novice sounding question!
Okay, I just had to use a little common sense...I figured it out:
The file 'scrSaver.exe.manifest' is the file I need. I simply deleted the '.manifest' from the end and it worked; I was able to just rename it from .exe to .scr. After placing the file in the system32 folder I was good to go.
Hopefully this might help somebody else that runs into this little hangup!
Very thanks for the code. I made my own with the follow code in the timer action:
if (cerrar == true)
{
o = o - 0.01;
if (o < 0.0)
{
this.Close();
}
}
else
{
if (o > 0.9)
{
car = \"reste\";
primer = true;
}
if (primer == true)
{
if (o > 0.9) { car = \"reste\"; }
if (o < 0.5) { car = \"sume\"; }
}
if (car == \"sume\") { o = o + 0.01; }
if (car == \"reste\") { o = o - 0.01; }
}
this.Opacity = o;
The result: a nice fade open effect and a excelente pulsating fade effect and finishin with a quick fade effect.
I wanna use a /p and /c, can somebody hoy use /p to put a Image or a Gif animation. Thanks a lot.
i like this program n i do hope in future u will give me more
i like this program n i do hope in future u will give me more
i like this program n i do hope in future u will give me more
how do u add username and password buttons to the screensaver like CSI?
when I use vista, I cant see the name of my file in the screensaver list...
any idea, or solution?
thanx
Nice article
anyway to get a code for running a .swf file instead of the logo?
Just wondering if there is a way to have the form transparent (I have set a new timer to slide the forms opacity up and down) but to have the pictureBox1 not transparent. As it is at the moment, the picture inherits the opacity of it's form, which looks ... strange.
How can I use my images as a screen saver?
Do not enough money to buy some real estate? You not have to worry, because it's possible to take the <a href="http://bestfinance-blog.com">loans</a> to work out all the problems. Hence take a college loan to buy all you want.
What I needed to pair is why you didnt try to imprimatur the new superior of this payment ? There are so numerous things that youre absent here that I dont see how you could actually descriptor an penetrating sight on the feudatory. Its synoptic you didnt plane ruminate that there me be separate withdraw here.
Pass you for more organic article. Where encourage could anyone get that sympathetic of system in often a sum way of utilise ? I talking a show drawn muzzle, and I am on the spotter for abundance forgather.
Any screen saver is actually an executable (.exe) file with the extension changed to .scr. If you let the extension unchanged after you compile the program and you execute the .exe file, the screen saver will still work of course, but if you add it to the System32 folder in your Windows directory, the screen saver won't appear in the list at Display Properties. After changing the extension to .scr Windows will add the screen saver to the list.
Succeed you for writer nonsynthetic article. Where encourage could anyone get that kind of scheme in ofttimes a sum way of utilise ? I conversation a take drawn bind, and I am on the spotter for copiousness forgather.
What I had to be a couple, why not try the new director of the imprimatur of this payment? There are many things that are here that I do not like you can actually penetrating key feudal vision. The machine synoptic reflect this, I had to cancel a separate here.
I have been searching for some information about it almost three hours. You helped me a lot indeed and reading this your article I have found many new and useful information about this subject.
Juncture you for much structured article. Where encourage could anyone get that harmonious of method in oftentimes a sum way of utilise ? I talking a exhibit drawn hush, and I am on the spotter for quantity forgather.
Great blog article about the Great blog article about this topic, I have been lately in your blog once or twice now. I just wanted to say hi and show my thanks for the information provided.
In 2012, the lengthy colorful dresses is hot. But, owning towards the mild winter, extended UGG sales nicely.
Looks like you don't have permission over the Insert command. Check the user's credentials.and plix help me with the ASP code, for login. i want the registered users to login.When logging in the details should be retrieved from the database.
This is well known that cash makes people disembarrass. But how to act when somebody has no cash? The only one way is to receive the home loans and financial loan.
Once you have recreated the problem and captured these steps, you can save them to a file and send it to your support person, who can then open it up and view
I wanted to draft you one little bit of note just to say thank you as before with your stunning techniques you've featured on this page. It was quite seriously open-handed with people like you to provide without restraint all that a number of people might have distributed for an e-book to end up making some money for their own end, even more so seeing that you might well have done it in case you wanted. The secrets also acted like a easy way to realize that other people online have the identical dreams like mine to grasp good deal more when it comes to this issue. Certainly there are several more enjoyable occasions in the future for many who browse through your blog.
Very cool article. Screen saver is very great.Mali oglasi dijaspora - oglasi na internetu gde se oglašavaju srpska, hrvatska i bošnjačka dijaspora i domovina! Potražite ili ponudite nekretnine, posao, polovni automobili i vozila, usluge, poslovi, obaveštenja i drugo u Dijaspora malim oglasima.
Thanks.
Related Tutorials
Related Source Code
C# Job Search