★ Develop screensaver under Windows with C ++ Builder 5

zhaozj2021-02-16  77

Develop screensaver under Windows with C Builder 5

Develop screensaver under Windows with C Builder 5

E_mail: codehunter@sohu.com

Website: http://codehunter.1YES.NET Abstract: 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. Screen saver

When the program is called, the operating system performs the program with a specific command line. This article organizes and handles all command lines, package

Including "/ p", "/ s", "/ c", "/ a", where "/ p" indicates that the screen saver is displayed in the preview window; "/ s" indicates the truly run

Screenkeeping; "/ c" means calling settings dialog; "/ a" means calling password setting dialog (invalid in WinNT). this

The program can simply implement a full-featured screen saver, you can modify when you run Windows screensaver settings.

Password (invalid in WinNT) can also set the frequency of the image display and saved the frequency value to the registry. Screen saver

Runtime images change the display position with the frequency you set. The author also left a homework to the reader, please see the selection map in Figure 1

Slice folder This project, press the browsing button to set the path to the image, the author has implemented the function of the browsing button and

Save the resulting path to the registry and let the screen save the value of PICDIR, PicDir is equal to the code when "NO"

The author has implemented that the code when PICDIR is not equal to "NO" is implemented by the reader. That is to let the reader realize a PicDir

The picture in the catalog is turned on the screen.

two. Implementation

First introduce several API functions.

WinMain function:

Int Winapi WinMain

Hinstance Hinstance, // Current instance handle

Hinstance hprevinstance, // Previous instance handle

LPSTR LPCMDLINE, / / ​​Pointer to command line parameters (parameters you want to use)

INT ncmdshow // state of the window

);

GetWindowlong Function: Get the function of the specified window information

Long getWindowlong

HWND HWND, // window handle

INT NINDEX / / Specifies the information returned

);

SetWindowlong Function: Change Window Properties

Long setwindowlong

HWND HWND, // window handle

INT NINDEX, / / ​​Specify information about the value to be set

Long dwnewlong // new value

);

SetParent function: Change the parent window of the specified window

HWnd setParent (

HWND HWNDCHILD, / / ​​To change the window handle of the parent form

HWND HWNDNEWPARENT / / The handle of the new parent form

);

GetClientRect function: get the window of the window

Bool getClienceRect

HWND HWND, // window handle

LPRECT LPRECT / / Rect structure

);

SetWindowPos function: Change the size, location, top window of the window, etc.

Bool setWindowPOS

HWND HWND, // window handle

HWND HWNDINSERTAFTER, / / ​​Arrange the Handle of Window Sequence (Z Order)

Int x, // horizontal position

Int Y, // Vertical Position

Int cx, // width

INT CY, / / ​​HEIGHTUINT UFLAGS // Window Location, etc.

);

SystemParametersInfo Function: Access or set system-level parameters

Bool SystemParametersInfo

UINT UIAction, / / ​​Specify system parameters to get or set

UINT UIPARAM, // Depends on action to be taken

Pvoid ​​Pvparam, // Depends on action to be taken

UINT FWININI / / User Profile Whether it changes the tag

);

Showcursor function: Show or hide cursor

Int Showcursor

Bool bshow // mouse visibility mark

);

GetVersion function: Get the version information of the system

DWORD GETVERSION (VOID)

The specific information of the above API functions can find about the MSSDK document. After understanding the basic function, the author is briefly described

Under implementation method.

1. New project, add two forms, named Mainform, frmconfig, respectively,

FRMControl. Add a Timer Control and Timage Control on the MainForm and the FRMControl form.

Set the borderstyle of the two forms to bsnone, the background color is set to black. Add one in the TIMAGE of the two forms

Zhang picture, FRMControl size is set to: high 130 pixels, width 160 pixels, Timage's Stretch property is set

value. FRMCONFIG style is shown in Figure 1.

2. Save the project file as ScreenSaver.cpp, other units are unitmain.cpp, respectively,

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 is normal, you will look

The picture is displayed on the screen with a random location.

figure 1

three. Source code

The following is all the source code for this program, where screensaver.cpp, unitmain.cpp is the core code.

/ * {*****************************} * /

/ * {***** screensaver.cpp ****} * /

/ * {*****************************} * /

/ / -------------------------------------------------------------------------------------------- ---------------------------

#include

#pragma HDRSTOP

Useres ("screensaver.res");

Useform ("Unitmain.cpp", frmmain;

Useform ("Unitconfig.cpp", frmconfig;

UseForm ("UnitControl.cpp", frMControl);

/ / -------------------------------------------------------------------------------------------- ---------------------------

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") starts the screensaver

Application-> Createform (__ classid (tfrmmain), & frmmain

Else IF (StartType == "/ P") // Preview

{

Application-> Createform (__ classid (tfrmControl), & frmControl

Temp = command.substring (3, command.length () - 2); // Get the screensaver preview window in the command line

String form of the mouth

Cpwindow = (long *) temp.toint (); // Force converting the string of the preview window handle to long shape

pointer

RECT * LOOKRECT; / / Create a Rect structure pointer

Long style = getWindowlong (Application-> Mainform-> Handle, GWL_Style); //

Take the style of the FRMControl window

STYLE = style | WS_CHILD;

SetWindowlong (Application-> Mainform-> Handle, GWL_Style, Style); // Settings Window

Sub-window

SetParent (Application-> Mainform-> Handle, CPWindow); // Setting the Screensaver Preview window is

Father Window of FrMControl

GetClientRect (CPWindow, LookRect); // Get the client area of ​​the screensaver preview window

SetWindowPos (Application-> Mainform-> Handle, HWND_TOP, 0, 0, LookRect-> Right, LookRect-> Bottom, SW

P_nozorder | swp_noactivate | swp_showwindow; // Overwrite the Window of FRMControl overlay the screen save preview window

Customer area and display it

}

Else if (StartType == "/ A") // Start password setting window

{

Temp = Command.Substring (3, Command.length () - 2);

Cpwindow = (long *) TEMP.TOINT ();

// The following is the process of dynamically calling the PWDChangePassworda function in mpr.dll

Typedef uint (Callback * Fun (LPSTR, HWND, UINT, UINT);

Hinstance HDLL = LoadLibrary ("mpr.dll");

Fun myfun;

IF (HDLL! = NULL)

{

GetProcaddress (HDLL, "PWDChangePassworda);

IF (! myfun) Freelibrary (HDLL);

Else

MYFUN ("Scrsave", CPWindow, 0, 0); // Function call

}

}

Application-> Run ();

}

Catch (Exception & Exception)

{

Application-> Showexception (& Exception);

Return 0;

}

/ / -------------------------------------------------------------------------------------------- ---------------------------

/ * {*****************************} * /

/ * {***** Unitmain.h ****} * /

/ * {*****************************} * /

/ / -------------------------------------------------------------------------------------------- ---------------------------

#ifndef unitmainh

#define unitmainh

/ / -------------------------------------------------------------------------------------------- ---------------------------

#include

#include

#include

#include

#include

#include

#include

/ / -------------------------------------------------------------------------------------------- --------------------------- Class Tfrmmain: Public TForm {__published: // IDE-management 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 __fastcall 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);}; / / -------------------------------------------------------------------------------------------- ------------------ --------- Extern package tfrmmain * frmmain; // ------------------------------- ------------------------------------------ #ENDIF / / / ---- -------------------------------------------------- --------------------- / * {********************************** *****} * / / * {***** Unitmain.cpp ****} * / / * {***************************** *********} * / // ----------------------------------- ---------------------------------------- # cd # include

#include "unitmain.h" #include

/ / -------------------------------------------------------------------------------------------- --------------------------- #pragma package (smart_init) #pragma resource "* .dfm" tfrmmain * frmmain; // --- -------------------------------------------------- ---------------------- __fastcall tfrmmain :: tfrmmain: TFORM (Owner) {} // ------- -------------------------------------------------- ---------------- void __fastcall tfrmmain :: formcall (TOBJECT * Sender) {// makes the window becomes the top window setWindowPos (this-> handle, hwnd_topmost, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); // When the window coverage screen this-> width = screen-> width; this-> height = screen-> height; this-> top = 0; this-> left = 0; version = GetVersion (); TRegistry * Registry = new TRegistry; try {if (Version> 0x80000000) {Registry-> RootKey = HKEY_CURRENT_USER; Registry-> OpenKey ( "// Control Panel // Desktop", false); PWProtect = Registry- > ReadInteger ( "ScreenSaveUsePassword"); // Check whether password protection Registry-> CloseKey ();} Registry-> RootKey = HKEY_CURRENT_USER; Registry-> OpenKey ( "// Software // CODEHUNTER", true); picdir = Registry- > ReadStrin g ("picdir"); // Get picture directory frequence = registry-> readinteger ("frequence"); // Get image display frequency if (picdir == ") PicDir =" no "; if (frequence <0 || FREQUENCE> 6) frequence = 2; Timer1-> Interval = 1000 * frequence; set timer} __finally {delete registry; picdir = "no";} // Detection Whether running on 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); // Disappear the cursor for while (! showcursor (false) < -5);

} // ----------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------- void __fastcall tfrmmain :: formkeydown (Tobject * Sender, Word & Key, TshiftState Shift) {this-> close ();} // -------------------------------------------- ------------------------------- void __fastcall tfrmmain :: FormMousedown (Tobject * Sender, TMousebutton Button, TshiftState Shift, Int X , int y) {this-> close ();} // ----------------------------------- ---------------------------------------- Void __fastcall tfrmmain :: FormClosequery (TOBJECT * SENDER , Bool & CANClose) {if (PWProtect && Version> 0x80000000) {bool passchck; // Display cursor, and call the password dialog while (! showcursor (true)> 5); // below is the dynamic call of the VerifyScreensavePWD function Typedef uint Callback * fun) (Hinstance HDLL = LoadLibrary ("password.cpl"); fun myfun; if (hdll! = Null) {MyFun = (fun) getProcaddress (HDLL, "VerifyScreensavePwd"); if (! Myfun) Freeelibrary (hdll); else passchck = myfun (this-> handle);} if (passchck == false) {while (! Showcursor (false) <-5; canclose = false;}}} // ---- ------------ -------------------------------------------------- ------------- void __fastcall TFrmmain :: FormClose (TObject * Sender, TCloseAction & Action) {while (ShowCursor (True)> 5!); if (PWProtect && Version> 0x80000000) SystemParametersInfo (SPI_SCREENSAVERRUNNING, 0 , 0, 0); // Exit screen saver status} // ------------------------------------ --------------------------------------- void __fastcall tfrmmain :: image1mousedown (Tobject * Sender, TMouseButton Button, TshiftState Shift, Int X, Int Y) {this-> close ();

} // ----------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------- 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;}} // --------------- -------------------------------------------------- -------- Void __fastcall tfrmmain :: Timer1Timer (TOBJECT * Sender) {IF (picdir == "no") {INT i; randomize (); i = rand ()% 2; if (i == 0) i = -1; Else i = 1; image1-> TOP = i * (rand ()% this-> height); image1-> left = i * (rand ()% this-> width);}} / / -------------------------------------------------------------------------------------------- --------------------------- / * {******************** ***********} * / / * {***** UnitControl.h ****} * / / / * {************** ****************} * / // ----------------------------- -------------------------------------------- # #FNDEF UnitControlh # Define UnitControlH // ---------------------------------------------- ---------------------------- # include # include

#include

#include

#include

#include

#include

/ / -------------------------------------------------------------------------------------------- --------------------------- Class TfRmControl: public tform {__published: // de-managed components TIMAGE * Image1; TTIMER * TIMER1; VOID __fastcall Timer1Timer (TObject * Sender); void __fastcall FormCreate (TObject * Sender); private: // User declarations public: // User declarations __fastcall TFrmControl (TComponent * Owner);}; // --------- -------------------------------------------------- --------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------- Endif // ----------------------------------------------- ---------------------------- / * {******************* ************} * / / * {***** UnitControl.cpp ****} * / / * {************** *****************} * / // ---------------------------- ----------------------------------------------- #include

#pragma hdrstop #include "unitControl.h" // -------------------------------------- ------------------------------------- #pragma package (smart_init) #pragma resource "* .dfm "TfRMControl * frmControl; // ------------------------------------------- -------------------------------- __fastcall tfrmcontrol :: tfrmcontrol (tcomponent * oowner): tform (oower) {} / / --------------------------------------------------- -------------------------- Void __fastcall tfrmcontrol :: Timer1Timer () {INT I; Randomize (); i = rand () % 2; if (i == 0) i = -1; else i = 1; image1-> TOP = i * (rand ()% this-> height; image1-> left = i * (rand ()% This-> width;} // ----------------------------------------- ---------------------------------- Void __fastcall tfrmcontrol :: formcreate (TOBJECT * Sender) {image1-> TOP = 0; image1-> left = 0; image1-> height = this-> height; image1-> width = this-> width;} // --------------- -------------------------------------------------- ------- / * {******************************************} * / / * {** *** Unitconfig.h ****} * / / / * {** ****************************} * / // ---------------- -------------------------------------------------- --------- #ifndef UnitConfigh #define unitconfigh // --------------------------------- ------------------------------------------ # include # include

#include

#include

#include

#include

#include

#include

/ / -------------------------------------------------------------------------------------------- --------------------------- Class TfRmconfig: public tform {__published: // 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 frequence; public: // user declarations __fastcall tfrmconfig (tComponent * Owner);}; // --------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------- Extern package tfrmconfig * frmconfig; // ---------------------------------------------------------------------------------------------------------------------- --------------------------------- #ENDIF / / ------------- -------------------------------------------------- ------------ / * {******************************} * / / * {***** Unitconfig.cpp ****} * / / * {*********************************************** *} * / // -------------------------------------------- ------------------------------ - #include

#pragma hdrstop #include "unitconfig.h" // -------------------------------------- ------------------------------------- #pragma package (smart_init) #pragma resource "* .dfm "Tfrmconfig * frmconfig; // ------------------------------------------- -------------------------------- __fastcall tfrmconfig :: tfrmconfig (tformponent * oowner): TFORM (OWNER) {} / / --------------------------------------------------- -------------------------- Void __fastcall tfrmconfig :: Button1click (TOBJECT * Sender) {= ("SELECT PICTURE DIR", " , PicDir)) Panel2-> Caption = Picdir;} // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------------------- void __fastcall tfrmconfig :: Button2click (Tobject * Sender) {// Write the information to the registry if (picdir == ") PicDir =" no "; this-> frequence = trackbar1-> position; Tregistry * reg = new Tregistry; try {reg-> rootkey = hkey_current_user; if (REG-> OpenKey ("// Software // Codehunter") {REG-> Writestring ("PicDir", Picdir); REG-> Writeinteger ("frequence", frequence); reg-> closekey (); }} __fi Nally {delete reg;} this-> close ();} // ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ----------------------------------------- Sourcecodes download (

http://codehunter.1yes.net/download/luCysaver.zip

)

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

New Post(0)