Sei sulla pagina 1di 2

Saturn Software Mills

Visit for more technical article http://www.saturnsoftmills.com

How to write your own Screensaver using Visual C++

Screensaver is a program that is used primarily to safeguard your computer


monitor from phosphorus burn. If you keep your computer idle for few minutes, the static
picture on your monitor may be harmful for the health of your monitor. The fixed pattern
of pixels will induce the electron gun to hit the same area on your monitor with the same
intensity. This can permanently damage your monitor. To avoid such situation, people
keep screensavers in to their computers. The duty of these screensavers is to save your
computer screen from this situation. After a certain predefined idle time, the screensaver
activates itself and envelop the complete screen. The characteristic of good screensaver is
that it should cover the entire screen and colors, picture, geometry etc. should have some
motion in to it. You might have seen that most of the time the background of the
screensavers is kept black. This is a good practice to relax the computer monitor.

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.

1. You must register a new class of window with no cursor.

m_lpszClassName = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW,
::LoadCursor(AfxGetResourceHandle(),
MAKEINTRESOURCE(IDC_NULLCURSOR)));

2. Get the extent of your computer monitor

CRect rect(0, 0, ::GetSystemMetrics(SM_CXSCREEN),


::GetSystemMetrics(SM_CYSCREEN));
Saturn Software Mills
Visit for more technical article http://www.saturnsoftmills.com

3. Now create a window

CreateEx(WS_EX_TOPMOST, m_lpszClassName, _T(""),WS_VISIBLE|WS_POPUP,


rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,
pParentWnd->GetSafeHwnd(), NULL, NULL );

Now with these three steps, you can create a rectangular window of full screen size
without a cursor.

4. Use WM_TIMER event to create a time loop.

During the creation of the window, set the time loop parameter as shown below.

SetTimer(1, 50, NULL);

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.

5. Write a function OnTimer( ) to handle WM_TIMER event.

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.

6. Write drawing code inside the OnPaint( ) function.

Keep all your drawing code inside this function. This is the heart of the screensaver. The
content of this function actually governs the screensaver.

7. Write callback functions to handle user inputs

Add these callback functions in to your application –


OnMouseMove ( )
OnLButtonDown( )
OnMButtonDown( )
OnRButtonDown( )
OnKeyDown( )
OnSysKeyDown( )

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.

Potrebbero piacerti anche