[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Available Code Version??

Posted on 2009-05-14
1
Medium Priority
?
318 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 500 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month18 days, 1 hour left to enroll

830 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