我正在Windows中编写一个程序,我想获得电脑显示器的亮度。 我正在使用Windows GetMonitorBrightness函数,但是我有一些麻烦。
这是我的代码到目前为止:
DWORD dw; HMONITOR hMonitor = NULL; DWORD cPhysicalMonitors; LPPHYSICAL_MONITOR pPhysicalMonitors = NULL; LPDWORD pdwMinimumBrightness=NULL; LPDWORD pdwCurrentBrightness=NULL; LPDWORD pdwMaximumBrightness=NULL; HWND hwnd = FindWindow(NULL, NULL); hMonitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONULL); BOOL bSuccess = GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, &cPhysicalMonitors); pPhysicalMonitors = (LPPHYSICAL_MONITOR)malloc(cPhysicalMonitors* sizeof(PHYSICAL_MONITOR)); bSuccess = GetPhysicalMonitorsFromHMONITOR(hMonitor, cPhysicalMonitors, pPhysicalMonitors); bSuccess = GetMonitorBrightness(hMonitor, pdwMinimumBrightness, pdwCurrentBrightness, pdwMaximumBrightness);
我按照http://msdn.microsoft.com/en-us/library/windows/desktop/dd692972%28v=vs.85%29.aspx上的文档编写了这个
但是当我运行这个代码时,我得到一个错误,说“这个函数失败,因为一个无效的监视器句柄被传递给它”。
我看不出我写的代码有什么问题,但我似乎无法弄清楚这个错误的原因。
编辑:我应该提到我在CRT显示器上尝试了这一点
编辑2:解决了这个问题,事实certificate我没有通过适当的句柄GetMonitorBrightness。
bSuccess = GetPhysicalMonitorsFromHMONITOR(hMonitor, cPhysicalMonitors, pPhysicalMonitors); HANDLE pmh = pPhysicalMonitors[0].hPhysicalMonitor; //<--------------- bSuccess = GetMonitorBrightness(pmh, pdwMinimumBrightness, pdwCurrentBrightness, pdwMaximumBrightness);
添加上面的标记行,解决了这个问题
您不检查MonitorFromWindow
的返回值。 如果没有找到监视器,它将返回NULL
因为你已经通过了MONITOR_DEFAULTTONULL
。 空不是监视器句柄。
试试MONITOR_DEFAULTTONEAREST
或MONITOR_DEFAULTTOPRIMARY
。