To prevent Windows screen protection from applications

Source: Internet
Author: User
Tags 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 to process this message in C + + Builder. The application triggers the OnMessage event of the Tapplication class after receiving any Windows messages, by defining the handler for the event, You can capture all Windows messages sent to an application (this, of course, does not include messages sent in the SendMessage function in your program).

The OnMessage event is defined as follows:

typedef void__fastcall (__closure *tmessageevent) (tagmsg &msg,bool&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 &Msg,bool &Handled);
public:     
__fastcall TForm1(TComponent* Owner);
};

Add the definition of Capturemessage to the. cpp file:

void __fastcall TForm1::CaptureMessage(tagMSG &Msg,bool &Handled)
{
if(Msg.message= =WM_SYSCOMMAND && Msg.wParam= =SC_SCREENSAVE)
Handled=true; //阻止屏幕保护的启动
else
Handled=false; //进行该消息的缺省处理
}

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->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.