c – 如何使用GetMonitorCapabilities和GetMonitorBrightness函数

前端之家收集整理的这篇文章主要介绍了c – 如何使用GetMonitorCapabilities和GetMonitorBrightness函数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试以编程方式调整我的显示器亮度.经过一些研究,我想出了这个 link,并编写了以下代码(主要是从其他链接复制粘贴,一个引导我).
#include "Windows.h"
#include "WinUser.h"
#include "PhysicalMonitorEnumerationAPI.h"
#include "HighLevelMonitorConfigurationAPI.h"
#include <strsafe.h>

void ShowError(LPTSTR lpszFunction);

int main()
{
    HMONITOR hMonitor = NULL;
    DWORD cPhysicalMonitors;
    LPPHYSICAL_MONITOR pPhysicalMonitors = NULL;

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

            // Get physical monitor handle.
            HANDLE hPhysicalMonitor = pPhysicalMonitors[0].hPhysicalMonitor;

            LPDWORD pdwMinimumBrightness = NULL;
            LPDWORD pdwCurrentBrightness = NULL;
            LPDWORD pdwMaximumBrightness = NULL;
            bSuccess = GetMonitorBrightness(hPhysicalMonitor,pdwMinimumBrightness,pdwCurrentBrightness,pdwMaximumBrightness);
            if (bSuccess == FALSE)
            {
                ShowError(TEXT("GetMonitorBrightness"));
            }

            // Close the monitor handles.
            bSuccess = DestroyPhysicalMonitors(cPhysicalMonitors,pPhysicalMonitors);

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

void ShowError(LPTSTR lpszFunction)
{
    // Retrieve the system error message for the last-error code
    LPVOID lpMsgBuf;
    LPVOID lpDisplayBuf;
    DWORD dw = GetLastError();

    FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER |
        FORMAT_MESSAGE_FROM_SYSTEM |
        FORMAT_MESSAGE_IGNORE_INSERTS,NULL,dw,MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPTSTR) &lpMsgBuf,NULL );

    // Display the error message and exit the process
    lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,(lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR));
    StringCchPrintf((LPTSTR)lpDisplayBuf,LocalSize(lpDisplayBuf) / sizeof(TCHAR),TEXT("%s Failed with error %d: %s"),lpszFunction,lpMsgBuf);
    MessageBox(NULL,(LPCTSTR)lpDisplayBuf,TEXT("Error"),MB_OK);

    LocalFree(lpMsgBuf);
    LocalFree(lpDisplayBuf);
}

执行此行时此代码崩溃:

bSuccess = GetMonitorBrightness(hPhysicalMonitor,pdwMaximumBrightness);

根据文档,可能不支持功能.

If this function is supported,the GetMonitorCapabilities function
returns the MC_CAPS_BRIGHTNESS flag.

因此,为了检查这一点,我在调用GetMonitorBrightness之前将以下块添加到我的代码中.

LPDWORD pdwMonitorCapabilities = NULL;
LPDWORD pdwSupportedColorTemperatures = NULL;
bSuccess = GetMonitorCapabilities(hPhysicalMonitor,pdwMonitorCapabilities,pdwSupportedColorTemperatures);
if (bSuccess == FALSE)
{
    ShowError(TEXT("GetMonitorCapabilities"));
}

不幸的是,在我添加了该块之后,我收到了以下错误

同样,根据documentation,如果监视器不支持DDC/CI,则GetMonitorCapabilities函数将失败.

然后我检查了我的显示器是否支持DDC/C++I,并发现它是.此外,当我从监视器设置手动禁用DDC/C++I支持时,先前的错误消息切换到下一个,所以现在我非常确定我的监视器具有DDC/C++I支持.

我觉得我做的一切都是正确的,但显然我不是.简而言之,GetMonitorCapabilities函数失败并显示一条错误消息,我无法给出任何意义,并且GetMonitorBrightness函数会崩溃.

笔记:

我的显示器是Dell U2713H.

我在64位Windows 7上.

我正在使用Microsoft Visual C编译器12.0(x86)

解决方法

您对GetMonitorBrightness()和GetMonitorCapabilities()的调用错误的.您正在传递NULL指针,但他们希望指向实际的DWORD变量:
DWORD dwMinimumBrightness = 0;
DWORD dwCurrentBrightness = 0;
DWORD dwMaximumBrightness = 0;
bSuccess = GetMonitorBrightness(hPhysicalMonitor,&dwMinimumBrightness,&dwCurrentBrightness,&dwMaximumBrightness);
DWORD dwMonitorCapabilities = 0;
DWORD dwSupportedColorTemperatures = 0;
bSuccess = GetMonitorCapabilities(hPhysicalMonitor,&dwMonitorCapabilities,&dwSupportedColorTemperatures);
原文链接:https://www.f2er.com/c/119157.html

猜你在找的C&C++相关文章