我正在尝试创建一个C++控制台应用程序,它将根据屏幕上显示的像素的平均亮度自动调整显示器的亮度.不幸的是,在我进入第二部分之前,我甚至无法使用DXVA库.
这是我当前的代码(我从这里采用了一些代码:如何使用GetMonitorCapabilities和GetMonitorBrightness函数):
#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include <WinUser.h>
#include <physicalmonitorenumerationapi.h>
#include <highlevelmonitorconfigurationapi.h>
#include <strsafe.h>
std::string GetLastErrorAsString()
{
DWORD errorMessageID = ::GetLastError();
if (errorMessageID == 0)
return std::string();
LPSTR messageBuffer = nullptr;
size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL);
std::string message(messageBuffer, size);
LocalFree(messageBuffer);
return message;
}
int main()
{
HWND hWnd = GetDesktopWindow();
HMONITOR hMonitor = MonitorFromWindow(hWnd, MONITOR_DEFAULTTOPRIMARY);
DWORD cPhysicalMonitors = 0;
BOOL bSuccess = GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, &cPhysicalMonitors);
if (!bSuccess) {
std::cout << "An error occured: " << GetLastErrorAsString().c_str() << std::endl;
}
LPPHYSICAL_MONITOR pPhysicalMonitors = (LPPHYSICAL_MONITOR)malloc(cPhysicalMonitors * sizeof(PHYSICAL_MONITOR));
if (bSuccess)
{
if (pPhysicalMonitors != NULL)
{
bSuccess = GetPhysicalMonitorsFromHMONITOR(hMonitor, cPhysicalMonitors, pPhysicalMonitors);
if (!bSuccess) {
std::cout << "An error occured: " << GetLastErrorAsString().c_str() << std::endl;
}
HANDLE hPhysicalMonitor = pPhysicalMonitors[0].hPhysicalMonitor;
DWORD dwMinimumBrightness = 0;
DWORD dwCurrentBrightness = 0;
DWORD dwMaximumBrightness = 0;
DWORD dwMonitorCapabilities = 0;
DWORD dwSupportedColorTemperatures = 0;
bSuccess = GetMonitorCapabilities(hPhysicalMonitor, &dwMonitorCapabilities, &dwSupportedColorTemperatures);
if (bSuccess) {
std::cout << "Capabilities: " << dwMonitorCapabilities << std::endl << "Supported color temperatures: " << dwSupportedColorTemperatures << std::endl;
}
else {
std::cout << "An error occured while getting monitor capabilities: " << GetLastErrorAsString().c_str() << std::endl;
}
bSuccess = GetMonitorBrightness(hPhysicalMonitor, &dwMinimumBrightness, &dwCurrentBrightness, &dwMaximumBrightness);
if (bSuccess) {
std::cout << "Minimum brightness: " << dwMinimumBrightness << std::endl << "Maximum brightness: " << dwMaximumBrightness << std::endl << "Current brightness: " << dwCurrentBrightness << std::endl;
}
else {
std::cout << "An error occured while getting brightness: " << GetLastErrorAsString().c_str() << std::endl;
}
bSuccess = DestroyPhysicalMonitors(cPhysicalMonitors, pPhysicalMonitors);
free(pPhysicalMonitors);
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到的亮度/功能函数的错误是这样的: An operation failed because a DDC/CI message had an invalid value in its command field.
我一直在谷歌搜索几个小时,我还没有找到一些解决我的问题.我有一个AMD显卡,我使用的是内置驱动程序(谷歌联想e545 AMD驱动程序),其中包括Catalyst版本15.7.1,Direct3D 9.14.10.01128和驱动程序版本15.20.1062.1004-150803a1-187674C.
我为这个混乱的问题和缺乏经验而道歉.