Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / Win32

A Simple Screensaver in C through Win32: CodeRain

1.57/5 (4 votes)
16 Aug 2012CPOL5 min read 25.7K   96  
ScreensSaver through fullscreen mode using Win32 APIs in C.

Introduction

It all started with a day I was really pissed off with the default Windows XP screensavers. Nothing was really great about those screensavers and then I started thinking to write one on my own in C using Win32 APIs when I get time (usually by sparing time). I am much fascinated about all the graphics and other stuff too. But I knew that I could not do that all at a time not even in short time. So I started learning Win32 programming through "Programming by Windows by Charles Petzold". As a beginner in Win32 programming it was very difficult for me to grasp even the basics of Window programming as it is not much simple when compared to other console programs. 

This is not an expert article about creating screen savers. But I could somehow achieve by making the executable as a screen saver through very simple steps. It did work in my machine.  

I have used Visual Studio 2010 to write and compile the .C/.H source codes. I really insist to have a look at Charles Petzold's book even before looking into the attached code as the Windows programming model is all together a different approach.

If a fresher who never had experience dealing with GUI through Win32 happens to look at the source thinks that I am an alien. But believe me it is quite different to that of a console program. But it is really good.

I had the same feeling too while learning the concepts.

Background 

The source code provided in this article is written in C on Windows. It is just fine if you know something about how to program on a compiler through C. 

I am targeting this article to Beginners who would really want to do something in full screen and then displaying a pattern of characters which would bring some level of interest of your friends.  

All you can do is try to create a project in visual c++ or Visual studio and start adding the below mentioned files to it. 

Create a Win32 application and then start adding the below mentioned files to Source files and Header files from the downloadable.

  1. MyMain.c
  2. MyWallProcs.c
  3. MyResources.h
  4. MyWallProcs.h

As it is a multi-threaded program, to compile the code please follow the below instructions. 

Using VC++ 6.0

Whenever you create a multithreaded Windows program, you need to change something in the Project Settings dialog box. Select the C/C++ tab, and select Code Generation in the Category combo box. In the Use Run-Time Library combo box, you should see Single-Threaded for the Release configuration and Debug Single-Threaded for the Debug configuration. Change these to Multithreaded and Debug Multithreaded, respectively. This will change a compiler flag to /MT, which the compiler needs to compile a multithreaded application. In particular, the compiler inserts the LIBCMT.LIB filename in the .OBJ file rather than LIBC.LIB. The linker uses this name to link with the run-time library functions. 

How to create a Screen Saver:

1. Once you have compiled the source code which would finally generate a .exe file, make sure to copy that to a temporary location and then rename it to .scr file extension. For example: a.exe to a.scr.

2. Now copy the a.scr to "C:\\WINDOWS\\SYSTEM32" folder

3. Right click on Desktop and you should find the 'a' screen saver in the drop down list of screen savers. 

Now do your stuff from here on.  

Note: You could do the same thing with the CodeRain.exe available in the downloadable version. 

The .scr file does work on my machine as a screen saver. 

Now...lets look into the concept that I actually wanted to implement on a full screen window as a screen saver. 

A code pattern and below is the sample of the rain I wanted to create through this thread and even I want the tail of the tree to blink as well. So I will show you how I achieved the way of blinking the way I wanted it to be. Consider each line below as a level and the length of the total levels as a tree, example as below:

level-1: 1 1 1 1 1 1                                                                 
level-2: 1 1 1 1 1 1                                                                 
level-3: 1 1 1 1 1 1                                                                 
level-4: 1 1 1 1 1 1                                                                 
level-5  1 1 1 1 1 1                                                                 
level-6  1 1 1 1 1 1                                                                 
level-7  0 1 0 c  ^

Now keeping the levels below 7 as constants and try changing the values of the level 7. We can achieve the blinking in some way and though blinking of a character has several other different meanings too. But I wanted the blinking of the level-7 in this way and thought it would be really interesting. I was following through out my thread this logic and simultaneously printing any number of trees across the wall and dynamically choosing the height of the tree. It is easy, isn't it?

I thought to learn Win32 programming in C, this could be a real good mini-project for me and then I started thinking of an interesting concept code rain.  Below is the name of the thread which does the actual implementation of the CodeRain in the project.

C++
VOID RunCodeRain(PVOID pvoid)  

Points of Interest

I have learned a few things while working with this project like below.

  1. Handling Win32 windows using raw APIs.
  2. Displaying Text on Output window at various Vertical and Horizontal positions.
  3. Various fonts and sizes of text.
  4. Fullscreen windows. 

and a lot more... 

It may not be the perfect code for experienced professionals, but it is ok for me that I have done something different. I will think on re-writing the entire screen saver again and again with enhanced functionality. 

But for now it is fine. Please reply with your valuable comments. 

History

This is version 0.1 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)