Screensaver code

Screen saver is executable that has file type *.scr and sitting in system directory. It is windows program that terminates self when user take some action like pushing the keyboard button or moving mouse. The animating part may be totally independent from windows part and can be done in different ways. The suggested is openGL, as the most convenient. The only part that require special attention is how to make screensaver available for a preview than means showing itself in a preview window. The solution can be seen from the code.

Screensaver preview

This screen saver is written as simple windows program combined with OpenGL graphics. The windows part is elementary. The concept of this program is isolation of windows part from the graphical GL part. In this project they are almost independent and even sitting in different files.

Windows part

All windows programs have few necessary elements that can be easily told from each other. First is entry point - the first function to call when program starts and others are call back functions i.e. functions that are called back by operating system when particular event occur. The entry point has distinct name and signature

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
WINAPI is calling convention that makes this function an entry point. This function body is list of several function calls that is executed top to bottom until it reaches the infinite loop where it starts continuously calling function

This function is interface to OpenGL. It is sitting in other file and interacting with list of other OpenGL functions defined in the same file. The windows program is terminated in case of user interaction with the mouse or keyboard. These events are specified in call back function

LONG WINAPI MainWndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
The name of this function is passed as parameter to a structure WNDCLASS in the WinMain function. This is basically a big picture how windows part works.


Every screen saver must be able to show self in a preview window shown in preamble. This small preview window is part of the dialog tab that pops up in response of right-hand side mouse button click and selection of the 'properties' option when cursor is on the desktop area. The preview is the different mode of execution of the program. This mode should be identified from inside of the program right after the start. When program is started from preview window it has different command line. This command line is extracted and parsed inside the function

parseCommand(HWND& hwndRef)
which returns one of the enumerated modes. When the mode is identified as preview, several parameters that change visual presentation are set differently in conditional fragment

if (scrMode == smPreview) {

	RECT rc; 
	GetWindowRect(hwndParent, &rc);
	cx = rc.right  - rc.left;
	cy = rc.bottom -;


	hDC = GetDC(hwndParent);
	format = ChoosePixelFormat(hDC, &pfd);
	SetPixelFormat(hDC, format, &pfd);
The example of this screen saver can be used as template for funnier and better graphical part without significant or any change in the windows part.