2

我正在 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:修复了这个问题,结果我没有将正确的句柄传递给GetMonitorBrightness。

bSuccess = GetPhysicalMonitorsFromHMONITOR(hMonitor, cPhysicalMonitors, pPhysicalMonitors);
HANDLE pmh = pPhysicalMonitors[0].hPhysicalMonitor; //<---------------  
bSuccess = GetMonitorBrightness(pmh, pdwMinimumBrightness, pdwCurrentBrightness, pdwMaximumBrightness);

添加上面的标记线,解决了这个问题

4

2 回答 2

1

#pragma comment(lib,"Dxva2.lib")在 . 旁边添加#include file

于 2018-04-07T15:48:20.027 回答
0

您没有检查MonitorFromWindow. 如果没有找到监视器,它会返回NULL,因为你已经通过了MONITOR_DEFAULTTONULL。Null 不是监视器句柄。

尝试MONITOR_DEFAULTTONEARESTMONITOR_DEFAULTTOPRIMARY

于 2013-01-08T15:08:52.687 回答