Jump to content
Delphi65

Turn display Off/On

Recommended Posts

Hi guys,

I have been using the following for earlier version of windows.

Quote

const
  MONITOR_ON      = -1;
  MONITOR_OFF     =  2;
  MONITOR_STANDBY =  1;

To turn off the monitor:


  SendMessage(Application.Handle, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_OFF);

To turn on the monitor:


  SendMessage(Application.Handle, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_ON);

But this unfortunately no longer works for Windows 10, any idea of how to achieve the same goal on Windows PC?

Many thanks in advance!

Share this post


Link to post
30 minutes ago, Uwe Raabe said:

Try HWND_BROADCAST instead of Application.Handle

Thanks Uwe!

I have tried it, it has no impact, it doesn't turn the display off.

Share this post


Link to post
2 hours ago, Delphi65 said:

SendMessageHandle, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_OFF); = 2

here, it works! -> Win10 22H2 64bits

-- 

2 hours ago, Delphi65 said:

MONITOR_STANDBY = 1  or MONITOR_ON = -1

doesnt!

 

image.png.91d3abbbfa809287fd65d48d62ed13a9.png

procedure TForm1.Button1Click(Sender: TObject);
begin
  Timer1.Interval := 5000;
  Timer2.Enabled  := true;
  //
  SendMessage(handle, WM_SYSCOMMAND, SC_MONITORPOWER, 2); //works
  //
  Timer1.Enabled := true;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  Timer1.Enabled := false;
  //
  SendMessage(handle, WM_SYSCOMMAND, SC_MONITORPOWER, -1); // doesnt
  //
  Memo1.Lines.Add(timetostr(now) + '  -> timer1 -> after power ON');
end;

procedure TForm1.Timer2Timer(Sender: TObject);
begin
  Memo1.Lines.Add(timetostr(now))
end;

 

Edited by programmerdelphi2k

Share this post


Link to post
1 hour ago, programmerdelphi2k said:

SendMessage(handle, WM_SYSCOMMAND, SC_MONITORPOWER, 2);

It doesn't work for me. Not with timer nor a button. I also have Windows 10  22H2 64bit

Share this post


Link to post
5 hours ago, Delphi65 said:

Hi guys,

I have been using the following for earlier version of windows.

Many thanks in advance!

Have you tried running the code under an admin account? This may be a rights issue, since you are changing a setting that effects all users.

Share this post


Link to post
1 hour ago, PeterBelow said:

Have you tried running the code under an admin account? This may be a rights issue, since you are changing a setting that effects all users.

I have tested with two different PC where both were pretty same and both with administrator account.

Share this post


Link to post
1 hour ago, programmerdelphi2k said:

my user in Windows is "Local / Adminstrator" by default Win installation!

I have the same, user is the administrator.

Share this post


Link to post

To turn monitor on simply simulate a keypressed:

 

  //Simulate the pression of SHIFT key ...
  keybd_event(VK_SHIFT, 0, 0, 0);
  keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);

Bye

Share this post


Link to post
1 hour ago, DelphiUdIT said:

To turn monitor on simply simulate a keypressed:

 


  //Simulate the pression of SHIFT key ...
  keybd_event(VK_SHIFT, 0, 0, 0);
  keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);

Bye

My problem is that i cannot turn the display off.

Yes, you are correct, it can be turned on by using a key press.

Edited by Delphi65
Missing letters

Share this post


Link to post
29 minutes ago, Delphi65 said:

My problem is that i cannot turn the display off.

Yes, you are correct, it can be turned on by using a key press.

PostMessage(HWND_BROADCAST , WM_SYSCOMMAND, SC_MONITORPOWER, 2);

This should works, it always works in every version of Windows. If it not works, it's possible that you have some drivers, services or others that prevents you from turning it off.

Any movements of the mouse and key pressing turn on the monitor.

 

Bye

  • Thanks 1

Share this post


Link to post

Thank you to all!

I have done the following and both Turn On/TurnOff works.

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls;
type
  TfrmPowerControl = class(TForm)
    btnOFF: TButton;
    btnExit: TButton;
    Label1: TLabel;
    btnON: TButton;
    timerTurnOffDisplay: TTimer;
    procedure btnOFFClick(Sender: TObject);
    procedure btnONClick(Sender: TObject);
    procedure btnExitClick(Sender: TObject);
    procedure timerTurnOffDisplayTimer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmPowerControl: TfrmPowerControl;

implementation

{$R *.dfm}

  const
  MONITOR_ON      = -1;
  MONITOR_OFF     =  2;
  MONITOR_STANDBY =  1;

procedure TfrmPowerControl.btnOFFClick(Sender: TObject);
begin
  PostMessage(HWND_BROADCAST , WM_SYSCOMMAND, SC_MONITORPOWER, 2);
end;

procedure TfrmPowerControl.btnExitClick(Sender: TObject);
begin
  Close;
  Application.Terminate;
end;

procedure TfrmPowerControl.btnONClick(Sender: TObject);
begin
  PostMessage(HWND_BROADCAST , WM_SYSCOMMAND, SC_MONITORPOWER, -1);
end;

procedure TfrmPowerControl.timerTurnOffDisplayTimer(Sender: TObject);
begin
  //PostMessage(HWND_BROADCAST , WM_SYSCOMMAND, SC_MONITORPOWER, 2);
end;

end.

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×