banner



How To Make Your Own Screensaver Windows 8

  • Download source files - eight.35 Kb
  • Download demo project - xviii.3 Kb

Sample screensaver image

Acquittance

I would like to beginning off this article by thanking all of those individuals who have sent me unbelievable number of emails appreciating my previous articles, peculiarly the ISAPI Extension one! You cannot imagine how I have been inspired by those lovely emails. I practise appreciate your time, and I hope that this article suits your needs also.

Introduction

Unlike the human life that could be hardly saved 'coz of existence invaded by other different so-called humans, computer monitors could be easily saved against the phosphor burn using a screen saver! Today, I am going to talk near Windows Screen Savers, i of those interesting topics that most of the programmers out in that location would like to know about.

Fundamentals

A screen saver is just a plain Win32 awarding that will be launched automatically when the mouse and keyboard accept been idle for a specified menses of time. Whenever this timer expires, the user could see either a blank screen or a very complex animation. Thereafter, if the user presses a cardinal on her keyboard or moves her mouse, the screen will be inverse back to the normal 1 that disappeared when the screen saver started.

To create your own screen saver, y'all take got two choices. First, you can create a normal Win32 application and handle the necessary messages sent to your window. Choosing the first mode, you lot take to develop a programme that creates a full screen window, also as a window procedure that processes window messages sent to your application. Within the window process, yous could accept something every bit follows:

          switch(bulletin) {          case          WM_SYSCOMMAND:          if(wParam == SC_SCREENSAVE || wParam == SC_CLOSE)          render          Imitation;          pause;          case          WM_LBUTTONDOWN:          instance          WM_MBUTTONDOWN:          case          WM_RBUTTONDOWN:          example          WM_KEYDOWN:          example          WM_KEYUP:          case          WM_MOUSEMOVE:                   break; }

In other words, you have to difficult lawmaking the pieces required for your screen saver and take care of the letters that is common along all the Windows screen savers.

The second choice, though, is trying to utilize a library file that is existence shipped with your compiler and does all of the to a higher place-mentioned hard coding (and even more than)! This style, yous tin can focus on the actual problem - that is creating the visual effects. Patently, we are going to utilise this library to avoid re-inventing wheels.

The screen saver library (scrnsave.lib) contains the WinMain office. This function among the other things, registers a window class that looks something equally follows:

WNDCLASS cls;   cls.hCursor = Cypher; cls.hIcon = LoadIcon(hInst, MAKEINTATOM(ID_APP)); cls.lpszMenuName = NULL; cls.lpszClassName =          "          WindowsScreenSaverClass"; cls.hbrBackground = GetStockObject(BLACK_BRUSH); cls.hInstance = hInst; cls.style = CS_VREDRAW | CS_HREDRAW | CS_SAVEBITS | CS_DBLCLKS; cls.lpfnWndProc = (WNDPROC) ScreenSaverProc; cls.cbWndExtra =          0; cls.cbClsExtra =          0;

In other words, information technology registers a black background window, having no mouse cursor and an icon identified by ID_APP. Moreover, information technology introduces its window procedure as ScreenSaverProc. This procedure is the eye of any screen saver, and almost all the coding is done hither.

This procedure, similar all the other window procedures, is declared as follows:

LONG ScreenSaverProc(HWND hWnd, UINT bulletin, WPARAM wParam, LPARAM lParam);

where hWnd is the handle of the desktop window, message is the actual message sent to the screen saver window, and wParam and lParam are boosted message-specific information. If you could remember from my previous "MFC vs. Win32" series, you could call back that whenever we do not need to process a message, we pass information technology to the DefWindowProc role. Nonetheless, when developing a screen saver, we need to use the DefScreenSaverProc instead. Information technology is the same as DefWindowProc with the exception of taking care of the other messages needed to keep the screen saver running!

Anyway, the library header file is named scrnsave.h and is placed in the include directory of the compiler. It contains the necessary prototypes to support the screen saver library. There are some global variables defined in the library and the extern version is included in the above-mentioned header file. Two of those important variables have been declared as follows:

          extern          HINSTANCE hMainInstance;          extern          BOOL   fChildPreview;

where hMainInstance is the instance handle of the screen saver, and fChildPreview states whether the screen saver is currently running in preview mode. Don't worry, we will examine each of these, shortly.

Goals

We are going to create a screen saver, named, Ball Fusion, that contains 7 balls moving after each other on a sine moving ridge path:

x = sin(ß) * cos(ß) * cos(2ß) y = cos(ß)

where ß is a positive number betwixt 0 and 360. You tin can modify the formula, if y'all want.

Other considerations

The Ball Fusion volition not have any configuration dialog box, and this means if y'all try to cull the Settings button in the screen saver control panel, you will go nada in response. This is just because of the lack of time. Actually, my boss started to shout at me when he saw the screen saver running on my system at office! Then I stopped any improvements, and that's why information technology has got no Settings box!

Getting started

To start, launch MSVC++ vi.0. From the "File" carte, select the "New" item. With the "Projects" tab selected, highlight "Win32 Application", and in the "Project Name" edit box, type BallFusion, and press the Ok push button. At present, choose "An empty project" radio button, and printing "terminate" and "ok" respectively. Now, information technology is time to add together the BallFusion.cpp to our project to beginning coding. To practise so, select the "New" item from the "File" bill of fare. Having the "C++ Source File" particular selected in the "Files" tab, enter BallFusion equally the "File Name" and press OK. This way, we have got the base of operations framework in place.

Open the BallFusion.cpp to insert the following code:

#include          "          windows.h"          #include          "          scrnsave.h"          LRESULT WINAPI ScreenSaverProc(HWND hWnd,       UINT message, WPARAM wParam, LPARAM lParam) {          return          0; }

The screen saver library also needs us to include two other functions in our screen saver, ScreenSaverConfigureDialog and RegisterDialogClasses. The former receives messages sent to a screen saver's configuration dialog box whilst the latter is used to register any window class required by the configuration box. Since our screen saver doesn't have any configuration dialog box, we could simply skip the implementation. However, we take even so included them in our screen saver plan:

BOOL WINAPI ScreenSaverConfigureDialog(HWND hDlg,             UINT message, WPARAM wParam, LPARAM lParam) {                    render          Imitation; }  BOOL WINAPI RegisterDialogClasses(HANDLE hInst) {                    return          TRUE; }

The next step is to add scrnsave.lib to the library modules we are going to use in our program. To do so, from the "Project" menu, select "Settings" and make the necessary changes shown below:

Project Settings

Clarification

Having completed the setting process of the compiler options, information technology is now time to focus on the actual plan. I have divers some global variables within the program, as follows:

          #ascertain MAX_BALLS 7                    #define pi 3.141592625                    #define szAppName "BallFusion i.0"                    #define szAuthor "Written by Mehdi Mousavi, © 2001"                    #ascertain szPreview "Brawl Fusion 1.0"                    typedef          struct          _BALLS {     UINT uBallID;     HICON hIcon;          int          x;          int          y;          int          angle; }Assurance;  BALLS gBalls[] = {IDI_REDBALL, NULL,          0,          0,          0,           IDI_GREENBALL, NULL,          0,          0,          25,           IDI_BLUEBALL, Null,          0,          0,          50,           IDI_PURPLEBALL, NULL,          0,          0,          75,           IDI_LIGHTREDBALL, NULL,          0,          0,          100,           IDI_YELLOWBALL, Null,          0,          0,          125,           IDI_BLACKBALL, NULL,          0,          0,          150};

where MAX_BALLS is the number of the maximum assurance, szAppName is the application's name that is shown at the lesser of the leftmost of the screen besides as the writer's name (szAuthor), szPreview is the string that is shown to the user when she is previewing the screen saver from its control panel, and the Balls structure is the structure to store a brawl configuration. This configuration consists of the identifier of the ICON (uBallID), a handle to the icon itself after being loaded (hIcon), the x and y position of the ball, and the starting angle in which the brawl starts moving. Finally, there'southward gBalls, an assortment of the above-mentioned structure.

When the screen saver procedure receives the WM_CREATE message, information technology loads the ball icons co-ordinate to the identifier of each ball already declared in the gBalls variable and stores this loaded icons under the handle already placed in the Brawl structure (hIcon). Even so, please pay attention that to load the icons, the main case handle of the application (hMainInstance) is given to the LoadImage part:

          for(i =          0; i < MAX_BALLS; i++)     gBalls[i].hIcon = (HICON)LoadImage(hMainInstance,                     MAKEINTRESOURCE(gBalls[i].uBallID),                     IMAGE_ICON,          48,          48,                     LR_DEFAULTSIZE);

Then the starting position (x and y member of the Brawl structure) is calculated for each brawl, co-ordinate to the post-obit formula:

ten = sin(ß) * cos(ß) * cos(2ß) y = cos(ß)

In other words:

xpos = GetSystemMetrics(SM_CXSCREEN) /          2; ypos = GetSystemMetrics(SM_CYSCREEN) /          2;          for(i =          0; i < MAX_BALLS; i++) {          double          alpha = gBalls[i].angle * pi /          180;     gBalls[i].x = xpos +          int((xpos -          30) * sin(alpha) * cos(alpha) * cos(ii          * blastoff));     gBalls[i].y = ypos -          30          +          int(265          * cos(alpha)); }

And a black castor is created to exist used while putting the icons on screen and a timer is started:

hBrush = CreateSolidBrush(RGB(0,          0,          0)); uTimer = SetTimer(hWnd,          ane,          1, NULL);

When the timer is started, the screen saver process receives the WM_TIMER bulletin. For this message, all nosotros do is calculate the x and y position of each ball, while increasing the angle variable (0 <= angle <= 360).

And then, the rectangular area of which the ball should be invalidated is calculated and placed in the rc variable.

          for(i =          0; i < MAX_BALLS; i++) {          double          alpha = gBalls[i].angle * pi /          180;     gBalls[i].x = xpos +          int((xpos -          30) * sin(alpha) * cos(alpha) * cos(2          * alpha));     gBalls[i].y = ypos -          thirty          +          int(265          * cos(alpha));     gBalls[i].bending = (gBalls[i].bending >=          360) ?          0          : gBalls[i].angle +          one;      rc.left = gBalls[i].10;     rc.right = gBalls[i].x +          48;     rc.top = gBalls[i].y;     rc.bottom = gBalls[i].y +          48;      InvalidateRect(hWnd, &rc, False); }

When the invalidate function is chosen, the plan receives the WM_PAINT message. All we demand to do upon the reception of this bulletin is to put each ball on its position indicated by x and y:

          if(fChildPreview) {     SetBkColor(hDC, RGB(0,          0,          0));     SetTextColor(hDC, RGB(255,          255,          0));     TextOut(hDC,          25,          45, szPreview, strlen(szPreview)); }          else          {     SetBkColor(hDC, RGB(0,          0,          0));     SetTextColor(hDC, RGB(120,          120,          120));     TextOut(hDC,          0, ypos *          2          -          twoscore, szAppName, strlen(szAppName));     TextOut(hDC,          0, ypos *          2          -          25, szAuthor, strlen(szAuthor));          for(i =          0; i < MAX_BALLS; i++)         DrawIconEx(hDC,         gBalls[i].x,         gBalls[i].y,         gBalls[i].hIcon,          48,          48,          0,         (HBRUSH)hBrush,         DI_IMAGE); }

And what about fChildPreview? As we said earlier, this flag states whether the screen saver is currently running in preview mode. I.e., if the user tries to lookout the screen saver within the screen saver's command panel, fChildPreview is Truthful. Otherwise, it is Imitation.

Display Properties

Terminal note

The compiler generates a .EXE file for this projection. However, you need to rename it to .SCR then that the screen saver control panel tin can load it within the Screen Saver combo-box (as shown above). Another important thing to think, is to include an ICON identified past ID_APP in your application. This will be the screen saver'southward icon, since the library introduces this ID every bit the application'due south ID when registering the window grade.

cls.hIcon = LoadIcon(hInst, MAKEINTATOM(ID_APP));

That'due south all folks - Aloha!

Further improvements

I've got an thought that improves this saver, that is keeping the current desktop every bit the background of the saver and start the animating process on that. To understand what I am talking about, add the WM_ERASEBKGND message to your window process and compile the program once again. Of course, this improvement will be possible if, and only if, I tin convince my boss!

Any comments, suggestions and/or questions are welcomed.

Source: https://www.codeproject.com/articles/1551/creating-a-screen-saver

Posted by: taylorwashound.blogspot.com

0 Response to "How To Make Your Own Screensaver Windows 8"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel