使用C ++ WinAPI

时间:2016-09-01 04:15:06

标签: c++ windows winapi monitor

我正在尝试在Windows 10计算机上设置亮度。显示似乎不支持setMonitorBrightness,而setDeviceGammaRamp会改变伽玛,白点等,所以我会尝试不使用它。

我正在尝试使用IOCTL_VIDEO_SET_DISPLAY_BRIGHTNESS控件来实现此功能。当我使用CreateFile()获取监视器句柄时,我检查句柄是否无效并且没问题。但是当我使用ERROR_INVALID_HANDLE致电DeviceIoControl()时,我得到IOCTL_VIDEO_SET_DISPLAY_BRIGHTNESS(错误6)。

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)

1 个答案:

答案 0 :(得分:1)

在通话中

DeviceIoControl(h, IOCTL_VIDEO_SET_DISPLAY_BRIGHTNESS,
                (DISPLAY_BRIGHTNESS*)&_displayBrightness, nOutBufferSize, NULL, 0, &ret,
                &olp)

&olp指向未初始化的OVERLAPPED结构。此结构具有事件对象( hEvent )的句柄,该句柄包含随机值。这是DeviceIoControl来电抱怨的无效句柄。

由于您没有使用CreateFile标记来呼叫FILE_FLAG_OVERLAPPED(无论如何对于显示设备真的没有意义),您不需要通过OVERLAPPED结构。只需通过NULL,呼叫就会成功:

DeviceIoControl(h, IOCTL_VIDEO_SET_DISPLAY_BRIGHTNESS,
                (DISPLAY_BRIGHTNESS*)&_displayBrightness, nOutBufferSize, NULL, 0, &ret,
                NULL)