Learnerz Planet
What's the benefits for being a member in LZPT?
•Has full access to other forums.
•Can Post/reply topics and discussions.
•Can create topics and discussions/poll.
•Can read topics, codes, URLS and download links.
•Learn something from someone(educationally).
•Share something for others to learn too.


Register and get access! in less than 2 minutes
REGISTER NOW!

Learnerz Planet

Learnerz Planet
 
HomeSearchMemberlistRegisterLog in
Welcome Please Enjoy your stay with our new domain name www.LearnerzPlanet.net

Share | 
 

 Changing the screen brightness programingly

View previous topic View next topic Go down 
AuthorMessage
Xeroxis Vx Labz
M O D E R A T O R
M O D E R A T O R
avatar

Posts : 56
Reputation : 37333
Join date : 2010-11-07
Age : 27

PostSubject: Changing the screen brightness programingly   Tue Nov 16, 2010 12:20 pm

Some video cards allows you to programmingly modify the Gamma Ramp values. You can use this feature to change the brightness of the entire screen.

Code:
#ifndef GAMMARAMP_H_
#define GAMMARAMP_H_


/*
CGammaRamp class

Encapsulates the Gamma Ramp API and changes the brightness of
the entire screen.


*/

class CGammaRamp
{
protected:
   HMODULE hGDI32;
   HDC hScreenDC;
   typedef BOOL (WINAPI *Type_SetDeviceGammaRamp)(HDC hDC, LPVOID lpRamp);

   Type_SetDeviceGammaRamp pGetDeviceGammaRamp;
   Type_SetDeviceGammaRamp pSetDeviceGammaRamp;

public:

   CGammaRamp();
   ~CGammaRamp();
   BOOL LoadLibrary();
   void FreeLibrary();
   BOOL LoadLibraryIfNeeded();
   BOOL SetDeviceGammaRamp(HDC hDC, LPVOID lpRamp);
   BOOL GetDeviceGammaRamp(HDC hDC, LPVOID lpRamp);
   BOOL SetBrightness(HDC hDC, WORD wBrightness);

};
#endif

Class C++ File: (gammaramp.cpp)

#include <windows.h>
#include "gammaramp.h"


/*
CGammaRamp class

Encapsulates the Gamma Ramp API and changes the brightness of
the entire screen.




*/

CGammaRamp::CGammaRamp()
{
   //Initialize all variables.
   hGDI32 = NULL;
   hScreenDC = NULL;
   pGetDeviceGammaRamp = NULL;
   pSetDeviceGammaRamp = NULL;
}

CGammaRamp::~CGammaRamp()
{
   FreeLibrary();
}


BOOL CGammaRamp::LoadLibrary()
{
   BOOL bReturn = FALSE;

   FreeLibrary();
   //Load the GDI library.
   hGDI32 = ::LoadLibrary("gdi32.dll");
   if (hGDI32 != NULL)
   {
      //Get the addresses of GetDeviceGammaRamp and SetDeviceGammaRamp API functions.
      pGetDeviceGammaRamp =
         (Type_SetDeviceGammaRamp)GetProcAddress(hGDI32, "GetDeviceGammaRamp");
      
      pSetDeviceGammaRamp =
         (Type_SetDeviceGammaRamp)GetProcAddress(hGDI32, "SetDeviceGammaRamp");
      
      //Return TRUE only if these functions exist.
      if (pGetDeviceGammaRamp == NULL || pSetDeviceGammaRamp == NULL)
         FreeLibrary();
      else
         bReturn = TRUE;
   }

   return bReturn;
}


void CGammaRamp::FreeLibrary()
{
   //Free the GDI library.
   if (hGDI32 != NULL)
   {
      ::FreeLibrary(hGDI32);
      hGDI32 = NULL;
   }
}


BOOL CGammaRamp::LoadLibraryIfNeeded()
{
   BOOL bReturn = FALSE;

   if (hGDI32 == NULL)
      LoadLibrary();

   if (pGetDeviceGammaRamp != NULL && pSetDeviceGammaRamp != NULL)
      bReturn = TRUE;

   return bReturn;
}


BOOL CGammaRamp::SetDeviceGammaRamp(HDC hDC, LPVOID lpRamp)
{
   //Call to SetDeviceGammaRamp only if this function is successfully loaded.
   if (LoadLibraryIfNeeded())
   {
      return pSetDeviceGammaRamp(hDC, lpRamp);
   }
   else
      return FALSE;
}


BOOL CGammaRamp::GetDeviceGammaRamp(HDC hDC, LPVOID lpRamp)
{
   //Call to GetDeviceGammaRamp only if this function is successfully loaded.
   if (LoadLibraryIfNeeded())
   {
      return pGetDeviceGammaRamp(hDC, lpRamp);
   }
   else
      return FALSE;

}


BOOL CGammaRamp::SetBrightness(HDC hDC, WORD wBrightness)
{
   /*
   Changes the brightness of the entire screen.
   This function may not work properly in some video cards.

   The wBrightness value should be a number between 0 and 255.
   128 = Regular brightness
   above 128 = brighter
   below 128 = darker

    If hDC is NULL, SetBrightness automatically load and release
   the display device context for you.

   */
   BOOL bReturn = FALSE;
   HDC hGammaDC = hDC;

   //Load the display device context of the entire screen if hDC is NULL.
   if (hDC == NULL)
      hGammaDC = GetDC(NULL);

   if (hGammaDC != NULL)
   {
      //Generate the 256-colors array for the specified wBrightness value.
      WORD GammaArray[3][256];

      for (int iIndex = 0; iIndex < 256; iIndex++)
      {
         int iArrayValue = iIndex * (wBrightness + 128);

         if (iArrayValue > 65535)
            iArrayValue = 65535;

         GammaArray[0][iIndex] =
         GammaArray[1][iIndex] =
         GammaArray[2][iIndex] = (WORD)iArrayValue;
         
      }

      //Set the GammaArray values into the display device context.
      bReturn = SetDeviceGammaRamp(hGammaDC, GammaArray);
   }

   if (hDC == NULL)
      ReleaseDC(NULL, hGammaDC);

   return bReturn;
}

Example for using the CGammaRamp class:

#include <windows.h>
#include "gammaramp.h"


int WINAPI WinMain(
  HINSTANCE hInstance,      // handle to current instance
  HINSTANCE hPrevInstance,  // handle to previous instance
  LPSTR lpCmdLine,          // command line
  int nCmdShow              // show state
)
{
   //Example for changing the brightness with CGammaRamp class:
   //Be aware that this exmaple may not work properly in all
   //Video cards.

   CGammaRamp GammaRamp;

   //Make the screen darker:
   GammaRamp.SetBrightness(NULL, 64);

   //Wait 3 seconds:
   Sleep(3000);

   //Return back to normal:
   GammaRamp.SetBrightness(NULL, 128);

   return 0;
}

Back to top Go down
View user profile
squatron
M O D E R A T O R
M O D E R A T O R
avatar

Posts : 63
Reputation : 37889
Join date : 2010-09-29

PostSubject: Re: Changing the screen brightness programingly   Sat Nov 20, 2010 10:50 pm

And this is Visual C++. Smile

_________________

Back to top Go down
View user profile
Xeroxis Vx Labz
M O D E R A T O R
M O D E R A T O R
avatar

Posts : 56
Reputation : 37333
Join date : 2010-11-07
Age : 27

PostSubject: Re: Changing the screen brightness programingly   Sat Nov 20, 2010 11:10 pm

indeed Embarassed
Back to top Go down
View user profile
squatron
M O D E R A T O R
M O D E R A T O R
avatar

Posts : 63
Reputation : 37889
Join date : 2010-09-29

PostSubject: Re: Changing the screen brightness programingly   Sat Nov 20, 2010 11:16 pm

(.cpp) LoL I think oyu need a SDK For this!

_________________

Back to top Go down
View user profile
Xeroxis Vx Labz
M O D E R A T O R
M O D E R A T O R
avatar

Posts : 56
Reputation : 37333
Join date : 2010-11-07
Age : 27

PostSubject: Re: Changing the screen brightness programingly   Sun Nov 21, 2010 5:35 am

tested it in VC++ 6.0 but never tried on 2010 Embarassed lol! i like this smiley
Back to top Go down
View user profile
Sponsored content




PostSubject: Re: Changing the screen brightness programingly   

Back to top Go down
 
Changing the screen brightness programingly
View previous topic View next topic Back to top 
Page 1 of 1
 Similar topics
-
» Effect of changing coupling cap value
» Changing the Rules? Yes or No?
» changing the color tubes emit
» Changing the colour of unglazed pots
» Map Screen Shot (RMXP)

Permissions in this forum:You cannot reply to topics in this forum
Learnerz Planet :: Computer Talk :: Visual C++-
Jump to: