首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Winapi c++试图获得主监视器的亮度

Winapi c++试图获得主监视器的亮度
EN

Stack Overflow用户
提问于 2016-11-23 00:05:58
回答 1查看 1.2K关注 0票数 0

我试图使用以下代码获取主监视器的亮度:

代码语言:javascript
运行
复制
    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()解决了这个问题,如下所示:

代码语言:javascript
运行
复制
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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-23 00:19:13

您正在将一个HMONITOR传递给函数。但是,它的文档声明物理监视器的句柄是必需的,并建议您调用GetPhysicalMonitorsFromHMONITOR()来获得它。实际上,由于MonitorFromPoint()返回一个HMONITOR,您的代码将无法在启用STRICT的情况下编译,这一实践有助于消除此类错误。

您应该包括对MonitorFromPoint()调用的错误检查。文档还建议您调用传递GetMonitorCapabilities()MC_CAPS_BRIGHTNESS,以确保监视器支持亮度请求。

有关更多细节,请参阅GetMonitorBrightness()文档:

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40753919

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档