To prevent Windows screen protection from applications

Source: Internet
Author: User
Tags definition bool

If you need a long period of time in your program to do a lot of data processing, this time is long enough for Windows to activate the screen saver because the user action is not detected, and once the screen saver is started, your program actually slows down, which can greatly affect the normal operation of the program. Is there a way to turn off screen protection before a program is processed for a long time? The answer is yes. Windows sends a WM_SYSCOMMAND message to the currently active application before the screen saver starts, where the wparam parameter specifies the type of system command that is about to be executed, in this case the value is sc_screensave. The question is how does the program capture the message? The OnMessage event handle of the Tapplication class can be used in C + + Builder to process this message. The application triggers the OnMessage event of the Tapplication class after receiving any Windows messages, and by defining the handler for this event, you can capture all Windows messages sent to the application ( This, of course, does not include messages sent in the program using the SendMessage function.

The OnMessage event is defined as follows:

typedef void__fastcall (__closure

*tmessageevent) (tagmsg amp; Msg,boolamp; Handled);

__property tmessageevent onmessage=

{read=fonmessage,write=fonmessage};

Where the tmessageevent type is the type of the OnMessage event, which defines the method for handling the message, the MSG parameter gives information about the Windows message, which is structured as follows:

typedef struct tagmsg{

HWND hwnd;

UINT message;

WPARAM WPARAM;

LPARAM LPARAM;

DWORD time;

Point pt;

The handled parameter determines how the message is processed next, and if the handled argument is set to True after a message is received, the message is not processed further, in this case the activation of the screen saver is canceled.

Start C + + Builder, create a new project file, and add a declaration to the member function capturemessage in the private segment of the header file:

Class Tform1:public Tform

{

__published:

Private

void __fastcall capturemessage (tagmsg amp; Msg,bool amp; Handled);

Public

__fastcall TForm1 (tcomponent* Owner);

};

Add the definition of Capturemessage to the. cpp file:

void __fastcall tform1::capturemessage (tagmsg amp; Msg,bool amp; Handled)

{

if (msg.message= =wm_syscommand amp;amp; msg.wparam= =sc_screensave)

Handled=true; Prevent screen saver from starting

Else

Handled=false; Perform the default processing for this message

}

Then use the defined Capturemessage function as the event handler for the OnMessage event, adding the following code to the main form's OnCreate event handler:

void __fastcall tform1::formcreate (tobject *sender)

{

Application-gt;onmessage=capturemessage;

}

Press F9 to compile and run the program, you can in advance to set the screen saver wait time to a minimum value to check the operation of the program. You will find that during the program run, the screen saver will not be activated, close the running program and wait for a while, the screen saver will appear normally. The above code runs through the C + + Builder3, win98 environment.

All Windows messages sent to the application (this, of course, does not include messages sent in the SendMessage function in the program).

The OnMessage event is defined as follows:

typedef void__fastcall (__closure

*tmessageevent) (tagmsg amp; Msg,boolamp; Handled);

__property tmessageevent onmessage=

{read=fonmessage,write=fonmessage};

Where the tmessageevent type is the type of the OnMessage event, which defines the method for handling the message, the MSG parameter gives information about the Windows message, which is structured as follows:

typedef struct tagmsg{

HWND hwnd;

UINT message;

WPARAM WPARAM;

LPARAM LPARAM;

DWORD time;

Point pt;

The handled parameter determines how the message is processed next, and if the handled argument is set to True after a message is received, the message is not processed further, in this case the activation of the screen saver is canceled.

Start C + + Builder, create a new project file, and add a declaration to the member function capturemessage in the private segment of the header file:

Class Tform1:public Tform

{

__published:

Private

void __fastcall capturemessage (tagmsg amp; Msg,bool amp; Handled);

Public

__fastcall TForm1 (tcomponent* Owner);

};

Add the definition of Capturemessage to the. cpp file:

void __fastcall tform1::capturemessage (tagmsg amp; Msg,bool amp; Handled)

{

if (msg.message= =wm_syscommand amp;amp; msg.wparam= =sc_screensave)

Handled=true; Prevent screen saver from starting

Else

Handled=false; Perform the default processing for this message

}

Then use the defined Capturemessage function as the event handler for the OnMessage event, adding the following code to the main form's OnCreate event handler:

void __fastcall tform1::formcreate (tobject *sender)

{

Application-gt;onmessage=capturemessage;

}

Press F9 to compile and run the program, you can in advance to set the screen saver wait time to a minimum value to check the operation of the program. You will find that during the program run, the screen saver will not be activated, close the running program and wait for a while, the screen saver will appear normally. The above code runs through the C + + Builder3, win98 environment.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.