• (2 Pages)
  • +
  • 1
  • 2

Write your own Windows screen saver! How to get started on writing one Rate Topic: ***** 5 Votes

#1 WolfCoder  Icon User is offline

  • Isn't a volcano just an angry hill?
  • member icon


Reputation: 784
  • View blog
  • Posts: 7,613
  • Joined: 05-May 05

Post icon  Posted 12 July 2006 - 11:08 PM

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 :)

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:

#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:

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:

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:

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:

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!

Is This A Good Question/Topic? 2
  • +

Replies To: Write your own Windows screen saver!

#2 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1875
  • View blog
  • Posts: 20,282
  • Joined: 17-March 01

Posted 13 July 2006 - 08:08 AM

Nice tutorial, I might have to try this.
Was This Post Helpful? 0
  • +
  • -

#3 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Posted 13 July 2006 - 09:08 AM

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?)
Was This Post Helpful? 0
  • +
  • -

#4 WolfCoder  Icon User is offline

  • Isn't a volcano just an angry hill?
  • member icon


Reputation: 784
  • View blog
  • Posts: 7,613
  • Joined: 05-May 05

Post icon  Posted 13 July 2006 - 09:27 PM

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 July 2006 - 09:27 PM

Was This Post Helpful? 0
  • +
  • -

#5 MarkoDaGeek  Icon User is offline

  • Dirty Technophile
  • member icon

Reputation: 11
  • View blog
  • Posts: 11,158
  • Joined: 13-October 01

Posted 26 July 2006 - 09:39 AM

Nice tutorial, Good work.
Was This Post Helpful? 0
  • +
  • -

#6 k.sangeeth  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 62
  • Joined: 27-July 07

Posted 01 August 2007 - 05:19 AM

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
Was This Post Helpful? 0
  • +
  • -

#7 Den12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 13-September 07

Posted 29 January 2008 - 07:29 AM

cool this is one ill have to try.


thanks wolfcoder.

i always wanted to make my own screen saver thanks.
Was This Post Helpful? 0
  • +
  • -

#8 royjm248  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 18-January 08

Posted 10 February 2008 - 01:53 AM

This looks good, and makes more sense than the visual tutorials. Thanks for posting :^: , I am going to try this and see what I do B) . By the way, I like your idea! How did it go?
Was This Post Helpful? 0
  • +
  • -

#9 Rowno  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 18-November 08

Posted 29 December 2008 - 10:32 PM

Nice tutorial, I'll be sure to give it a try in the future when I have enough spare time to create a screen saver ;)
Was This Post Helpful? 0
  • +
  • -

#10 alf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 22-February 09

Posted 15 April 2009 - 02:25 PM

Looks like a really great tutorial. I like your idea about an RPG that saves itself upon exit.

Would be cool with a virtual society or some sort of fishtank that spawned new fish etc. At least something that evolved day by day ^-^. Awsome idea.
Was This Post Helpful? 0
  • +
  • -

#11 bhermance  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 30-April 09

Posted 19 August 2009 - 04:57 PM

Thanks for the tutoral on how to make a screensaver! I will try it as soon as I learn how to do some basic programming. I have always wanted to do this! ( I am a newbie here )
I am hoping to find some free training program or guide on here before I start!

Thanks Again!,
Brett
Was This Post Helpful? 0
  • +
  • -

#12 Project 2501  Icon User is offline

  • import awesome as Project_2501
  • member icon

Reputation: 9
  • View blog
  • Posts: 1,565
  • Joined: 14-December 08

Posted 26 November 2009 - 11:41 PM

Very nice tutorial! I'm gonna try this tonight
Was This Post Helpful? 0
  • +
  • -

#13 milleja46  Icon User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 272
  • Joined: 17-December 09

Posted 03 January 2010 - 08:12 AM

Great tut :^: i am going to try this later after church!
Was This Post Helpful? 0
  • +
  • -

#14 borninlyoko  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 7
  • View blog
  • Posts: 350
  • Joined: 03-December 09

Posted 10 January 2010 - 12:40 AM

Cool, I'd love to do this, but don't have the time/patience/attention span
Was This Post Helpful? 0
  • +
  • -

#15 Guest_snake32*


Reputation:

Posted 06 March 2010 - 03:50 AM

Perfect tut!
But I have one question:
Whether it is possible to use it for Windows 7 or Windows Vista?

IMHO: to Use WM_TIMER for animation it is not good.
Was This Post Helpful? 0

  • (2 Pages)
  • +
  • 1
  • 2