How to use C ++ Builder 5.0 to implement the development of screen saver

zhaozj2021-02-17  75

In the Windows system, there are many screen savers, such as "three-dimensional text", "three-dimensional variation", and some application software specially produced. If you can make a screen saver with your own personality, how much it should be. In fact, users can develop a screen saver with their own style, how good is it, how is the screen saver work, how is it true? .

Principle: The file name of the screen saver is the SCR file, in fact, a standard .exe file, he has two components: protection program execution

Interface, protection program setting interface. At the same time, it is possible to prevent the screen saver repeated execution and screen refresh brings a problem screen saver to handle several messages for Windows. Finally, in order to enable the protection program to operate according to the parameters set by the user, the registry technique is used to save the user's setup information.

Now use a screen saver as an example to introduce as follows:

1. Create an engineering file Project1 containing two forms Form1 and Form2 in C Builder 5.0. Its FORM1 is used to set the running parameters, and the FORM2 is used to perform the screen saver.

Add the following controls in Form1:

Name CAPTION

Label1 running speed

Button1 is determined

Button2 cancellation

Edit1

Add the following controls in Form2

Name Property Value

Timer1 Interval 10

2, add the following code in Unit1.h:

#include "registr.hpp"

Private:

Tregistry MyReg;

Tregistry MyReg;

3. Add the following code in the TFORM1 onshow event:

Void __fastcall tform1 :: form1show (TOBJECT * SENDER)

{

MyReg = New Tregistry;

MyReg-> rootkey = hkey_local_machine;

MyReg-> OpenKey ("Software // MyCompany // Remember", true);

Edit1-> text = myreg-> readinteger ("interval"); // Read the time setting information of the screen saver from the registry //

}

4. Add the following code to the Click event of Form1's Button1:

Void __fastcall tform1 :: button1click (Tobject * Sender)

{

MyReg-> WriteInteger ("Intervl", Edit1-> text.toint ());

MyReg-> free (); // Write the time information of the screen saver in the registry //

CLOSE ();

}

5. Add the following code in the Click event of FOM1 button2:

Void __fastcall tform1 :: button2click (Tobject * Sender)

{

CLOSE ();

}

6. Add the following code in Unit2.h:

PUBLIC:

Begin_MESSAGE_MAP

Message_handler (WM_RASEBKGND, TwmeseBkGnd, WMERaseBkGnd)

Message_handler (wm_activate, twmactivate, wmactivate)

Message_handler (WM_SysCommand, TwmsysCommand, WMSysCommand)

END_MESSAGE_MAP (TFORM) / / Define Windows Messages // INT X1, X2, X3, X4, X5, X6;

Private:

Void __fastcall createparams (tcreateparams & params);

Void __fastcall wmerasebkgnd (twmesebkgnd & msg);

Void __fastcall wmactivate; twmactivate & msg;

Void __fastcall wmsysCommand (TWMSysCommand & MSG); // to respond to and process Windows messages. //

Graphics :: Tbitmap * screenbitmap; // Screen saver background color //

7. Add the following code in the oncreate event of Form2:

Void __fastcall tform2 :: form2create (TOBJECT * Sender)

{

LEFT = 0;

TOP = 0;

Width = screen-> width;

Height = screen-> height; // Make the size of the window to the size of the entire screen //

Cursor = crnone; file: // Hide Screen Cursor //

Screenbitmap-> width = width;

Screenbitmap-> height = height;

Sreenbitmap-> canvas-> brush-> color = clblack; // set screen background size and color //

Screenbitmap-> Canvas-> FillRect (RECT (0, 0, Width, Height);

Timer1-> enabled = true;

FORM1-> MyReg-> OpenKey ("Software // MyCompany // Remember", True);

Timer1-> Interval = form1-> myreg-> readinteger ("interval");

X4 = 10;

X5 = 20;

X6 = 30;

}

8. Add the following code in the onClose event in Form2:

Void __fastcall tform2 :: form2close (TOBJECT * Sender)

{

Timer1-> enabled = false;

Delete screenbitmap;

}

9. Add the following code in Form2 onkeyDown, onMouseDown, onmousend event:

Void __fastcall tform2 :: form2keydown (TOBJECT * Sender, Word & Key,

TshiftState Shift)

{

CLOSE ();

MyReg-> Fee ();

}

Void __fastcall tform2 :: form2mousedown (TOBJECT * Sender, TMousebutton Button,

TshiftState Shift, Int X, Int Y)

{

CLOSE ();

MyReg-> free ();

}

Void __fastcall tform2 :: form2mousemove (Tobject * Sender, TshiftState Shift, Int x,

Int Y)

{

CLOSE ();

MyReg-> free ();

} // If a mouse move occurs, the keyboard button is turned off the screen saver //

10. Handling WM_ERARSEBKGND message for Windows:

Void __fastcall tform2 :: wmerasebkgnd (twmesebkgnd & msg) {

Msg.result = false; file: // Do not refresh the screen background //

}

11, handle WM_ACTIVE message for Windows:

Void __fastcall tform2 :: wmactivate (twmactivate & msg)

{

IF (msg.active == false)

Close (); // After turning off the shutdown screen protection, the program automatically exits //

}

12. Handling WM_SYSCOMMAND message for Windows:

Void __fastcall tform1 :: wmsysCommand (TwmsysCommand & MSG)

{

IF (msg.cmdtype == sc_screensave)

Msg.Result = true; / / Prevent the screen saver from being repeated ///

Else

TFORM :: Dispatch (& msg); // The program sent the message to other objects //

13. Add the following code in Timer1 Timer Event:

Void __fastcall tform1 :: Timer1Timer (TOBJECT * SENDER)

{

X1 = random (screen.height);

X2 = random (screen.weight);

X3 = random (screen.height);

Canvas-> Pen-> Color = RGB (0,0,0);

Canvas-> Pen-> width = 10;

Canvas-> Brush-> color = RGB (0,0,0);

Canvas-> Ellipse (X4, X5, ABS (X4-x6), ABS (X5-X6));

Canvas-> Pen-> Color = RGB (ranndom (255), Random (255), Random (255));

Canvas-> Pen-> width = 10;

Canvas-> brush-> color = RGB (ranndom (255), Random (255), Random (255));

Canvas-> Ellipse (X1, X2, ABS (X1-X3), ABS (X2-X3));

X4 = x1;

X5 = x2;

X6 = x3; // screen protection effect, users can define themselves //

}

14. Add the following code in the ONPAINT event of Form2:

Void __fastcall tform1 :: formpaint (TOBJECT * SENDER)

{

Canvas-> DRAW (0, 0, Screenbitmap); // Heavy Picture Background //

}

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

New Post(0)