我试图使用以下代码获取主监视器的亮度:
POINT monitorPoint = { 0, 0 };
HANDLE monitor = MonitorFromPoint(monitorPoint, MONITOR_DEFAULTTOPRIMARY);
DWORD minb, maxb, currb;
if (GetMonitorBrightness(monitor, &minb, &currb, &maxb) == FALSE) {
std::cout << GetLastError() << std::endl;
}
但是它失败了,GetLastError()
返回87
,意思是Invalid Parameter
。
编辑:I使用EnumDisplayMonitors()
和GetPhysicalMonitorsFromHMONITOR()
解决了这个问题,如下所示:
std::vector<HANDLE> pMonitors;
BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) {
DWORD npm;
GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, &npm);
PHYSICAL_MONITOR *pPhysicalMonitorArray = new PHYSICAL_MONITOR[npm];
GetPhysicalMonitorsFromHMONITOR(hMonitor, npm, pPhysicalMonitorArray);
for (unsigned int j = 0; j < npm; ++j) {
pMonitors.push_back(pPhysicalMonitorArray[j].hPhysicalMonitor);
}
delete pPhysicalMonitorArray;
return TRUE;
}
// and later inside main simply:
EnumDisplayMonitors(NULL, NULL, MonitorEnumProc, NULL);
// and when I need to change the brightness:
for (unsigned int i = 0; i < pMonitors.size(); ++i) {
SetMonitorBrightness(pMonitors.at(i), newValue);
}
现在我遇到了两个新问题:
1)从EnumDisplayMonitors()
获得2个监视器句柄,因为我有2个监视器。问题是只有我的主要作品。每当我尝试使用辅助监视器时,我都会得到以下错误:
0xC0262582: ERROR_GRAPHICS_I2C_ERROR_TRANSMITTING_DATA
2)在使用SetMonitorBrightness()
一段时间后,即使对主监视器也停止工作,我得到以下错误:
0xC026258D
发布于 2016-11-23 00:19:13
您正在将一个HMONITOR
传递给函数。但是,它的文档声明物理监视器的句柄是必需的,并建议您调用GetPhysicalMonitorsFromHMONITOR()
来获得它。实际上,由于MonitorFromPoint()
返回一个HMONITOR
,您的代码将无法在启用STRICT
的情况下编译,这一实践有助于消除此类错误。
您应该包括对MonitorFromPoint()
调用的错误检查。文档还建议您调用传递GetMonitorCapabilities()
的MC_CAPS_BRIGHTNESS
,以确保监视器支持亮度请求。
有关更多细节,请参阅GetMonitorBrightness()
文档:
https://stackoverflow.com/questions/40753919
复制相似问题