Share via

Why does GetMonitorBrightness function return false in the following code ?

Anonymous
2016-08-26T05:11:42+00:00

I am creating an app in Qt ( Its a cross platform IDE ) in which i want to add the feature of changing screen brightness but when i call GetMonitorBrightness function it returns false and i don't know why. Maybe my code is not right i don't know please help me to fix this.

And i have only monitor so there is no need of calling GetNumberOFPhyscialMonitors function right ??

My Code :

void EyeCare::on_startcaringButton_clicked()

{

    HMONITOR monitorHandler = NULL ;

    LPDWORD minBrightnessLevel = NULL , maxBrightnessLevel = NULL,

    currentBrightnessLevel = NULL ;

    HWND windowHandler ;

    windowHandler = FindWindow(NULL,NULL) ;

    monitorHandler = MonitorFromWindow(windowHandler, MONITOR_DEFAULTTOPRIMARY) ;

    if (GetMonitorBrightness(monitorHandler,minBrightnessLevel,

                         currentBrightnessLevel, maxBrightnessLevel) ){

        DWORD newBrightnessLevel = (*maxBrightnessLevel) - 50 ;  // my current brightness is 100% 

        SetMonitorBrightness(monitorHandler,newBrightnessLevel) ;

        SaveCurrentMonitorSettings(monitorHandler) ;

        style.append("color: green ;") ;

    }else

        style.append("color: red ;") ;

    ui->startcaringButton->setStyleSheet (style) ;

    QErrorMessage *errorMessage = new QErrorMessage ;

    errorMessage->showMessage(GetLastErrorStdStr());

}

QString GetLastErrorStdStr()

{

  DWORD error = GetLastError();

  if (error)

  {

    LPTSTR errorMessage;

    FormatMessage(

                FORMAT_MESSAGE_FROM_SYSTEM |

                FORMAT_MESSAGE_IGNORE_INSERTS |

                FORMAT_MESSAGE_ARGUMENT_ARRAY |

                FORMAT_MESSAGE_ALLOCATE_BUFFER,NULL,

                error,0,(LPWSTR) &errorMessage,0, NULL );

    return QString :: fromWCharArray(errorMessage) ;

  }

}

My Header Files:

#include <windows.h>

       #include <highlevelmonitorconfigurationapi.h>

My Library Files:


 dxva2.dll ( copied it from "c:/windows/system32" into my project folder's Dll folder )

 dxva2.lib  ( copied it from "c:/windows/system32" into my project folder's Library folder )

this is how you include library in Qt:

win32:CONFIG(release,debug|release): LIBS += -L&&PWD/Dll/ -ldxva2

win32:CONFIG(release, debug|release): LIBS += -L$$PWD/Library/ -ldxva2

else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/Library/ -ldxva2

else:unix: LIBS += -L$$PWD/Library/ -ldxva2

INCLUDEPATH += $$PWD/Library

DEPENDPATH += $$PWD/Library
Windows for home | Previous Windows versions | Apps

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

0 additional answers

Sort by: Most helpful