How To Write Your Own Screensaver Using Visual C++
How To Write Your Own Screensaver Using Visual C++
In this article, I will tell you how to write your own screensaver program using Visual
C++. The prerequisite to learn this is that you must have programming knowledge in
Visual C++. The very first thing you must know that screensavers are also executable
files with extension “.scr”. If you can write a program to create some animation in a
window, you can easily build a screensaver too. A screensaver also draw the animation in
side a window only. But this window is little different from the conventional one. In a
screensaver, we create a window without any caption bar, menu bar, toolbar, status bar.
The size of the window is equal to the display size of your monitor. Once you have
created this window, keep the background color black (or any other color of your choice).
Now draw some picture, geometry inside the window and animate it. That’s it. Your
screensaver is ready. But still few things are missing. You know that on some user input
action like button press or mouse move etc. the screensaver must disappear. You must
catch these user input signals and destroy the screensaver window to finish it. This way
you can create a screensaver of your own design. Once you have created the screensaver,
change its extension from .exe to .scr and copy it to C:/Windows/system32 directory. It
will automatically be listed in the screensaver list.
Now, let me tell you how you can do all this using Visual C++ programs.
m_lpszClassName = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW,
::LoadCursor(AfxGetResourceHandle(),
MAKEINTRESOURCE(IDC_NULLCURSOR)));
Now with these three steps, you can create a rectangular window of full screen size
without a cursor.
During the creation of the window, set the time loop parameter as shown below.
This function ensures that the WM_TIMER event will be generated every 1/20 sec. This
will be helpful in creation and updation of your animation.
Here, you must update the animation variables etc. Always send an invalidate message
from this function call. This will call the OnPaint( ) message. This way, you can update
your drawing on the screen.
Keep all your drawing code inside this function. This is the heart of the screensaver. The
content of this function actually governs the screensaver.
All these callback functions must have the following line inside them –
PostMessage(WM_CLOSE);
So, this is all about how you can write your own screensaver. So do not wait START
WRITING.