Delphi65 0 Posted December 17, 2022 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
Uwe Raabe 2056 Posted December 17, 2022 Try HWND_BROADCAST instead of Application.Handle Share this post Link to post
Delphi65 0 Posted December 17, 2022 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
programmerdelphi2k 237 Posted December 17, 2022 (edited) 2 hours ago, Delphi65 said: SendMessage( Handle, 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! 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 December 17, 2022 by programmerdelphi2k Share this post Link to post
Delphi65 0 Posted December 17, 2022 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
PeterBelow 238 Posted December 17, 2022 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
programmerdelphi2k 237 Posted December 17, 2022 my user in Windows is "Local / Adminstrator" by default Win installation! Share this post Link to post
Delphi65 0 Posted December 17, 2022 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
Delphi65 0 Posted December 17, 2022 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
DelphiUdIT 172 Posted December 18, 2022 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
programmerdelphi2k 237 Posted December 18, 2022 in fact, if "Turn OFF" works, "Turn ON" should too! but dont! Share this post Link to post
Delphi65 0 Posted December 18, 2022 (edited) 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 December 18, 2022 by Delphi65 Missing letters Share this post Link to post
DelphiUdIT 172 Posted December 18, 2022 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 1 Share this post Link to post
Delphi65 0 Posted December 19, 2022 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