C++ Builder

La Isla Bonita



SendMessage Function: How to turn the Monitor Off and On
SendMessage Function: How to turn the Monitor Off and On


Description

This example demonstrates how to turn the monitor off and on via the Win32 API function: SendMessage().

This is achieved by sending the WM_SYSCOMMAND windows message with the first parameter (wParam) set to SC_MONITORPOWER.

To turn the monitor off we set the second parameter (lParam) to 0:

    SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 0);
To turn the monitor on we set the second parameter (lParam) to -1:

    
    SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, -1);

Files

MonitorOnAndOff.cpp

#include <vcl.h>
#pragma hdrstop

#include "MonitorOnAndOff.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
TTimer* Timer1;
String OldCaption;
String NewCaption;

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
      //Create Timer
    Timer1 = new TTimer(this);
    Timer1->Enabled  = false;
    Timer1->Interval = 1000;
    Timer1->OnTimer  = Timer1Timer;
    Label1->Caption = "Your monitor is on";
    OldCaption = NewCaption = Label1->Caption;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
// Turn off the Monitor

   Label1->Caption = NewCaption = "Your monitor will be turned off in 1 second";
   Update();

   if (OldCaption != NewCaption)
   {
       Sleep(1000);
       SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 0);
       Sleep(5000);
       Timer1->Enabled = true;
   }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
// Turn On the Monitor

    SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, -1);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
// Turn On the Monitor automatically after 5 seconds

    SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, -1);
    Timer1->Enabled = false;
    Label1->Caption = "The Timer has turned on your monitor";
}

MonitorOnAndOff.h

#ifndef MonitorOnAndOffH
#define MonitorOnAndOffH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
        TButton *Button1;
        TButton *Button2;
        TLabel *Label1;
        void __fastcall Button1Click(TObject *Sender);
        void __fastcall Button2Click(TObject *Sender);
        void __fastcall Timer1Timer(TObject *Sender);
private:	// User declarations
public:		// User declarations
        __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

Homepage

Copyright © 1997-2003 Rodolfo A. Frino. All rights reserved.