我正在用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:修复了这个问题,结果发现我没有给GetMonitorBrightness传递一个适当的句柄。
bSuccess = GetPhysicalMonitorsFromHMONITOR(hMonitor, cPhysicalMonitors, pPhysicalMonitors);
HANDLE pmh = pPhysicalMonitors[0].hPhysicalMonitor; //<---------------
bSuccess = GetMonitorBrightness(pmh, pdwMinimumBrightness, pdwCurrentBrightness, pdwMaximumBrightness);
加上上面的标记行,解决了这个问题。
发布于 2018-04-07 15:48:20
将#pragma comment(lib,"Dxva2.lib")
添加到#include file
旁边。
发布于 2013-01-08 15:08:52
您没有检查MonitorFromWindow
的返回值。如果找不到监视器,它将返回NULL
,因为您已经通过了MONITOR_DEFAULTTONULL
。Null不是监视器句柄。
试试MONITOR_DEFAULTTONEAREST
或MONITOR_DEFAULTTOPRIMARY
。
https://stackoverflow.com/questions/14214290
复制相似问题