C#如何关闭特定的监视器?

时间:2019-11-27 08:47:47

标签: c# winapi

我知道如何使用user32.dll打开/关闭所有监视器。

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

但是我只想打开/关闭一个。

2 个答案:

答案 0 :(得分:1)

您总是可以从nirsoft运行ControlMyMonitor.exe

从1.20版开始,您可以使用以下方法打开和关闭显示器:

ControlMyMonitor.exe /SwitchValue "\\.\DISPLAY1\Monitor0" D6 1 5

答案 1 :(得分:0)

@Dai是对的。使用Low-Level Monitor Configuration Functions可以关闭指定的监视器。

以下是选择一台显示器并在5秒钟后将其关闭后将其关闭的示例。

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

#pragma comment(lib, "Dxva2.lib")

const BYTE PowerMode = 0xD6;  // VCP Code defined in VESA Monitor Control Command Set (MCCS) standard
const DWORD PowerOn = 0x01;
const DWORD PowerOff = 0x04;

// Monitor description struct
struct MonitorDesc
{
    HANDLE hdl;
    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;

                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;
}

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

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

    // Here select the first one monitor as example
    MonitorDesc targetMonitor = monitors[0];

    // turn off
    if (targetMonitor.power == PowerOn)
    {
        MonitorSwitch(targetMonitor, PowerOff);
    }

    Sleep(5000);

    // turn on
    if (targetMonitor.power == PowerOff)
    {
        MonitorSwitch(targetMonitor, PowerOn);
    }
}