Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 118,502 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 1,080 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Write your own Windows screen saver!

 
Reply to this topicStart new topic

> Write your own Windows screen saver!, How to get started on writing one

Rating  5
WolfCoder
Group Icon



post 12 Jul, 2006 - 11:08 PM
Post #1


In this tutorial, I will show you how to write a screen saver. First, create a new Win32 project (configure for Win32 platform). Create a new code file and pull up a char and grab some Mountain Dew or what have you.

You will need to know generally how to program WindowsAPI but i'm teaching you new API features to use. A screen saver is used to prevent phosphor burn for all you old school monitor fans. Since the new LCDs are here with their sharp images, the screen saver became more of just a cool thing to put on your computer anyway. It's supposed to run when the computer is idle for more than a granted amount of time. Some people password the saver to protect sensitive information that was visible on the screen before the screen saver ran.

Know that i'm done talking about them, here's how to code them. The normal main function for a windows program is already written! It's located in a file called ScrnSave.lib. You must link it to your project before you begin (MSVS users, right click on resources, add existing, and then navigate to the library file located under PlatformSDK and lib).

Then you must also link ComCtl32.lib because the screen saver library uses it. Don't worry! I won't make you write COM so relax smile.gif

Then include any other librarys and whatnot if you intend to use DirectX, OpenGL, GDI, or other graphic API for the screen saver.

Let's get to the point, here is a list of includes:

CODE

#include <windows.h>
#include <scrnsave.h>


These are needed in your code file. Nothing complicated, just an extra header for screen savers.

All of the basic window application initialization stuff was already done. The window procedure (read WindowAPI documentation if you don't what what that is) is already defined! It's called LONG WINAPI ScreenSaverProc(HWND hwnd,UINT message,WPARAM wparam,LPARAM lparam). That's the function prototype for the procedure. Here's what an empty basic one looks like:

CODE

LONG WINAPI ScreenSaverProc(HWND hwnd,UINT message,WPARAM wparam,LPARAM lparam)
{
    // Handles screen saver messages
    switch(message)
    {
    case WM_CREATE:
        // Creation of the screen saver window
        return 0;
    case WM_ERASEBKGND:
        // Erases the screen saver background
        return 0;
    case WM_TIMER:
        // Handles the timer
        return 0;
    case WM_DESTROY:
        // Cleans up the screen saver window
        PostQuitMessage(0);
        return 0;
    }
    return DefScreenSaverProc(hwnd,message,wparam,lparam);
}


These are the basic four message types to be handled. You must start up graphic engines and application under WM_CREATE. Fill the screen with black or whatever you want for a background under WM_ERASEBKGND. Place the screen saver logic for one frame under WM_TIMER (to move something, you would increment it's position by one here). WM_TIMER is called however many times a second you feel like it calling. Finally, clean up your app and graphics engines under WM_DESTROY.

You're missing an actual timer. You must set the timer so that your application will execute. Here's what you do:

CODE

uTimer = SetTimer(hwnd, 1, 1000, NULL);


Instead of 1000, enter the amount of milliseconds needed to pass for the code under WM_TIMER to be executed. uTimer is needed because:

CODE

KillTimer(hwnd, uTimer);


KillTimer must be placed under WM_DESTROY and that variable is needed to tell Windows what timer you're killing.

Now for the last bit:

CODE

BOOL WINAPI ScreenSaverConfigureDialog(HWND hwnd,UINT message,WPARAM wparam,LPARAM lparam)
{
    return true;
}
BOOL WINAPI RegisterDialogClasses(HANDLE hmodule)
{
    return true;
}


The two functions here are needed, but they can do nothing if you want them to. The configure dialog function is really supposed to pop up a dialog box so the user can configure your screen saver, but if you don't know how to write a dialog box, just return true.

The Register dialog box classes thing is for special controls in your configure dialog box, if you don't what to use classes for anything dialog related, just return true here too.

So, all you need to do is fill in the blanks with code simillar to an animation and you're set to run. But before you run, you must rename the .exe file to .scr and then run otherwise it does not work. It's annoying, but you're screen saver will close immediatly unless you give it a .scr extension. That way, Windows knows it's a screen saver, and will only terminate it when you move the mouse or press keys on the keyboard.

Have fun!
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

skyhawk133
Group Icon



post 13 Jul, 2006 - 08:08 AM
Post #2
Nice tutorial, I might have to try this.
Go to the top of the page
+Quote Post

born2c0de
Group Icon



post 13 Jul, 2006 - 09:08 AM
Post #3
The tutorial is on a subject that I've not seen on DIC so far.
I checked the code and all seems good.
It's great.(If it wasn't I wouldn't have approved it would I?)
Go to the top of the page
+Quote Post

WolfCoder
Group Icon



post 13 Jul, 2006 - 09:27 PM
Post #4
Thanks!

It took me a long while making sense of the giant help file on MSVS. Now I can have a screen saver I wrote. (geek/nerd stuff warning) Someone I know wrote a 2D cellular automaton program that ran as a screen saver. I'm thinking of applying the same concept to have a simulated MMORPG run when my computer is idle. It loads the state of the game on start-up and saves it all upon termination. It's interesting to play with the AI parameters and see what happens.

Of course it might be easier to write a geometry demo or something.

(edit fixed a spelling error with geometry)

This post has been edited by WolfCoder: 13 Jul, 2006 - 09:27 PM
Go to the top of the page
+Quote Post

MarkoDaGeek
Group Icon



post 26 Jul, 2006 - 09:39 AM
Post #5
Nice tutorial, Good work.
Go to the top of the page
+Quote Post

k.sangeeth
**



post 1 Aug, 2007 - 05:19 AM
Post #6
I tried it and I was able to make it work ..
Would be great if someone helps out how to do this on linux environment
Go to the top of the page
+Quote Post

Den12
*



post 29 Jan, 2008 - 07:29 AM
Post #7
cool this is one ill have to try.


thanks wolfcoder.

i always wanted to make my own screen saver thanks.
Go to the top of the page
+Quote Post

royjm248
*



post 10 Feb, 2008 - 01:53 AM
Post #8
This looks good, and makes more sense than the visual tutorials. Thanks for posting icon_up.gif , I am going to try this and see what I do cool.gif . By the way, I like your idea! How did it go?
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 10/11/08 07:49AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month