Solved

Available Code Version??

Posted on 2009-05-14
1
269 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Windows Drag & Drop Location 2 85
APK file name 7 63
IExtractImage Delphi 14 164
calling procedure inside activexForm from secondform 1 208
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…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now