如何使用GetMonitorCapabilities和GetMonitorBrightness函数

我正在尝试以编程方式调整我的显示器亮度。 经过一点研究,我想出了这个链接 ,并且写下了下面的代码(主要是复制来自其他链接的粘贴)。

#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, 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); } 

执行此行时,此代码崩溃:

 bSuccess = GetMonitorBrightness(hPhysicalMonitor, pdwMinimumBrightness, pdwCurrentBrightness, pdwMaximumBrightness); 

根据文档,该function可能不被支持。

如果支持该函数,则GetMonitorCapabilities函数将返回MC_CAPS_BRIGHTNESS标志。

所以,为了检查,我在调用GetMonitorBrightness之前将下面的代码块添加到我的代码中。

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

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

在这里输入图像说明

同样,根据文档 ,如果监视器不支持DDC / CI ,则GetMonitorCapabilitiesfunction会失败。

然后,我检查了我的显示器是否支持DDC / CI,并发现它是。 此外,当我从显示器设置手动禁用DDC / CI支持时,之前的错误信息会切换到下一个,所以现在我非常确定我的显示器支持DDC / CI

在这里输入图像说明

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

笔记:

我的显示器是Dell U2713H 。

我在64位Windows 7上。

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

Solutions Collecting From Web of "如何使用GetMonitorCapabilities和GetMonitorBrightness函数"

您调用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);