我就先用下面的代码主监视器的亮度:WINAPI C++试图让主显示器的亮度
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
。
编辑:我设法解决这个使用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);
}
现在我遇到了2点新的问题:
1)从EnumDisplayMonitors()
我得到2监控处理,因为我有2台监视器。问题是只有我的主要作品。每当我尝试这样的东西与辅助监视器我得到这个错误:
0xC0262582: ERROR_GRAPHICS_I2C_ERROR_TRANSMITTING_DATA
2)使用SetMonitorBrightness()
一段时间它停止用于主显示器的工作,甚至后,我得到了以下错误:
0xC026258D
你验证'monitor'不是'NULL'或'INVALID_HANDLE_VALUE'? – selbie
您是否调用GetMonitorCapabilities来确认MC_CAPS_BRIGHTNESS标志是否可用? – selbie
'MonitorFromPoint()'返回一个'HMONITOR',而不是'HANDLE'。如果您使用“STRICT”启用编译,则您的代码将无法编译。 –