Solved

Available Code Version??

Posted on 2009-05-14
1
278 Views
Last Modified: 2013-11-05
Hi there,

Please see this code attached below
http://www.nirsoft.net/vc/change_screen_brightness.html
is there a Delphi version of this?
0
Comment
Question by:_Nameless_
1 Comment
 
LVL 1

Accepted Solution

by:
KillerCode earned 125 total points
ID: 24392474
unit GammaRamp;
// translated from a c++ source on
// http://www.nirsoft.net/vc/change_screen_brightness.html
// translation by retnyg

interface

uses windows,kol;

CONST NULL = 0;

type

  //LPVOID = ^PVOID;
  LPVOID = Pointer;
  PVOID = array[0..2] of array [0..255] of word;
  Type_SetDeviceGammaRamp = function(DC:HDC; lpRamp:LPVOID):boolean; stdcall;

  PgammaRamp = ^TGammaRamp;
  TGammaRamp = Object(TObj)
  protected
    hGDI32:HMODULE;
    hScreenDC:HDC;
    pGetDeviceGammaRamp,pSetDeviceGammaRamp: Type_SetDeviceGammaRamp;
//    function pGetDeviceGammaRamp(DC:HDC; lpRamp:LPVOID):boolean;
//    function pSetDeviceGammaRamp(DC:HDC; lpRamp:LPVOID):boolean;
    //Type_SetDeviceGammaRamp;

  public
    function LoadLib:boolean;
    procedure FreeLib;
    function LoadLibraryIfNeeded:boolean;
    function SetDeviceGammaRamp(DC:HDC; lpRamp:LPVOID):boolean;
    function GetDeviceGammaRamp(DC:HDC; lpRamp:LPVOID):boolean;
//    function SetDeviceGammaRamp:Type_SetDeviceGammaRamp;
//    function GetDeviceGammaRamp:Type_SetDeviceGammaRamp;
    function SetBrightness(dc:HDC; wBrightness:WORD):boolean;
    destructor Destroy; virtual;
  end;

function NewGammaRamp: PGammaRamp;

implementation

function NewGammaRamp: PGammaRamp;
begin
      //Initialize all variables.
  new(result,create);
  Result.hGDI32 := NULL;
  result.hScreenDC := NULL;
//  @result.pGetDeviceGammaRamp := nil;
//  @result.pSetDeviceGammaRamp := nil;
end;



function TGammaRamp.LoadLib:boolean;
begin
  result := FALSE;
  FreeLib;
  //Load the GDI library.
  hGDI32 := LoadLibrary('gdi32.dll');
  if (hGDI32 <> NULL) then begin

  //Get the addresses of GetDeviceGammaRamp and SetDeviceGammaRamp API functions.
    @pGetDeviceGammaRamp :=GetProcAddress(hGDI32, 'GetDeviceGammaRamp');
    @pSetDeviceGammaRamp :=GetProcAddress(hGDI32, 'SetDeviceGammaRamp');
  //Return TRUE only if these functions exist.
    if (@pGetDeviceGammaRamp = nil) or (@pSetDeviceGammaRamp = nil) then
      FreeLib()
    else result := TRUE;
  end;
end;


procedure TGammaRamp.FreeLib;
begin
  if (hGDI32 <> NULL) then begin
    FreeLibrary(hGDI32);
    hGDI32 := NULL;
  end;
end;

function TGammaRamp.LoadLibraryIfNeeded:boolean;
begin
  result := FALSE;
  if (hGDI32 = NULL) then LoadLib();
  if (@pGetDeviceGammaRamp <> Nil) and (@pSetDeviceGammaRamp <> Nil) then
    result:=true;
end;

function TGammaRamp.SetDeviceGammaRamp(DC:HDC; lpRamp:LPVOID):boolean;
//Call to SetDeviceGammaRamp only if this function is successfully loaded.
begin
  if (LoadLibraryIfNeeded) then
   result:= pSetDeviceGammaRamp(DC, lpRamp)
  else result := FALSE;
end;

function TGammaRamp.GetDeviceGammaRamp(DC:HDC; lpRamp:LPVOID):boolean;
//Call to GetDeviceGammaRamp only if this function is successfully loaded.
begin
  if (LoadLibraryIfNeeded) then result:= pGetDeviceGammaRamp(DC, lpRamp)
  else result := FALSE;
end;


function TGammaRamp.SetBrightness(DC:HDC;wBrightness: WORD):boolean;
{
      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.

}
var hGammaDC : HDC;
    GammaArray: PVOID;
    iIndex, iArrayValue: integer;
begin
  result := FALSE;
  // hdc hgammadc = hDC
  //Load the display device context of the entire screen if hDC is NULL.
  if (DC = NULL) then hGammaDC := GetDC(NULL);
  if (hGammaDC <> NULL) then begin
  //Generate the 256-colors array for the specified wBrightness value.
  //WORD GammaArray[3][256];
     for iIndex := 0 to 255 do begin
       iArrayValue := iIndex * (wBrightness + 128);
       if (iArrayValue > 65535) then
         iArrayValue := 65535;
       GammaArray[0,iIndex] := WORD(iArrayValue);
       GammaArray[1,iIndex] := WORD(iArrayValue);
       GammaArray[2,iIndex] := WORD(iArrayValue);
     end;
    //Set the GammaArray values into the display device context.
    result := SetDeviceGammaRamp(hGammaDC, @GammaArray);
  end;
  if (DC = NULL) then ReleaseDC(NULL, hGammaDC);
end;

end.


///Example Program
program Project1;

{$APPTYPE CONSOLE}

uses
  Kol,
  gammaramp in 'gammaramp.pas';

var
  rampe: PGammaRamp;
  x: integer;
begin
  UseInputOutput;
  writeln('GammaChanger 1.0 :: coded by retnyg ::');
  if paramcount = 0 then begin
    x := 128;
    writeln('Syntax: GammaChange xxx');
    writeln('i.e. GammaChange 64');
    writeln('now setting gamma to 128 (windows default)');
  end
  else begin
    x:=str2int(paramstr(1));
    writeln('setting gamma to ' + paramstr(1));
  end;
  rampe := NewGammaRamp;
  rampe.SetBrightness(0,x);
  rampe.FreeLib;
end.

0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question