7

I know how to turn off all monitors

SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM) 2);

But I want to turn only single one.

l00k
  • 1,525
  • 1
  • 19
  • 29
  • It's required to use following function from BOOL SetVCPFeature(HANDLE hMonitor, BYTE bVCPCode, DWORD dwNewValue); In order to get handle you need to use EnumDisplayMonitors and then pass HMONITOR into GetPhysicalMonitorsFromHMONITOR. bVCPCode to switch power: 0xD6 dwNewValue to turn on: 0x01, to turn off 0x04 – l00k Sep 30 '15 at 08:07
  • Sample: http://pastebin.com/ZaT99Xek – l00k Sep 30 '15 at 10:30

1 Answers1

9

As I mentioned above - it is required to use lowlevelmonitorconfigurationapi library.

The following code is a sample about how to create an independent screensaver for all monitors.

#include <lowlevelmonitorconfigurationapi.h>
#include <windows.h>

const BYTE PowerMode = 0xD6;
const DWORD PowerOn = 0x01;
const DWORD PowerOff = 0x04;

/*
 * Monitor description struct
 */
struct MonitorDesc
{
    HANDLE hdl;
    RECT rc;
    DWORD lastTick;
    DWORD power;
};

/*
 * Monitor enumeration callback
 */
BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
{
    std::vector<MonitorDesc>* pMonitors = reinterpret_cast< std::vector<MonitorDesc>* >(dwData);

    DWORD nMonitorCount;
    if(GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, &nMonitorCount))
    {
        PHYSICAL_MONITOR* pMons = new PHYSICAL_MONITOR[nMonitorCount];

        if(GetPhysicalMonitorsFromHMONITOR(hMonitor, nMonitorCount, pMons))
        {
            for(DWORD i=0; i<nMonitorCount; i++)
            {
                MonitorDesc desc;
                desc.hdl = pMons[i].hPhysicalMonitor;
                desc.rc = *lprcMonitor;

                pMonitors->push_back(desc);
            }
        }
        delete[] pMons;
    }
    return TRUE;
}

/*
 * Switch monitor power
 */
void MonitorSwitch(MonitorDesc& monitor, DWORD mode)
{
    if(monitor.power == mode)
        return;

    SetVCPFeature(monitor.hdl, PowerMode, mode);
    monitor.power = mode;
}

/*
 * Main
 */
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
{
    // Idle time
    DWORD IdleTime = 10 * 60 * 1000;

    std::vector<MonitorDesc> monitors;
    EnumDisplayMonitors(NULL, NULL, &MonitorEnumProc, reinterpret_cast<LPARAM>(&monitors));

    DWORD tick = GetTickCount();

    // Init
    for(auto& monitor : monitors)
    {
        monitor.power = PowerOn;
        monitor.lastTick = tick;
    }

    // Last cursor position
    POINT lastPos;
    GetCursorPos(&lastPos);

    while(1)
    {
        DWORD tick = GetTickCount();

        POINT pt;
        GetCursorPos(&pt);

        // Check cursor move
        bool move = false;
        if(pt.x != lastPos.x || pt.y != lastPos.y)
        {
            move = true;
        }

        lastPos = pt;

        if(move)
        {
            // Check which monitor status should be updateted
            for(auto& monitor : monitors)
            {
                if(pt.x >= monitor.rc.left && pt.x < monitor.rc.right && pt.y >= monitor.rc.top && pt.y < monitor.rc.bottom)
                {
                    monitor.lastTick = tick;
                    break;
                }
            }
        }

        for(auto& monitor : monitors)
        {
            // Check idle time
            DWORD delta = (tick - monitor.lastTick);
            if(delta >= IdleTime)
            {
                // turn off
                if(monitor.power == PowerOn)
                {
                    MonitorSwitch(monitor, PowerOff);
                }
            }
            else
            {
                // turn on
                if(monitor.power == PowerOff)
                {
                    MonitorSwitch(monitor, PowerOn);
                }
            }
        }
        Sleep(50);
    }

    return 0;
}
Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
l00k
  • 1,525
  • 1
  • 19
  • 29
  • 1
    The `PowerMode` VCP code is documented in page 70 of [this document](https://milek7.pl/ddcbacklight/mccs.pdf) – GetFree Aug 28 '21 at 00:37