How To Turn On/Off Monitor with Delphi

Do you need to shutdown the monitor to conserve power during program execution? In this article I will explain how to manipulate monitor power through Delphi code.

Step 1

Understand WM_SYSCOMMAND message. Before I explain the steps required to shutdown monitor power, I will explain a little bit about the WM_SYSCOMMAND message. WM_SYSCOMMAND is sent to the window to inform that the user is doing a system level command. Shutting down the monitor is one of the system commands that WM_SYSCOMMAND generated.

Step 2

Power on the monitor. There are three states of monitor power: On, Off and Stand By (low power mode). To power the monitor on, you send WM_SYSCOMMAND with wParam set to SC_MONITORPOWER and lParam set to $FFFFFFFF (or -1). The following code is an example of how to do it.

//power on monitor


SendMessage(Application.Handle, WM_SYSCOMMAND, SC_MONITORPOWER, $FFFFFFFF);

Step 3

Power off the monitor. To power off the monitor, you send WM_SYSCOMMAND with wParam set to SC_MONITORPOWER and lParam set to 2.

//power off monitor
SendMessage(Application.Handle, WM_SYSCOMMAND, SC_MONITORPOWER, 2);

Step 4

Power the monitor to stand by mode. And last, to put the monitor into low power mode (stand by), you send WM_SYSCOMMAND with wParam set to SC_MONITORPOWER and lParam set to 1.

//power off monitor
SendMessage(Application.Handle, WM_SYSCOMMAND, SC_MONITORPOWER, 1);

Step 5

Include required units in your uses clause. In order to make it work, you need to include required units in your uses clause. Function SendMessage() is declared in windows.pas unit and WM_SYSCOMMAND is declared in messages.pas unit, so you must ensure it is included in uses clause.

 

Share this article!

Follow us!

Find more helpful articles: