Adjust display gamma value
I am looking into modifying brightness/contrast/gamma of my display i found an api whose purpose i think is this but i didn't had much success implementing it... here is the code
var
i,j:Integer;
buf:array [0..2,0..255] of Word;
wBright:Word;
myDC:HDC;
begin
myDC:=GetDc(GetDesktopWindow);
GetDeviceGammaRamp(mydc,buf);
for i:=0 to 2 do
for j:=0 to 255 do
begin
buf[i][j]:=buf[i][j] + 100; //if i don't modify the values the api works
end;
SetDeviceGammaRamp(mydc,buf);
end;
I will be grateful if you point me into right direction. Thanks.
The last error says : The parameter is i开发者_如何学编程ncorrect
The values in the array must really be a ramp, i.e. they map the possible R, G and B values to a brightness value. This way you can create funny effects too, but not with the routine below. Found something like this on the web:
uses Windows;
// SetDisplayBrightness
//
// Changes the brightness of the entire screen.
// This function may not work properly in some video cards.
//
// The Brightness parameter has the following meaning:
//
// 128 = normal brightness
// above 128 = brighter
// below 128 = darker
function SetDisplayBrightness(Brightness: Byte): Boolean;
var
GammaDC: HDC;
GammaArray: array[0..2, 0..255] of Word;
I, Value: Integer;
begin
Result := False;
GammaDC := GetDC(0);
if GammaDC <> 0 then
begin
for I := 0 to 255 do
begin
Value := I * (Brightness + 128);
if Value > 65535 then
Value := 65535;
GammaArray[0, I] := Value; // R value of I is mapped to brightness of Value
GammaArray[1, I] := Value; // G value of I is mapped to brightness of Value
GammaArray[2, I] := Value; // B value of I is mapped to brightness of Value
end;
// Note: BOOL will be converted to Boolean here.
Result := SetDeviceGammaRamp(GammaDC, GammaArray);
ReleaseDC(0, GammaDC);
end;
end;
Unfortunately, in my Win7 VM in Parallels on a Mac, I can't test his, but it should work on most normal Windows PCs.
Edit
FWIW, I ran it in my Win7 VM and the routine returns True
. If I use other values, e.g.
Value := 127 * I;
the routine returns False
and
ShowMessage(SysErrorMessage(GetLastError));
displays
The parameter is incorrect
Changing this to:
Value := 128 * I;
returns True
again. I assume the values must form some kind of slope (or ramp). This routine creates a linear ramp. I guess you can also use other kinds, e.g. a sigmoid, to achieve other effects, like higher contrast.
I can't, of course, see any differences in brightness in the VM, sorry.
Update: But it seems to work for David Heffernan and I could just test it on my sister in law's laptop, and there it works too.
精彩评论