To turn off monitor in Windows, SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM)2) is most widely used.
Instead, we can use low-level monitor configuration functions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* dependencies */ | |
#pragma comment(lib, "dxva2") | |
#pragma comment(lib, "user32") | |
#include <lowlevelmonitorconfigurationapi.h> | |
typedef struct _MONITORPOWERPARAM | |
{ | |
LPCWSTR szPhysicalMonitorDescription; | |
BOOL bPowerOn; | |
} MONITOR_POWER_PARAM, *PMONITOR_POWER_PARAM; | |
BOOL CALLBACK _MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM lParam) | |
{ | |
UNREFERENCED_PARAMETER(hdcMonitor); | |
UNREFERENCED_PARAMETER(lprcMonitor); | |
DWORD dwMonitorCount; | |
if (GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, &dwMonitorCount)) | |
{ | |
PHYSICAL_MONITOR* pMons; | |
if (pMons = HeapAlloc(GetProcessHeap(), 0, sizeof(PHYSICAL_MONITOR) * dwMonitorCount)) | |
{ | |
if (GetPhysicalMonitorsFromHMONITOR(hMonitor, dwMonitorCount, pMons)) | |
{ | |
for (DWORD i = 0; i < dwMonitorCount; i++) | |
{ | |
if (lstrcmpiW(((PMONITOR_POWER_PARAM)lParam)->szPhysicalMonitorDescription, pMons[i].szPhysicalMonitorDescription) == 0) | |
{ | |
SetVCPFeature(pMons[i].hPhysicalMonitor, 0xd6, ((PMONITOR_POWER_PARAM)lParam)->bPowerOn ? 1 : 4); | |
} | |
DestroyPhysicalMonitor(pMons[i].hPhysicalMonitor); | |
} | |
} | |
HeapFree(GetProcessHeap(), 0, pMons); | |
} | |
} | |
return TRUE; | |
} | |
VOID SetMonitorPower(LPCWSTR lpszPhysicalMonitorDescription, BOOL bPowerOn) | |
{ | |
MONITOR_POWER_PARAM lparam; | |
lparam.szPhysicalMonitorDescription = lpszPhysicalMonitorDescription; | |
lparam.bPowerOn = bPowerOn; | |
EnumDisplayMonitors(NULL, NULL, _MonitorEnumProc, (LPARAM)&lparam); | |
} | |
void main() | |
{ | |
SetMonitorPower(L"Generic PnP Monitor", FALSE); | |
Sleep(1000); | |
SetMonitorPower(L"Generic PnP Monitor", TRUE); | |
} |