The screen saver under Windows system is an application based on command line. When the screensaver is called, the operating system executes the program with a specific command line. This paper organizes and processes all command lines, including "/p", "/s", "/c" and "/a", where "/p" means to display the screen saver in the preview window; "/s" means to run the screen saver; "/c" means calling the setup dialog box; And "/a" means calling the password setting dialog box (invalid in WinNT). This program realizes a full-featured screensaver as simply as possible. When running the Windows screen saver installer, you can not only modify the password (invalid in WinNT), but also set the frequency of picture display and save the frequency value in the registry. When the screen saver runs, the image will change its display position at the frequency you set. I also left an assignment for readers. Please see the item of selecting a picture folder in figure 1. Press the browse button to set the path of the picture. The author realizes the function of browse button.
Save the path of the registry, and let the screensaver read the value of picdir when it starts. The code when picdir is equal to "No" has been realized by the author, and the code when picdir is not equal to "No" has been realized by the reader. In other words, let readers realize a screensaver that can display pictures in the picdir directory in turn.
Two. Realization method
First, introduce several API functions.
WinMain function:
Intwinapi winmain (hinstanceinstance,//current instance handle.
HINSTANCE hPrevInstance,//Last instance handle
LPSTR lpCmdLine,//Pointer to command line parameter (parameter to be used in this program)
Int nCmdShow // window status);
GetWindowLong function: a function to get the information of the specified window.
LONG GetWindowLong( HWND hWnd,//window/port handle.
Int nIndex // reference/confirm the returned information);
SetWindowLong function: change window properties.
LONG SetWindowLong(HWND hWnd,//window/port handle.
Int nIndex,//Information that specifies the value to set.
LONG dwNewLong // new value);
SetParent function: changes the parent window of the specified window.
HWND SetParent(HWND hWndChild,//Want/Change the window handle of the parent form.
Handle of the parent form of HWND hWndNewParent // new/);
GetClientRect function: Get the client area of the window.
BOOL GetClientRect(HWND hWnd,//window handle.
Address of LPRECT lpRect //RECT/ structure);
SetWindowPos function: change the size, position, top-level window, etc. The window.
Boolsettwindowpos (hwnd hwnd,//window handle.
Hwnd hwnd inserter,//handle to arrange the window order (z order)
Int X,//horizontal position
Int Y,//vertical position
int cx,// width
int cy,// height
Uintlags// window position tags);
SystemParametersInfo function: access or set system-level parameters.
Boolsystemaparametersinfo (uintuction,//specifies the system parameters to get or set.
UINT uiParam,//depends on the action to be taken.
PVOID pvParam,//depends on the action to be taken.
Uint fwini// Whether the user profile changes the flag);
ShowCursor function: Show or hide the cursor.
Int ShowCursor(BOOL bShow // mouse visibility tag);
GetVersion function: Get the version information of the system.
DWORD GetVersion(VOID)
The specific information of the above API functions can be found in the MSSDK documentation. After understanding the basic functions, the author briefly describes the implementation method.
1. Create a new project, add two forms and name the three forms MainForm, FrmConfig and FrmControl respectively. Add Timer control and TImage control on MainForm and FrmControl form respectively, set the BorderStyle of the two forms to bsNone, and set the background color to black. Add a picture to the Timage of two forms, with the size of FrmControl set to 130 pixels high and 160 pixels wide, and the Stretch property of TImage set to true. The style of FrmConfig is shown in figure 1.
2. Save the project file as screensaver.cpp, and other companies as Unitmain.cpp,
Unitcontrol.cpp,Unitconfig.cpp。
3. Write code. See the third part of the source program for the specific code.
4. Compile it into an executable file and change the file extension to scr.
5. Finally, copy the screen saver into the windows directory for testing. If everything is all right, just watch.
Until the picture is displayed anywhere on the screen.
Three. source code
The following is all the source code of this program, in which screensaver.cpp and Unitmain.cpp are the core codes.
/*{*******************************}*/
/*{***** screensaver.cpp ****}*/
/*{*******************************}*/
// - /
# Including
#pragma hdrstop
USERES(" screen saver . RES ");
USEFORM("Unitmain.cpp ",Frmmain);
USEFORM("Unitconfig.cpp ",frm config);
USEFORM("Unitcontrol.cpp ",frm control);
// - /
Winapi winmain (hinstance, hinstance, lpstr p, int)//"p" is a pointer to the command line parameter.
{ String StartType
AnsiString Command = p,temp
HWND CPWindow = NULL
if(Command== " ")
StartType = "/c ";
other
StartType = command. Substring (1, 2); //Get the first two parameters of the command line
attempt
{
Application initialization ();
If(StartType=="/c")// Starts the settings window.
application-create form(_ _ classid(TFrmConfig),frm config);
Else if(StartType=="/s ") starts the screen saver.
application-create form(_ _ classid(TFrmmain),Frmmain);
Else if(StartType=="/p")// preview
{
application-create form(_ _ classid(tfrm control),frm control);
Temp = command. Substring (3, command. Length ()-2); //Gets the string form of the handle of the screen saver preview window in the command line.
CPWindow =(long *) temperature. ToInt(); //casts the string form of the preview window handle to a long integer pointer.
RECT * look rect; //Create a RECT structure pointer.
long STYLE = GetWindowLong(Application-MainForm-Handle,GWL _ STYLE); //get the style of the FrmControl window.
style = style | WS _ CHILD
SetWindowLong (application-main form-handle, GWL _ style, style); //Set the window as a child window.
SetParent (application-main form-handle, CP window); //Set the screen saver preview window as the parent window of FrmControl.
GetClientRect(CPWindow,lookrect); //Get the client area of the screen saver preview window.
SetWindowPos(Application-main form-Handle,HWND_TOP,0,0,lookrect-right,lookrect-bottom,SW
p _ no zorder | SWP _ no activate | SWP _ show window); //Overlay the window of FrmControl on the workspace of the screen saver preview window and display it.
}
Else if(StartType=="/a")// Starts the password setting window.
{
Temp = command. Substring (3, command. Length ()-2);
CPWindow =(long *) temperature. ToInt(); //The following is the process of mpr.dll dynamically calling the PwdChangePasswordA function.
typedef UINT(CALLBACK * FUN)(LPSTR,HWND,UINT,UINT);
HINSTANCE hDll=LoadLibrary("mpr。 DLL”);
FUN myfun
if(hDll! = empty)
{
myfun=(FUN)GetProcAddress(hDll," PwdChangePasswordA ");
If (! Myfun) Free Library (HDLL);
other
myfun("SCRSAVE ",CPWindow,0,0); //Call of function
}
}
Application-run ();
}
Catch (exception)
{
Application-ShowException;
}
Returns 0;
}
// - /
/*{*******************************}*/
/*{*****
Home page of the company.h
****}*/
/*{*******************************}*/
// - /
#ifndef UnitmainH
# Define unit maintenance h
// - /
# Including
# Including
# Including
# Including
# Including
# Including
# Including
// - /
Class TFrmmain: public TForm
{
_ _ Published: // IDE managed components
TTimer * timer 1;
TImage * image 1;
void _ _ fast call form create(to object * Sender);
void _ _ fast call form keydown(to object * Sender,WORD Key,
TShiftState Shift);
void _ _ fast call for mousedown(to object * Sender,TMouseButton Button,
TShiftState Shift,int X,int Y);
void _ _ fastcall FormCloseQuery(to object * Sender,bool can close);
void _ _ fast call form close(to object * Sender,TCloseAction Action);
void _ _ fast call image 1 mousedown(to object * Sender,
TMouseButton Button,TShiftState Shift,int X,int Y);
void _ _ fast call image 1 mousemove(to object * Sender,TShiftState Shift,
int X,int Y);
void _ _ fast call timer 1 timer(to object * Sender);
Private://User statement
DWORD PWProtect
DWORD version;
String picdir
Int frequency;
Public://User statement
_ _ fast call TFrmmain(t component * Owner);
};
// - /
External package TFrmmain * Frmmain
// - /
#endif
// - /
/*{*******************************}*/
/*{***** Unitmain.cpp
****}*/
/*{*******************************}*/
// - /
# Including
#pragma hdrstop
# Including
# contains "Unitmain.h"
# Including
// - /
#pragma package (smart_init)
#pragma resource "*. dfm "
TFrmmain * Frmmain
// - /
_ _ fast call TFrmmain::TFrmmain(t component * Owner)
{
}
// - /
void _ _ fast call TFrmmain::form create(to object * Sender)
{
//Make the window the topmost window.
SetWindowPos(this-Handle, HWND_TOPMOST, 0,0,0,0, SWP _ no move | SWP _ Noxize);
//When the window covers the screen.
This-Width = screen width;
This-Height = screen height;
this-Top = 0;
this-Left = 0;
version = GetVersion();
treg istry * Registry = new treg istry;
attempt
{
If (version 0x80000000){
Registry-root key = HKEY _ current _ user;
Registry-open key(" \ Control Panel \ Desktop ",false);
pw protect = Registry-ReadInteger(" ScreenSaveUsePassword "); //Check whether it is password protected.
registry-close key(); }
Registry-root key = HKEY _ current _ user;
registry-open key(" \ Software \ code hunter ",true);
PicDir = Registry-ReadString(" PicDir "); //Get the picture directory
frequency = Registry-ReadInteger(" frequency "); //Get the frequency of image display.
if(picdir = = " ")picdir = " no ";
If (frequency 0 | | frequency 6) Frequency = 2;
Timer 1- interval = 1000 * frequency; Set timer
}#p# Subtitle #e#
_ _ finally
{
Delete the registry;
picdir = " no
}
//Check whether it runs under NT.
If (version! =0)
If (pwprotectversion 0x8000000)//If the system requires password protection and the system is not NT, set the system to the screen saver state to make the cursor disappear.
SystemParametersInfo(SPI _ SCREENSAVERRUNNING, 1,0,0);
And (! show cursor(false)-5);
}
// - /
void _ _ fast call TFrmmain::form keydown(to object * Sender,WORD Key,
TShiftState shift)
{
this-Close();
}
// - /
void _ _ fast call TFrmmain::form mousedown(to object * Sender,
TMouseButton button, TShiftState Shift, int X, int Y)
{
this-Close();
}
// - /
void _ _ fast call TFrmmain::FormCloseQuery(to object * Sender,bool CanClose)
{
if (PWProtect
Version 0x80000000)
{
bool PassChck
//Display/Display the cursor and call the password dialog box.
And (! ShowCursor(True)
5);
//With/ is a dynamic call to the VerifyScreenSavePwd function.
Typedef UINT (callback * fun) (hwnd);
h instance hDll = LoadLibrary(" password . CPL ");
FUN myfun
if(hDll! = empty)
{
myfun=(FUN)GetProcAddress(hDll," VerifyScreenSavePwd ");
If (! Myfun) Free Library (HDLL);
other
PassChck = my fun(this-Handle);
}
if(PassChck == false)
{
And (! ShowCursor(False)
-5);
CanClose = false
}
}
}
// - /
Void _ _ fastcall tfrmmain:: formclose (toobject * sender, TCloseAction operation)
{
And (! ShowCursor(True)
5);
if(PWProtectVersion0x80000000)
SystemParametersInfo(SPI _ SCREENSAVERRUNNING,0,0,0); //Exit the screen saver.
}
// - /
void _ _ fast call TFrmmain::image 1 mousedown(to object * Sender,
TMouseButton button, TShiftState Shift, int X, int Y)
{
this-Close();
}
// - /
void _ _ fast call TFrmmain::image 1 mousemove(to object * Sender,
TShiftState Shift,int X,int Y)
{
static int MouseMoves = 0;
Mouse movement = mouse movement