2016-11-23 40 views
0

我就先用下面的代码主监视器的亮度:WINAPI C++试图让主显示器的亮度

POINT monitorPoint = { 0, 0 }; 
    HANDLE monitor = MonitorFromPoint(monitorPoint, MONITOR_DEFAULTTOPRIMARY); 

    DWORD minb, maxb, currb; 
    if (GetMonitorBrightness(monitor, &minb, &currb, &maxb) == FALSE) { 
     std::cout << GetLastError() << std::endl; 
    } 

但失败并GetLastError()回报87这意味着Invalid Parameter

编辑:我设法解决这个使用EnumDisplayMonitors()GetPhysicalMonitorsFromHMONITOR()这样的:

std::vector<HANDLE> pMonitors; 

BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) { 

    DWORD npm; 
    GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, &npm); 
    PHYSICAL_MONITOR *pPhysicalMonitorArray = new PHYSICAL_MONITOR[npm]; 

    GetPhysicalMonitorsFromHMONITOR(hMonitor, npm, pPhysicalMonitorArray); 

    for (unsigned int j = 0; j < npm; ++j) { 
     pMonitors.push_back(pPhysicalMonitorArray[j].hPhysicalMonitor); 
    } 

    delete pPhysicalMonitorArray; 

    return TRUE; 
} 

// and later inside main simply: 
EnumDisplayMonitors(NULL, NULL, MonitorEnumProc, NULL); 

// and when I need to change the brightness: 
for (unsigned int i = 0; i < pMonitors.size(); ++i) { 
    SetMonitorBrightness(pMonitors.at(i), newValue); 
} 

现在我遇到了2点新的问题:

1)从EnumDisplayMonitors()我得到2监控处理,因为我有2台监视器。问题是只有我的主要作品。每当我尝试这样的东西与辅助监视器我得到这个错误:

0xC0262582: ERROR_GRAPHICS_I2C_ERROR_TRANSMITTING_DATA

2)使用SetMonitorBrightness()一段时间它停止用于主显示器的工作,甚至后,我得到了以下错误:

0xC026258D

+0

你验证'monitor'不是'NULL'或'INVALID_HANDLE_VALUE'? – selbie

+0

您是否调用GetMonitorCapabilities来确认MC_CAPS_BRIGHTNESS标志是否可用? – selbie

+0

'MonitorFromPoint()'返回一个'HMONITOR',而不是'HANDLE'。如果您使用“STRICT”启用编译,则您的代码将无法编译。 –

回答

2

您正在向函数传递一个HMONITOR。但是,其文档指出,需要处理物理监视器,而不是建议您致电GetPhysicalMonitorsFromHMONITOR()以获取它。事实上,由于MonitorFromPoint()返回HMONITOR,您的代码将无法在启用STRICT的情况下编译,这种做法有助于消除此类错误。

您应该包含错误检查MonitorFromPoint()的调用。并且文档还建议您应该致电GetMonitorCapabilities()通过MC_CAPS_BRIGHTNESS以确保显示器支持亮度请求。

请参阅的GetMonitorBrightness()的文档更详细:

+0

'GetMonitorCapabilities()'返回错误'ERROR_GRAPHICS_INVALID_PHYSICAL_MONITOR_HANDLE'这意味着我的问题是'HANDLE monitor = MonitorFromPoint(...)'行。你能给我一个例子如何使用'GetPhysicalMonitorsFromHMONITOR'来获得显示器的句柄吗? – DimChtz

+1

您是否看到['GetPhysicalMonitorsFromHMONITOR()'文档](https://msdn.microsoft.com/en-us/library/windows/desktop/dd692950.aspx)中的示例?唯一的区别是它使用'MonitorFromWindow()'而不是'MonitorFromPoint()',但在你的例子中并不重要。 –

+0

@雷米我终于做到了。现在我又遇到了一个问题。在从arduino(串行通信)读取一些读数后,我在while循环中使用'SetMonitorBrightness()'。它工作几分钟,但一段时间后'SetMonitorBrightness()'失败,错误代码为'3223725453'> 15999,我找不到它的意思。 – DimChtz