问题描述
我正在尝试在 Windows 10 机器上设置亮度.显示器似乎不支持 setMonitorBrightness
,并且 setDeviceGammaRamp
会改变伽玛、白点等,所以我尽量不使用它.
I am trying to set the brightness on a Windows 10 machine. The display doesn't seem to support setMonitorBrightness
, and setDeviceGammaRamp
alters the gamma, white point etc, so I would try not to use it.
我正在尝试使用 IOCTL_VIDEO_SET_DISPLAY_BRIGHTNESS
控件使其工作.当我使用 CreateFile()
获取监视器句柄时,我会检查句柄是否无效并且没有问题.但是当我使用 IOCTL_VIDEO_SET_DISPLAY_BRIGHTNESS
调用 DeviceIoControl()
时,我得到 ERROR_INVALID_HANDLE
(错误 6).
I am trying to get this to work using IOCTL_VIDEO_SET_DISPLAY_BRIGHTNESS
control. When I get the monitor handle using CreateFile()
, I check if the handle is invalid and it is fine. But I get ERROR_INVALID_HANDLE
(error 6) when I call DeviceIoControl()
with IOCTL_VIDEO_SET_DISPLAY_BRIGHTNESS
.
typedef struct _DISPLAY_BRIGHTNESS {
UCHAR ucDisplayPolicy;
UCHAR ucACBrightness;
UCHAR ucDCBrightness;
} DISPLAY_BRIGHTNESS, *PDISPLAY_BRIGHTNESS;
DISPLAY_BRIGHTNESS _displayBrightness;
_displayBrightness.ucDisplayPolicy = 0;
_displayBrightness.ucACBrightness = 0; //for testing purposes
_displayBrightness.ucDCBrightness = 0;
DWORD ret = NULL;
OVERLAPPED olp;
DWORD nOutBufferSize = sizeof(_displayBrightness);
HANDLE h = CreateFile(L"\\\\.\\LCD",
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0, NULL);
if (h == INVALID_HANDLE_VALUE) {
//Does not reach here
return false;
}
if (!DeviceIoControl(h, IOCTL_VIDEO_SET_DISPLAY_BRIGHTNESS, (DISPLAY_BRIGHTNESS *)&_displayBrightness, nOutBufferSize, NULL, 0, &ret, &olp))
{
// GetLastError() returns error code 6 - Invalid handle
return false;
}
另外,我应该使用 CreateFile()
来获取监视器句柄,还是我可以调用 MonitorFromWindow(nullptr, MONITOR_DEFAULTTOPRIMARY)
代替?
Also, should I be using CreateFile()
to get the monitor handle, or can I call MonitorFromWindow(nullptr, MONITOR_DEFAULTTOPRIMARY)
instead?
推荐答案
在通话中
DeviceIoControl(h, IOCTL_VIDEO_SET_DISPLAY_BRIGHTNESS,
(DISPLAY_BRIGHTNESS*)&_displayBrightness, nOutBufferSize, NULL, 0, &ret,
&olp)
&olp
指向一个未初始化的 OVERLAPPED 结构.这个结构有一个事件对象(hEvent)的句柄,它保存一个随机值.这是 DeviceIoControl
调用抱怨的无效句柄.
&olp
points to an uninitialized OVERLAPPED structure. This structure has a handle to an event object (hEvent), that holds a random value. This is the invalid handle the DeviceIoControl
call complains about.
由于您没有使用 FILE_FLAG_OVERLAPPED
标志调用 CreateFile
(无论如何这对于显示设备来说确实没有意义),您不需要传递OVERLAPPED
结构.只需传入NULL
,调用就会成功:
Since you aren't calling CreateFile
with a FILE_FLAG_OVERLAPPED
flag (which really wouldn't make sense for a display device anyway) you don't need to pass an OVERLAPPED
structure at all. Simply pass NULL
, and the call will succeed:
DeviceIoControl(h, IOCTL_VIDEO_SET_DISPLAY_BRIGHTNESS,
(DISPLAY_BRIGHTNESS*)&_displayBrightness, nOutBufferSize, NULL, 0, &ret,
NULL)
这篇关于使用 C++ WinAPI 在 Windows 10 上设置亮度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!