无效的显示句柄GetNumberOfPhysicalMonitorsFromHMONITOR

时间:2017-11-21 16:33:15

标签: c++ winapi

我试图在我的显示器上创建一个用于调整亮度的应用程序,但似乎我无法获得正确的功能句柄。 (我无法传递if(GetPhysMonitorSuccess))。 我在我的一台显示器上打开了DDC / CI选项(我有2个,第二个在菜单中没有这样的选项)。

#include "stdafx.h"
#include <Windows.h>
#include "highlevelmonitorconfigurationapi.h"
#include <iostream>
#include <strsafe.h>
void ShowError(LPTSTR lpszFunction);
int main()
{
    DWORD B1, B2, B3, MonitorCount;
    HWND ActiveWindowHandle = GetDesktopWindow();//GetForegroundWindow();
    if (ActiveWindowHandle != nullptr)
    {
        HMONITOR MonitorHandle = MonitorFromWindow(nullptr, MONITOR_DEFAULTTOPRIMARY);
        BOOL GetMonitorCountSuccess = GetNumberOfPhysicalMonitorsFromHMONITOR(MonitorHandle, &MonitorCount);

        if (GetMonitorCountSuccess)
        {
            PHYSICAL_MONITOR *MonitorArray = new PHYSICAL_MONITOR[MonitorCount];
            BOOL GetPhysMonitorSuccess = GetPhysicalMonitorsFromHMONITOR(MonitorHandle, MonitorCount, MonitorArray);
            HANDLE HandleToPhysicalMonitor = MonitorArray[0].hPhysicalMonitor;

            if (GetPhysMonitorSuccess)
            {
                BOOL GetMonBrightnessSuccess = GetMonitorBrightness(HandleToPhysicalMonitor, &B1, &B2, &B3);
                std::cout << B1 << B2 << B3;
                if(!GetMonBrightnessSuccess)
                    ShowError(TEXT("GetMonitorBrightness"));
            }
            else
                ShowError(TEXT("GetPhysicalMonitorFromHMONITOR"));
        }
        else
            ShowError(TEXT("GetNumberOfPhysicalMonitorsFromHMONITOR"));
    }

    std::cin.get();
    return 0;
}

//found that on the net
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,
        0, 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, dw, lpMsgBuf);
    MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);

    LocalFree(lpMsgBuf);
    LocalFree(lpDisplayBuf);
}

帮助赞赏:)

0 个答案:

没有答案