In-depth programming of Screen Saver

Source: Internet
Author: User
Tags clear screen textout
Deep Dive into Screen Saver Programming Author: Dong Xiao Release Date: 2001/03/28
 
Abstr:
At present, the V program has become more and more exquisite and more powerful. It can provide all the multimedia functions such as images, animations, audios, and videos. Although we can easily obtain beautiful screen saver, making Screen Saver By ourselves may be more attractive to users. The following describes how to create screen saver in detail.

Body:  



In-depth programming of Screen Saver
  


Readers are quite familiar with screen saver. Last year, the screen saver of the Titanic went viral in the world, revealing its charm. Some screen saver programs provide all the multimedia functions such as images, animations, audios, and videos. Although it is easy to get beautiful screen saver, having your own screen saver may be more attractive to users.
Vc5.0/6.0 is a good tool for developing screen saver. Static Link Library SCRNSAVE. Lib provides support for screen saver. SCRNSAVE. LIB contains the main program and default functions for creating the Screen Saver. You can easily program and connect to it using the SDK. Despite the difficulties in SDK programming, it is easier or even easier to write a relatively simple screen saver than to use MFC programming.
You can also use MFC to write Screen Saver programs. Unfortunately, MFC does not support SCRNSAVE. Lib. You must manually complete the functions provided by SCRNSAVE. Lib, Which is troublesome and hard to understand. However, when writing complex screen saver programs, MFC provides much more convenient functions for display module and dialog box processing than SDK. For the sake of simplicity, we will first introduce how to use the SDK to write screen saver.

I. Screen Saver and SCRNSAVE. Lib

The Screen Saver is described as follows from the developer's perspective:
First, the screen saver is a Win32 API that supports a special application and is automatically activated by the system. When the conditions are met, the system sends the wm_syscommand message with the wparam value of SC _screensave to the current activity window, and then the system is executed in the current activity window. the screen saver specified in the [boot] area of the INI file.

The screen saver activation conditions are:
1. No mouse or keyboard input within the specified time.
2. The current active window is a standard Windows application. For non-Windows applications, the message wm_syscommand is ignored.
3. Apparently, if the currently active program takes over the wm_syscommand message whose wparam value is SC _screensave and does not pass it to the defwindowproc function, screen saver can be disabled. This is particularly useful for programs that are unwilling to be interrupted during operation, such as video playback and CD burning.

Secondly, you can select the desired screen saver from the display on the control panel and configure the parameters of the screen saver. The Screen Saver should provide a dialog box for configuring the screen saver.

Again, the screen saver has special output functions, resource definitions, and variable declarations. SCRNSAVE. LIB contains the main program for creating screen saver. When the Screen Saver is started, SCRNSAVE. Lib automatically creates a full screen window and describes the window class as a full black screen without a light mark.
The screen saver you write must contain three basic functions: screensaverproc, screensaverconfiguredialog, and registerdialogclasses, and be connected to SCRNSAVE. Lib.
1. The screensaverproc Window Function processes specific messages and passes unprocessed messages to SCRNSAVE. Lib. Screensaverproc generally processes the following messages:
Wm_create reads the initialization data of. ini or registry, sets the timer and other initialization operations.
Wm_erasebkgnd erase the background to prepare for the next drawing.
Wm_timer for drawing output. You can implement your own animation functions and other operations.
Wm_destroy Delete timer and other objects
Screensaverproc transmits unprocessed messages to the defscreensaverproc function in SCRNSAVE. Lib for processing. It completes many complex and key operations, such as when to activate, when to close, and so on, greatly facilitating the preparation of screen saver.
2. The screensaverconfiguredialog function processes the screen saver configuration dialog box. This dialog box is called by the display setting program of the control panel. User input configuration data is output to. ini or registry.
3. The registerdialogclasses function registers the window class in the screen saver configuration dialog box. If you do not use a special window or control, you can simply return true.

In addition, there are some principles for writing Screen Saver programs.
1. To make the control panel recognizable, the screen saver extension must be changed to. Scr and stored in the Windows directory.
2. the icon of the Screen Saver must be defined as id_app in the resource file. Id_app is defined by SCRNSAVE. H. of the system.
3. The resource file must contain a description string. This string is used to display the name of the screen saver on the control panel. It must be at the top of the string table. SCRNSAVE. H. defines its ID as 1.
4. the ID of the Screen Saver configuration dialog box in the resource file must be dlg_scrnsaveconfigure. It is defined by SCRNSAVE. H. of the system.

Ii. programming example

1. Minimal screen saver. This is a screen saver that only contains the most basic modules and displays a line of active text on the screen during runtime. The procedure is as follows:
Step 1: Create a 32-bit application project in vc5.0/6.0 (do not select MFC wizard exe). Create the following minisaver. cpp file:
# Include
# Include
// The SCRNSAVE. Lib header file
# Include "resource. H"
// Declare three basic functions
Lresult winapi screensaveproc
(Hwnd, uint, wparam, lparam );
Bool winapi screensaveconfiguredialog
(Hwnd, uint, wparam, lparam );
Bool winapi registerdialogclasses (hinstance );
// Define three basic functions
Lresult winapi screensaverproc
(Hwnd, uint message, wparam,
Lparam)
{HDC;
Rect RC;
Static int xpos; // the abscissa of the text
Static char slidetext [] = "Welcome to screen saver! ";
Static uint timerid; // Timer
Switch (Message)
{
Case wm_create:
Xpos = 0;
Timerid = settimer (hwnd, 1,250, null); // set the timer
Break;

Case wm_erasebkgnd:
// Empty operation, handled by defscreensaverproc
Break;
Case wm_timer:
HDC = getdc (hwnd );
// Clear screen
Setrect (& rc, 0, 0, getsystemmetrics (sm_cxscreen ),
Getsystemmetrics (sm_cyscreen ))
Fillrect (HDC, & rc, getstockobject (black_brush ));
// Output text
Settextcolor (HDC, RGB (255 * rand (),
255 * rand (), 255 * rand ()));
Setbkcolor (HDC, RGB (0, 0 ));
Textout (HDC, xpos, getsystemmetrics (sm_cyscreen)/2,
Slidetext, strlen (slidetext ));
// Move the abscissa of the text
Xpos = (xpos + 10) % getsystemmetrics (sm_cxscreen );
Releasedc (hwnd, HDC );
Break;

Case wm_destroy:
Killtimer (hwnd, timerid); // deletes the timer.
Postquitmessage (0 );
Return 0;
}
Return defscreensaverproc
(Hwnd, message, wparam, lparam );
}

Bool winapi screensaverconfiguredialog
(Hwnd, uint message, wparam,
Lparam)
{// The configuration dialog box is not required for the moment. Only false is returned.
Return false;
}

Bool winapi registerdialogclasses
(Hinstance)
{// Not required. Only true is returned.
Return true;
}


Step 2: Define the resource file minisaver. RC. Generate an icon in the VC environment and its ID is id_app. Add the ids_description string "minisaver" to the string table and use it as the first entry in the string table.
Step 3: Compile the connection and rename the generated minisaver. EXE to minisaver. scr plugin to the Windows directory. Note: Before compilation, you must add the SCRNSAVE. Lib Library to the link option in setting of the Project menu.
Step 4: select the screen saver minisaver in the display settings of the control panel. Do not rush to configure the screen saver because the configuration dialog box is not provided by minisaver.

2. relatively complete screen saver mysaver. The screen saver provides the configuration dialog box and dialog box, and uses image display. You can add your favorite images to your resources.
Step 1: Create a 32-bit application project in vc5.0/6.0 (without selecting MFC wizard exe) and define the resource file minisaver. RC.
Generate an icon in the VC environment and its ID is id_app.
Add the ids_description string "minisaver" to the string table and use it as the first entry in the string table. Add ID to idsappname
The string "Screen Saver. mysaver". For more information, see the program below.
Add bitmap with ID idb_bitmap1
Add the ID dlg_about dialog box. There is only a button with the ID of idok
Add id_app as the ID. The ID of this dialog is defined by SCRNSAVE. h and can only be dlg_scrnsaveconfigure. The ID is idc_edit.
The Edit Control. The ID is "idok", "idcancel", and "idabout" buttons.
It is worth noting that the id_app and dlg_scrnsaveconfigure defined by resource. h may conflict with the predefined SCRNSAVE. H value.
Set id_app in resource. h to 100 and dlg_scrnsaveconfigure to 2003.
Step 2: Create the mysaver. cpp file as follows:
# Include

# Include "resource. H"
// Declare three basic functions
Lresult winapi screensaveproc
(Hwnd, uint, wparam, lparam );
Bool winapi screensaveconfiguredialog
(Hwnd, uint, wparam, lparam );
Bool winapi registerdialogclasses (hinstance );
Bool winapi aboutdialog (hwnd, uint, wparam, lparam );
// Define global variables
Char szinifilename [] = "control. ini ";
// The Screen Saver configuration data is stored in the control. ini file.
Char szsection [32];
// The name of the Screen Saver configuration data in the control. INI File Location Area
Char szentry [] = "slide text :";
// Screen Saver configuration data item name
Char slidetext [256];
// Screen Saver configuration data, which is text content
// Define three basic functions
Lresult winapi screensaverproc
(Hwnd, uint message, wparam,
Lparam)
{Static hbitmap hbmp; // bitmap handle
HDC, hmemdc; // hmemdc is a memory device that caches bitmap.
Rect RC;
Static int xpos = 0;
Static uint timerid;
Switch (Message)
{
Case wm_create:
// The File Location Area Name szsection is assigned as a resource.
Idsappname. The hmaininstance is // SCRNSAVE. Lib.
Screen Saver instance handle defined
Loadstring (hmaininstance, idsappname,
Szsection, sizeof (szsection ));
Strcpy (sshortetext, "Welcome to screen saver! ");
// Read the [Screen Saver. mysaver] In the control. ini file.
The configuration data of the partition is sent to slidetext.
Getprivateprofilestring (szsection, szentry,
Slidetext, slidetext,
Sizeof (slidetext), szinifilename );
// Bitmap
Hbmp = loadbitmap (hmaininstance,
Makeintresource (idb_bitmap1 ));
Timerid = settimer (hwnd, 1,250, null );
Break;

Case wm_erasebkgnd:
HDC = getdc (hwnd );
// Display bitmap
Hmemdc = createcompatibledc (HDC );
SelectObject (hmemdc, hbmp );
Setrect (& rc, 0, 0, getsystemmetrics (sm_cxscreen ),
Getsystemmetrics (sm_cyscreen)-25 );
Bitblt (HDC, RC. Top, RC. Left, RC. Right, RC. Bottom,
Hmemdc, RC. Top, RC. Left, srccopy );
// Clear the bottom line to prepare for text display.
Setrect (& rc, 0, getsystemmetrics (sm_cyscreen)-25
Getsystemmetrics (sm_cxscreen ),
Getsystemmetrics (sm_cyscreen ));
Fillrect (HDC, & rc, getstockobject (black_brush ));
Releasedc (hwnd, HDC );
Deletedc (hmemdc );
Return 1;

Case wm_timer:
HDC = getdc (hwnd );
// Clear the bottom row
Setrect (& rc, 0, getsystemmetrics (sm_cyscreen)-25,
Getsystemmetrics (sm_cxscreen ),
Getsystemmetrics (sm_cyscreen ));
Fillrect (HDC, & rc, getstockobject (black_brush ));
// Output text
Settextcolor (HDC, RGB (255 * rand (),
255 * rand (), 255 * rand ()));
Setbkcolor (HDC, RGB (0, 0 ));
Textout (HDC, xpos, getsystemmetrics (sm_cyscreen)-25,
Slidetext, strlen (slidetext ));
Xpos = (xpos + 10) % getsystemmetrics (sm_cxscreen );
Releasedc (hwnd, HDC );
Break;

Case wm_destroy:
Deleteobject (hbmp );
Killtimer (hwnd, timerid );
Postquitmessage (0 );
Return 0;
}
Return defscreensaverproc
(Hwnd, message, wparam, lparam );
}

Bool winapi screensaverconfiguredialog
(Hwnd, uint message, wparam,
Lparam)
{Switch (Message)
{
Case wm_initdialog:
Loadstring (hmaininstance, idsappname,
Szsection, sizeof (szsection ));
Strcpy (sshortetext, "Welcome to screen saver! ");
Getprivateprofilestring (szsection,
Szentry, slidetext,
Slidetext, sizeof (slidetext), szinifilename );
Setdlgitemtext (hwnd, idc_edit, slidetext );
Setfocus (getdlgitem (hwnd, idc_edit ));
Return false;

Case wm_command:
Switch (wparam)
{
Case idok:
// Retrieve the Edit Control text data and write it to the control. ini file.
Getdlgitemtext (hwnd, idc_edit,
Slidetext, sizeof (slidetext ));
Writeprivateprofilestring (szsection,
Szentry, slidetext, szinifilename );
Enddialog (hwnd, true );
Return true;

Case idcancel:
Enddialog (hwnd, false );
Return true;

Case idabout:
// Call the about dialog box
Dialogbox (hmaininstance,
Makeintresource (dlg_about ),
Hwnd, (farproc) aboutdialog );
Return true;
}
Break;
}
Return false;
}

Bool winapi registerdialogclasses
(Hinstance)
{
Return true;
}

Bool winapi aboutdialog
(Hwnd, uint message, wparam,
Lparam)
{
Switch (Message)
{
Case wm_initdialog:
Return true;

Case wm_command:
Switch (wparam)
{
Case idok:
Enddialog (hwnd, true );
Return true;
}
Break;
}
Return false;
}

Finally, compile the connection and rename the generated minisaver. EXE to minisaver. scr plugin to the Windows directory. Now you can use the configuration dialog box.


Author member name: heliking

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.