Deepen shallow screen saver programming

zhaozj2021-02-16  116

In layman's language screen saver program Author: Dong Xiao Published: 2001/03/28 Abstract: V program screen now more and more beautiful, more powerful, can provide all the multimedia features images, animation, audio, video, etc. . Although we can easily get a beautiful screen saver, it is more attractive to the user to make a screen saver. The following is a detailed introduction to how to make a screen saver.

-------------------------------------------------- ------------------------------ Text:

Deepen shallow screen saver programming

The reader is quite familiar with the screen saver. Last year, the Titanic screen protector has a worldwide, and it is very charm. Some screen saver features are very powerful, and all multimedia features such as images, animations, audio, video, etc. Although it is easy to get a beautiful screen saver, you have your own screen saver to more attractive users. VC5.0 / 6.0 is a good tool for developing a screen saver. Static Link Library Scrnsave.LIB provides support for screen saver. Scrnsave.lib contains the main program and default functions of the screen saver, and users can easily program and connect with SDK. Although SDK programming is more than MFC, writing relatively simple screen saver is quite easy to easily programming with MFC. You can also write a screen saver using the MFC. But unfortunately, the MFC does not support Scrnsave.Lib, and must be manually completed by the functions provided by Scrnsave.lib, which is more troublesome and not understood. However, when writing a complex screen saver, the MFC can provide a more convenient than SDK for display modules and dialog processing. To simply introduce how to write a screen saver using the SDK.

I. Screen Saver and Scrnsave.lib

First, from the developer's angle to the screen saver as follows: First, the screen saver is Win32 API supports a special application and is automatically activated by the system. Its mechanism is when the condition is satisfied, the system issues a word parameter wm_sysCommand message to the current active window, and then executes the screen saver specified in the [Boot] area in the System.ini file by the current active window.

The condition of the screen saver activation is 1. There is no mouse or keyboard input within the specified time. 2. The current active window is a standard Windows application. Because non-Windows applications, WM_SYSCOMMAND messages will not be ignored. 3. Obviously, if the currently active program takes over the word parameter WPARAM value for the WM_SYSCOMMAND message of SC_SCREENSAVE and does not pass to the DefWindowProc function, the screen saver can be disabled. This is especially useful for programs that are unwilling to be interrupted in some runs, such as video playbacks.

Second, you can select the required screen saver in the monitor of the control panel and configure the parameters of the screen saver. The screen saver should provide a dialog 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 establishing a screen saver. ScRnsave.lib automatically creates a full screen window when the screen saver starts, and describes the full-scale screen of the window class-free. The screen saver written by the user must contain three basic functions ScreenSaverProc, ScreensaverConfiguredialog, and RegisterDialogClasses and connect to the Scrnsave.lib. 1. The ScreenSaverProc window function processes a specific message and passes the unprocessed messages to Scrnsave.LIB. ScreenSaverProc typically manages the following message: WM_CREATE reads the initialization data of the .ini or the registry, set the timer and other initialization operations. The WM_RASEBKGND erase background is prepared for the next step. WM_TIMER performs drawing output. Users can implement their own animation features and other operations. WM_DESTROY Delete Timer and other object ScreenSaverProc passes unprocessed messages to the DefScreensaverProc function processing in Scrnsave.lib. It is greatly facilitating the screen saver writing by which many complex and critical operations are completed. 2. ScreensaverConfigureDialog Function Processing Screen Salad Program Configuration dialog. The dialog is called by the display setup program of the control panel. The configuration data entered by the user is output to .ini or registry. 3. RegisterDialogClasses Function Registration Screen Sword Configuration dialog window class. If you don't use a special window or control, you can simply return True. In addition, there are some principles written in the screen saver. 1. To enable the control panel to identify, the extension of the screen saver must be changed to .scr and store it in a Windows directory. 2. The icon of the screen saver (icon) must be defined as ID_app in the resource file. ID_APP is defined by the system's scRNSAVE.H. 3. A description string must be included in the resource file. This string is used to control the name of the screen saver. It must be in the first place of the string table. Scrnsave.h. Define its ID to 1. 4. The ID of the screen saver configuration dialog in the resource file must be DLG_ScrnsaveConfigure. It is defined by the system's scRNSAVE.H.

2. Programming Example

1. The smallest screen saver minisaver. This is a screen saver that contains only the most basic module, and the text is displayed on the screen. The steps are as follows: The first step is established in VC5.0 / 6.0 (not Mfc Wizard Exe). Create the following Minisaver.cpp file: #include # include // scRnsave.lib's header file #include " resource.h "// declaration of 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 HWND, UINT Message, WPARAM WPARAM, LPARAM LPARAM) {HDC HDC; Rect RC; Static Int Xpos; // Text SlideTexT [] = "Welcome to the screen saver!" Static uint timerid; // timer switch (message) {case wm_create: xpos = 0; timerid = setTimer (hWnd, 1, 250, null); // Set timer Break; Case WM_RASEBKGND: // Air operation, DEFSCREENSAVERPROC processing 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, slodetext, strlen (slidext)); // Mobile text abscissions XPOS = (XPOS 10)% GETSYSTEM Metrics (SM_CXSCREEN); ReleaseDC (HWND, HDC); Break;

Case WM_DESTROY: KILLTIMER (HWND, Timerid); // Remove Timer PostquitMessage (0); Return 0;} Return DefScreensaverProc (Hwnd, Message, WPARAM, LPARAM);}

Bool WinApi ScreensaverConfigureDialog (HWND HWND, UINT MESSAGE, WPARAM WPARAM, LPARAM LPARAM) {// No configuration dialog is required to return Falsereturn False;}

Bool WinApi RegisterDialogclasses (Hinstance Hinstance) {// Neaver need, only return true return true;}

Step 2, define the resource file minisaver.rc. Generate an icon in the VC environment, its ID is ID_APP. Add the ID to the string table "minisaver", and as a string table first step, compile the connection and change the generated minisaver.exe to MINISAVER.SCR to copy to the Windows directory. Note that before compiling, you must add the Scrnsave.lib library in the LINK option in the setting of the Project menu. In the fourth step, select the screen saver minisaver in the monitor settings of the control panel. Note Don't rush to configure the screen saver because Minisaver has no configuration dialog box. 2. More complete screen saver mysaver. The screen saver provides a configuration dialog and a dialog, and an image display is used. Readers can add their favorite images to resources. The first step is established in VC5.0 / 6.0 (not selecting MFC Wizard EXE) and defines the resource file minisaver.rc. Generate an icon in the VC environment, its ID is ID_APP. Add the ID to the string table "Minisaver" and as a string table as a string table. Add ID to IDSAppname string "Screen Saver.Mysaver", explain the rear program. Add ID to IDB_bitmap1 to join the ID to the DLG_About dialog box. Only one ID is an IDOK button to join ID_APP. The ID of this dialog is defined by scRnsave.h and can only be DLG_ScrnsaveConfigure. Among them, IDC_EDIT's edit control, ID is IDOK, IDCANCEL, and Idabout's buttons. It is worth noting that the ID_APP and DLG_SCRNSAVECONFIGURE defined by resource.h may conflict with Scrnsave.h predefined values, which can be manually set to 100 in resource.h, and DLG_ScrnsaveConfigure is set to 2003. Step 2, establish the following mysaver.cpp file: #include #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); // Defines global variable char szinifilename [] = "control.ini"; // screen saver configuration data stored in Control.ini file char szsection [32]; // screen protection Program configuration data in the Control.ini file location area name char szentry [] = "slide text:"; // screen saver configuration data item name char slidext [256]; // Screen saver configuration data, here is text content / / Define three basic functions LRESULT WINAPI ScreensaverProc (HWND HWND, UINT Message, WParam WParam, LParam Lparam) {static hbitmap hbmp; // bitmap handle HDC HDC, HMEMDC; // HMEMDC is a memory device, cache bitmap. Rect rc; static int xpos = 0; static uint timerid; switch (message) {copy wm_create: // file location area name Szsection assignment is resource IDSAPNAME. Among them, hmainintance (HmainInstance, IDSAPNAME, SZSECTION); STRCPY (SlideText, "Welcome screen saver!"); // read the control.ini file in SLIDETEX in [Screen Saver.MySaver] configuration data area to SlideTextGetPrivateProfileString (szSection, szEntry, SlideText, SlideText, sizeof (SlideText), szIniFileName); // the bit map hBmp = LoadBitmap (hMainInstance, MAKEINTRESOURCE (IDB_BITMAP1)); timerID = SetTimer (hWnd, 1,250, NULL); break; case WM_ERASEBKGND: hDC = GetDC (hWnd); // display a 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, SRCCPY); // Clear the bottom line, Text display is prepared. SetRect (& rc, 0, GetSystemMetrics (SM_CYSCREEN) -25GetSystemMetrics (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 hWnd, UINT message, WPARAM wParam, LPARAM lParam) {switch (message) {case WM_INITDIALOG: LoadString (hMainInstance, idsAPPName, szSection, sizeof (szSection)); strcpy (SlideText, "Welcome 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: // EDIT take control text data file and write control.ini 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: // Calls the About dialog DialogBox (HmainInstance, MakeintResource (DLG_ABOUT), HWND, (FARPROC) Aboutdialog; return;} Break;} Return False;}

Bool WinApi RegisterDialogclasses (Hinstance Hinstance) {Return True;}

Bool WinApi Aboutdialog (HWND HWND, UINT MESSAGE, WPARAM WPARAM, LPARAM LPARAM) {Switch {Case WM_INITDIALOG: RETURN TRUE

Case WM_Command: Switch (wparam) {Case IDok: enddialog (hwnd, true);} Break;} Return False;

Finally, compile the connection and rename the generated minisaver.exe to MINISAVER.SCR to copy to the Windows directory. You can use the configuration dialog in this point. Author member name: Heliking

转载请注明原文地址:https://www.9cbs.com/read-8427.html

New Post(0)