Let the app prohibit Windows screen protection

zhaozj2021-02-08  233

---- If you need a long period of time in your program to make a lot of data processing, this time is enough to activate the screen saver because Windows does not detect the user action, once the screen saver is started Your program is actually slower, which greatly affects the normal operation of the program. Is there a way to turn off the screen protection before the program is processed for a long time? The answer is yes. Windows sends a WM_SYSCOMMAND message to the currently activated application before starting the screen saver, where the WPARAM parameter specifies the upcoming system command type, in this example is SC_Screensave. The problem is how the program captures this message? This message can be processed using the TAPPLICATION class onMessage event handle in C Builder. The application will trigger the onMessage event of the TAPPLICATION class after receiving any Windows messages. By defining the handler of this event, you can capture all Windows messages sent to the application (this is of course not included in the program in the program.) .

---- OnMessage event is defined as follows:

Typedef void__fastcall (__ closure

* TAGMSG & MSG, BOOL & HANDLEDs;

__property tMessageEvent OnMessage =

{read = fonMessage, Write = fonmessage};

---- The TMessageEvent type is the type of OnMessage event. It defines how to process messages, the MSG parameter gives the relevant information about Windows messages, and its structure is as follows:

Typedef struct tagmsg {

Hwnd hwnd;

Uint Message;

WPARAM WPARAM;

LParam Lparam;

DWORD TIME;

Point pt;}

---- Handled parameter determines how to perform the next step for the message, if the Handled parameter is set to True after receiving a message, the message will not be further processed, in this example, the disconnection screen is canceled. Activation.

---- Start C Builder, create a new project file, add a declaration of the member function CaptureMessage in the header file:

Class TFORM1: Public TForm

{

__publish:

Private:

Void __fastcall CaptureMessage (Tagmsg & MSG, Bool & Handled);

PUBLIC:

__fastcall tform1 (tComponent * Owner);

}

---- Increase the definition of CaptureMessage in .CPP file:

Void __fastcall tform1 :: CaptureMessage (tagmsg & msg, bool & hand)

{

IF (msg.message = = wm_syscommand && msg.wparam = = sc_screensave)

Handled = true; / / block the start of screen protection

Else

Handled = false; // Default processing for this message

}

---- Then, the defined CaptureMessage function is used as the event processor of the OnMessage event, add the following code to the main form of the host:

Void __fastcall tform1 :: formcreate (TOBJECT * SENDER)

{

Application-> onMessage = CaptureMessage;

}

---- Press F9 to compile and run the program, you can set the wait time of the screen protected into a smaller value to verify the operation of the program in advance. You will find that the screen saver will not be activated, then wait for a while, and the screen saver will appear normally after the program is turned off. The above code runs in C Builder3, Win98 environment

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

New Post(0)