E_mail: codehunter@sohu.com
Summary: This article demonstrates the implementation process of screen saver under Windows through a specific program. One. Introduction The screen saver under the window system is an application based on Command Line. The operating system performs the program with the specific command line when the screen saver is called. This article organizes and processes all command lines, including "/ p", "/ s", "/ c", "/ a", where "/ p" indicates that the screen saver is displayed in the preview window; "/ s" means Really run screen saver; "/ c" indicates call settings dialog; "/ a" means calling the Password Settings dialog (invalid in WinNT). This program makes a full-featured screen saver as a full-featured screen saver, you can modify the password (invalid in Winnt) and set the frequency of the image and save the frequency value to the registry. When the screen is running, the image changes the display position with the frequency you set. The author also left a homework to the reader. Please see the item in Figure 1, press the Browse button to set the path path, the author has implemented the function of the browsing button and saves the resulting path to the registry In, and let the screensaver start on the value of Picdir, the code author of Picdir is equal to "NO" is implemented, and the PICDIR does not equal the code when "NO" is implemented by the reader. That is to let the reader implements a screensaver program that can display the picture in the PICDIR directory.
two. The implementation method first introduces several API functions. Winmain function: int WINSTANCE WINMAIN (Hinstance Hinstance, // Current instance handle Hinstance HPREVINSTANCE, / / Previous instance handle LPSTR LPCMDLINE, / / Pointer to command line parameters (parameter to be used by this program) int ncmdshow // window status GetWindowlong Function: Get the function of the specified window information long getWindowlong (HWND HWND, File: // Specify the return information); setWindowlong function: Change window properties long setwindowlong (hwnd hwnd, file: / / Window handle INT NINDEX, / / Specify the message for the value to be set for the value of the value); setParent function: Change the parent window hWnd setParent (hwnd hwndchild, file: // to change the window of the parent form) Handle HWND HWNDNEWParent file: // The handle of the new parent window); getClientRect function: Get the window of the customer area Bool getClientRectRect (hwnd hwnd, // window handle LPRECT LPRECT FILE: // RECT structure); setWindowPos function: change Window size, location, top window, etc. BOOL SETWINDOWPOS (HWND HWND, // Window Handle HWND HWNDINSERTAFTER, / / Arranged Handle (Z Order) INT X, // Horizontal Position Int Y, // Vertical Position Int CX, // Width int CY, // Height Uint UFLAGS // Window Location Mark); SystemParametersInfo Function: Access or Set System-Level Parameters Bool SystemParametersInfo (uint uiaction , // Specify whether the system parameter uIparam, // depends on action to be taken pvoid pvparam, // depends on action to be taken uint fwini // user profile changes the tag); ShowCursor function: Showcursor function: display or Hide Cursor Int Showcursor (Bool Bshow // Mouse Scale); getVersion function: Get system version information DWORD GETVERSION (VOID)
The specific information of the above API functions can find about the MSSDK document. Learn about the basic function, the author is a brief introduction. 1. New project, add two forms, named Mainform, frmconfig, frmControl, respectively. Add a Timer control and TIMAGE control on the mainform and frmcontrol form, set the borderstyle of the two forms to BSnone, the background color is set to black. Add a picture on the TIMAGE of the two forms, the FRMControl size is set to: high 130 pixels, width 160 pixels, and TIMAGE STRETCH attributes are set to true value. The style of frmconfig is shown in Figure 1.2. The save project file is ScreenSaver.cpp, and other units are set as UnitMain.cpp, UnitControl.cpp, unitconfig.cpp. 3. Write the code, see the source program of the third part. 4. Compiled into an executable and change the file extension to SCR. 5. Finally, you can test the screen saver to the Windows directory. If everything you will see, you will see the picture on the screen with a random location. Figure 1. Source code The following is all source code for this program, where screensaver.cpp, unitmain.cpp is the core code.
/ * {**************************************} * // * {***** screensaver.cpp ** **} * // * {*****************************} * / file: // ---- -------------------------------------------------- --------------------- # include #pragma hdrstopuseres ("screensaver.res"); useform ("UnitMain.cpp", frmmain; useform ("UnitConfig. CPP ", frmconfig; useform (" UnitControl.cpp ", frMControl); File: // ----------------------------- -------------------------------------------- WinAPI WinMain (Hinstance Hinstance, LPSTR P, INT // "P" is a pointer to command line parameters {String StartType; Ansistring Command = P, Temp; hwnd cpwindow = null; if (command == ") StartType =" / c " Else StartType = command.substring (1, 2); // Get the first two parameters of the command line try {Application-> Initialize (); if (startType == "/ c") // Start setting window Application-> Createform (__ classid (tfrmconfig), & frMConfig; Else if (startType == "/ s) Startup screen save Application-> Createform; else if (starttype ==" / p ") // Preview {Application-> Createform (__ classid (tfrmControl), & frMControl); temp = command.substring (3, command.length () - 2); // Get the screen save preview window handle of the screen handle Form cpwindow = (long *) temp.toint (); // forced to convert the string of the preview window handle to a long shape pointer
RECT * lookrect; // establish a RECT structure pointer Long style = GetWindowLong (Application-> MainForm-> Handle, GWL_STYLE); // Gets the window style FrmControl style = style | WS_CHILD; SetWindowLong (Application-> MainForm-> Handle, GWL_Style, Style); // Settings the window to sub-window setParent (Application-> Mainform-> Handle, CPWindow); // Set the screen save preview window getClientRect (cpwindow, lookRect); // Get the screen save preview window Customer SETWINDOWPOS (Application-> Mainform-> Handle, HWND_TOP, 0, 0, LookRect-> Right, LookRect-> Bottom, SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWINDOW); // Overwide FRMControl's window overview the client area of the screen save preview window, and Display it} else if (startType == "/ a") // Start password setting window {TEMP = Command.Substring (3, command.length () - 2); cpwindow = (long *) TEMP.TOINT (); File: // The following is the process of dynamically calling the PWDEF uint (Callback * Fun) (LPSTR, HWND, UINT, UINT); Hinstance HDLL = LoadLibrary ("MPR.dll) "); Fun myfun; if (hdll! = Null) {Myfun = (fun) getProcaddress (HDLL," PWDChangePassworda); if (! Myfun) Freelibrary (HDLL); Else Myfun ("Scrsave", Cpwindow, 0, 0 ); // function call}} Application-> run ();} catch (exception & exception) {Application-> showexception (& Exception);
File: // --------------------------------------------- ----------------------------- / * {**************** **************} * // * {***** Unitmain.h ****} * // * {************ *******************} * / file: // ------------------------ -------------------------------------------------- - # iFNDef Unitmainh # Define unitmainhfile: // ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------------------------------- # Include #include #include #include #include #include #include file : // ----------------------------------------------- ---------------------------- Class TfrmMain: Public TForm {__ Published: // Ide-Managed Components TTIMER * TIMER1; TIMAGE * image1 void __fastcall FormCreate (TObject * Sender); void __fastcall FormKeyDown (TObject * Sender, WORD & Key, TShiftState Shift); void __fastcall FormMouseDown (TObject * Sender, TMouseButton Button, TShiftState Shift, int X, int Y); void __fastcall FormCloseQuery (TObject * Sender, bool & canclose; void __fast call FormClose (TObject * Sender, TCloseAction & Action); void __fastcall Image1MouseDown (TObject * Sender, TMouseButton Button, TShiftState Shift, int X, int Y); void __fastcall Image1MouseMove (TObject * Sender, TShiftState Shift, int X, int Y); void __fastcall Timer1Timer (TObject * Sender); private: // User declarations DWORD PWProtect; DWORD Version; String picdir; int frequence; public: // User declarations __fastcall TFrmmain (TComponent * Owner);}; file: // --- -------------------------------------------------- ---------------------- Extern package tfrmmain * frmmain
File: // ---------------------------------------------- ----------------------------- # endiff: // ---------------- -------------------------------------------------- --------- / * {******************************} * // * {* **** Unitmain.cpp ****} * // * {******************************} * / file: // --------------------------------------------- ------------------------------ # include #pragma hdrstop # include #include "Unitmain.h" #include file: // -------------------------------------------------- ------------------------- # pragma package (smart_init) #pragma resource "* .dfm" TFRMMAIN * frmmain; file: // --- -------------------------------------------------- ----------------------__ fastcall tfrmmain :: tfrmmain: TFORM (OWNER) {} file: // ------- -------------------------------------------------- ------------------ Void __fastcall tfrmmain :: formcreate (TOBJECT * Sender) {file: // makes the window becomes the top window setWindowPos (this-> handle, hwnd_topmost, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); File: // When the window coverage screen this-> width = SC Reen-> width; this-> height = screen-> height; this-> TOP = 0; this-> left = 0; version = getVersion (); Tregistry * registry = new Tregistry; try {ix (version> 0x80000000) {Registry-> RootKey = HKEY_CURRENT_USER; Registry-> OpenKey ( "// Control Panel // Desktop", false); PWProtect = Registry-> ReadInteger ( "ScreenSaveUsePassword"); // detecting whether password protection Registry-> CloseKey () ;} Registry-> RootKey = HKEY_CURRENT_USER; Registry-> OpenKey ( "// Software // CODEHUNTER", true); picdir = Registry-> ReadString ( "PicDir"); // get the picture directory frequence = Registry-> ReadInteger ( "frequence"); // Get the frequency IF displayed by the image (PicDir ==
"") picdir = "no"; if (frequence <0 || frequence> 6) frequence = 2; timer1-> interval = 1000 * frequence; set timer} __finally {delete registry; picdir = "no";} file : // Test if it runs in NT if (Version! = 0) if (PWPROTECT & VERSION> 0x80000000) // If the system requires password protection and this system is non-NT, set the system to screen saver status SystemParametersInfo (spi_screensaverrunning, 1, 0, 0); file: // disappears the cursor for while (! Showcursor (false) <-5);} file: // ----------------------- -------------------------------------------------- --void __fastcall tfrmmain :: formydown (TOBJECT * Sender, Word & Key, TshiftState Shift) {this-> close ();} file: // ------------------------------------------------------------------------------------------------------------------ -------------------------------------------------- ------- void __fastcall tfrmmain :: FormMouseDown (Tobject * Sender, TMousebutton Button, TshiftState Shift, Int x, int y) {this-> close ();} file: // ------- -------------------------------------------------- ------------------ Void __fastcall tfrmmain :: formclosequery (TOBJECT * Sender, Bool & CANCLOSE) {IF (PWPROTECT && Version> 0x80000000) {BOOL PasschCK File: // Display the cursor and call the password dialog while (! Showcursor (true)> 5); file: // The following is the dynamic call of the VerifyScreensavePwd function TypeDef uint (hwnd); hinstance hdll = loadingLibrary ("password.cpl"); Fun myfun; if (hdll! = null) {myfun = (fun) getProcaddress (HDLL, "VerifyScreensavePwd"); if (! myfun) Freelibrary (HDLL); Else Passchck = myfun (this- > Handle);} if (passchck == false) {While (! Showcursor (false) <-5;
CANCLOSE = false;}}} file: // --------------------------------------- ------------------------------------
void __fastcall TFrmmain :: FormClose (TObject * Sender, TCloseAction & Action) {while (ShowCursor (True)> 5!); if (PWProtect && Version> 0x80000000) SystemParametersInfo (SPI_SCREENSAVERRUNNING, 0, 0, 0); // exit is blanked} file : // ----------------------------------------------- ---------------------------- Void__fastcall tfrmmain :: image1mousedown (Tobject * Sender, TMousebutton Button, TshiftState Shift, Int x, int y ) {this-> close ();} file: // ------------------------------------ --------------------------------------- void __fastcall tfrmmain :: image1mousemove (TOBJECT * SENDER, TshiftState Shift, INT X, INT Y) {static int mousemoves = 0; mousemoves = mousemoves 1; if (mousemoves> 4) {this-> close (); mousemoves = 0;}} file: // ------ -------------------------------------------------- --------------------- void __fastcall tfrmmain :: Timer1Timer (Tobject * sender) {IF (picdir == "no") {INT i; randomize (); i = rand ()% 2; if (i == 0) i = -1; else = 1; image1-> TOP = i * (rand ()% this-> height); image1-> left = i * ( Rand ()% this-> width;}} file: // ----------------------------------- ---------------------------------------- / * {******* ************************} * // * {***** UnitControl.h ****} * // * {** ****************************} * / file: // -------------- -------------------------------------------------- ----------- # iFNDef UnitControlh # Define UnitControlhfile: // ------------------------------------------------------------------------------------------------ -------------------------------------------- # include #include #include # Include #include #include #include #include file: // ------------------------------------- -------------------------------------- Class TfRMControl: public tform {__ Published: // IDE- Managed Components TIMAGE * image1;
TTimer * Timer1; void __fastcall Timer1Timer (TObject * Sender); void __fastcall FormCreate (TObject * Sender); private: // User declarationspublic: // User declarations __fastcall TFrmControl (TComponent * Owner);}; file: // --- -------------------------------------------------- ---------------------- EXTERN PACKAGE TFRMControl * frmControl; file: // ------------------ -------------------------------------------------- ------- # endiffile: // ---------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------- / * {********** ********************} * // * {***** UnitControl.cpp ****} * // * {***** *************************} * / file: // ----------------- -------------------------------------------------- -------- # Include #pragma hdrstop # include "UnitControl.h" file: // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------- Pragma package (smart_init) #pragma resource "* .dfm" TFRMControl * frmControl; File: // ------------------------------------------------------------- ---------------------------------------------__ f AstCall TfRMControl :: TFRMControl (TComponent * Owner): TFORM (OWNER) {} file: // ----------------------------- -------------------------------------------- void __fastcall tfrmControl: : Timer1Timer (TOBJECT * Sender) {INT i; randomize (); i = rand ()% 2; if (i == 0) i = -1; else = 1; Image1-> TOP = i * (rand () % this-> height); image1-> left = i * (rand ()% this-> width);} file: // ---------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------- ----- void __fastcall tfrmControl :: formcall (tobject * sender) {image1-> top = 0; image1-> left = 0; image1-> height = this-> height; image1-> width = this-> width ;
File: // --------------------------------------------- ----------------------------- / * {**************** **************} * // * {***** Unitconfig.h ****} * // * {************ *******************} * / file: // ------------------------ -------------------------------------------------- - # iFNDef UnitConfigh # Define Unitconfighfile: // ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ----------------------------------- # Include #include #include #include #include #include #include #include #include #include #include #include #include #includ Include file: // --------------------------------------------- --------------------------------------------- Public TFRMCONFIG: PUBLIC TFORM {__ Publish: // Ide-Managed Components TPANEL * PANEL1; TBUTTON * button1; TPanel * Panel2; TLabel * Label1; TTrackBar * TrackBar1; TLabel * Label2; TLabel * Label3; TLabel * Label4; TButton * Button2; void __fastcall Button1Click (TObject * Sender); void __fastcall Button2Click (TObject * Sender); private: // user declarations ansistring picdir; int 4 Quence; public: // user declarations __fastcall tfrmconfig (tComponent * Owner);}; file: // ---------------------------- ---------------------------------------------- Extern package tfrmconfig * FrMconfig; file: // ------------------------------------------- -------------------------------- # endiffile: // ------------- -------------------------------------------------- ------------ / * {****************************************} * // * {***** Unitconfig.cpp ****} * // * {********************************************** *} * / file: // ------------------------------------------ -------------------------------- # include #pragma hdrstop # include "unitconfig.h" file: // -------------------------------------------------- ------------------------ # prgma package (smart_init) #pragma resource