GetMonitorCapabilities返回false

时间:2017-05-20 09:10:41

标签: qt winapi

我想在Windows上调整显示器的亮度。

我按照这个页面:

How to use GetMonitorCapabilities and GetMonitorBrightness functions

但是,GetMonitorCapabilities函数返回false。

我使用qDebug打印,hPhysicalMonitor为NULL。这点错了吗?

GetPhysicalMonitorsFromHMONITOR函数返回true)

如何修复此代码?

HMONITOR hMonitor = NULL;
DWORD cPhysicalMonitors = 1;
LPPHYSICAL_MONITOR pPhysicalMonitors;

HWND hWnd = GetDesktopWindow();

// Get the monitor handle.
hMonitor = MonitorFromWindow(hWnd, MONITOR_DEFAULTTOPRIMARY);

// Get the number of physical monitors.
BOOL bSuccess = GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, &cPhysicalMonitors);

if (bSuccess)
{
    // Allocate the array of PHYSICAL_MONITOR structures.
    pPhysicalMonitors = (LPPHYSICAL_MONITOR)malloc(cPhysicalMonitors* sizeof(PHYSICAL_MONITOR));

    if (pPhysicalMonitors != NULL)
    {
        // Get the array.
        bSuccess = GetPhysicalMonitorsFromHMONITOR( hMonitor, cPhysicalMonitors, pPhysicalMonitors);
        if (bSuccess == FALSE)
        {
            qDebug("GetPhysicalMonitorsFromHMONITOR");
        }
        // Get physical monitor handle.
        HANDLE hPhysicalMonitor = pPhysicalMonitors[0].hPhysicalMonitor;

        DWORD pdwMonitorCapabilities = 0;
        DWORD pdwSupportedColorTemperatures = 0;
        bSuccess = GetMonitorCapabilities(hPhysicalMonitor, &pdwMonitorCapabilities, &pdwSupportedColorTemperatures);
        if (bSuccess == FALSE)
        {
            qDebug("GetMonitorCapabilities");
        }

        DWORD pdwMinimumBrightness = 0;
        DWORD pdwCurrentBrightness = 0;
        DWORD pdwMaximumBrightness = 0;
        bSuccess = GetMonitorBrightness(hPhysicalMonitor, &pdwMinimumBrightness, &pdwCurrentBrightness, &pdwMaximumBrightness);
        if (bSuccess == FALSE)
        {
            qDebug("GetMonitorBrightness");
        }

        qDebug(QByteArray::number(bSuccess));

        qDebug(QByteArray::number((WORD)pdwMinimumBrightness));
        qDebug(QByteArray::number((WORD)pdwCurrentBrightness));
        qDebug(QByteArray::number((WORD)pdwMaximumBrightness));
        // Close the monitor handles.
        bSuccess = DestroyPhysicalMonitors(cPhysicalMonitors, pPhysicalMonitors);

        // Free the array.
        free(pPhysicalMonitors);
    }
}

1 个答案:

答案 0 :(得分:0)

如果要使用WmiMonitorBrightnessMethods调整亮度,请使用以下代码。

     public static void SetWmiMonitorBrightness(byte targetBrightness) 
    {
        ManagementScope scope = new ManagementScope("root\\WMI");
        SelectQuery query = new SelectQuery("WmiMonitorBrightnessMethods");
        using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query))
        {
            using (ManagementObjectCollection objectCollection = searcher.Get())
            {
                foreach (ManagementObject mObj in objectCollection)
                {
                    mObj.InvokeMethod("WmiSetBrightness",
                        new Object[] { UInt32.MaxValue, targetBrightness });
                    break;
                }
            }
        }
    }

在我拥有的特定监视器上执行GetMonitorBrightness会导致ERROR_GRAPHICS_I2C_ERROR_TRANSMITTING_DATA。

所以我改用WmiSetBrightness。很好。

如果您收到有关ERROR_GRAPHICS_I2C_ERROR_TRANSMITTING_DATA的提示,请与我分享。