首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Windows-C++中执行SetMonitorBrightness函数时出现无效监视器句柄错误

在Windows-C++中执行SetMonitorBrightness函数时出现无效监视器句柄错误
EN

Stack Overflow用户
提问于 2013-01-08 11:48:33
回答 2查看 2.4K关注 0票数 2

我正在用Windows编写一个程序,我想要获得计算机监视器的亮度。我正在使用GetMonitorBrightness函数,但是我遇到了一些问题。

到目前为止,这是我的代码:

代码语言:javascript
运行
复制
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传递一个适当的句柄。

代码语言:javascript
运行
复制
bSuccess = GetPhysicalMonitorsFromHMONITOR(hMonitor, cPhysicalMonitors, pPhysicalMonitors);
HANDLE pmh = pPhysicalMonitors[0].hPhysicalMonitor; //<---------------  
bSuccess = GetMonitorBrightness(pmh, pdwMinimumBrightness, pdwCurrentBrightness, pdwMaximumBrightness);

加上上面的标记行,解决了这个问题。

EN

回答 2

Stack Overflow用户

发布于 2018-04-07 15:48:20

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

票数 1
EN

Stack Overflow用户

发布于 2013-01-08 15:08:52

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

试试MONITOR_DEFAULTTONEARESTMONITOR_DEFAULTTOPRIMARY

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

https://stackoverflow.com/questions/14214290

复制
相关文章

相似问题

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