Skip to content

Instantly share code, notes, and snippets.

@3735943886
Last active August 4, 2023 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save 3735943886/9f54ebb0c8e0b7f7317e9ce9c40ae292 to your computer and use it in GitHub Desktop.
Save 3735943886/9f54ebb0c8e0b7f7317e9ce9c40ae292 to your computer and use it in GitHub Desktop.
Turn On / Off display monitor
/* 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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment