2016-11-23 57 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