setDeviceGammaRamp调节 gamma ramp用法

C++代码: CSDN版本:

 
代码: API函数原形如下():
BOOL WINAPI GetDeviceGammaRamp(
  HDC hDC,
  LPVOID lpRamp
); BOOL WINAPI SetDeviceGammaRamp(  HDC hDC,  LPVOID lpRamp);其中lpRamp指向的是一个3x256的WORD型数组,内部存放的是需要Gamma校正的梯度,范例如下: void * lpGamma = NULL;
WORD gMap[3][256] = {0};
lpGamma = &gMap;
HDC hdc = ::GetDC(NULL);
::GetDeviceGammaRamp(hdc, lpGamma);    //得到当前Gamma
for (INT i = 0; i < 256; i++)
{
  gMap[0] = 256*i;
        gMap[1] = 256*i;
        gMap[2] = 256*i;
}
::SetDeviceGammaRamp(hdc, lpGamma);    //设为标准Gamma

C++代码: nirsoft版本:

 

代码:
#ifndef GAMMARAMP_H_
#define GAMMARAMP_H_


/*
CGammaRamp class

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

Written by Nir Sofer.
http://www.nirsoft.net


*/

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.

Written by Nir Sofer.
http://www.nirsoft.net


*/

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;
}
VB6:
代码:
Option Explicit
'Gamma Type
Private Type Gamma
    Red As Integer
    Green As Integer
    Blue As Integer
End Type
Private GammaDay As Gamma                'Day Gamma
Private GammaNight As Gamma              'Night Gamma

'Gamma APIs
Private Ramp1(0 To 255, 0 To 2) As Integer
Private Ramp2(0 To 255, 0 To 2) As Integer
Private Declare Function GetDeviceGammaRamp Lib "gdi32" (ByVal hdc As Long, lpv As Any) As Long
Private Declare Function SetDeviceGammaRamp Lib "gdi32" (ByVal hdc As Long, lpv As Any) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long

'Set Gamma
Public Sub SetGamma(ByVal intRed As Integer, ByVal intGreen As Integer, ByVal intBlue As Integer)
Dim i As Integer
Dim ScrDC As Long
   
    'Get Screen's DC
    ScrDC = GetDC(GetDesktopWindow)
    intRed = intRed / 2
    intGreen = intGreen / 2
    intBlue = intBlue / 2
    'Change Ramp
    For i = 0 To 255
        If intRed < 0 Then Ramp2(i, 0) = ConvToSignedValue(ConvToUnSignedValue(Ramp1(i, 0)) * (100 - Abs(intRed)) / 100)
        If intRed = 0 Then Ramp2(i, 0) = Ramp1(i, 0)
        If intRed > 0 Then Ramp2(i, 0) = ConvToSignedValue(65535 - ((65535 - ConvToUnSignedValue(Ramp1(i, 0))) * (100 - intRed) / 100))
        If intGreen < 0 Then Ramp2(i, 1) = ConvToSignedValue(ConvToUnSignedValue(Ramp1(i, 1)) * (100 - Abs(intGreen)) / 100)
        If intGreen = 0 Then Ramp2(i, 1) = Ramp1(i, 1)
        If intGreen > 0 Then Ramp2(i, 1) = ConvToSignedValue(65535 - ((65535 - ConvToUnSignedValue(Ramp1(i, 1))) * (100 - intGreen) / 100))
        If intBlue < 0 Then Ramp2(i, 2) = ConvToSignedValue(ConvToUnSignedValue(Ramp1(i, 2)) * (100 - Abs(intBlue)) / 100)
        If intBlue = 0 Then Ramp2(i, 2) = Ramp1(i, 2)
        If intBlue > 0 Then Ramp2(i, 2) = ConvToSignedValue(65535 - ((65535 - ConvToUnSignedValue(Ramp1(i, 2))) * (100 - intBlue) / 100))
    Next
    'Set Gamma
    SetDeviceGammaRamp ScrDC, Ramp2(0, 0)
End Sub
'Save Gamma
Public Sub SaveGamma()
    Dim ScrDC As Long
   
    'Get Screen's DC
    ScrDC = GetDC(GetDesktopWindow)
   
    'Reset it
    GetDeviceGammaRamp ScrDC, Ramp1(0, 0)
End Sub
'RestoreGamma
Public Sub RestoreGamma()
    Dim ScrDC As Long
   
    'Get Screen's DC
    ScrDC = GetDC(GetDesktopWindow)
   
    'Reset it
    SetDeviceGammaRamp ScrDC, Ramp1(0, 0)
End Sub
Private Function ConvToSignedValue(lngValue As Long) As Integer
    'Cheezy method for converting to signed integer
    If lngValue <= 32767 Then
        ConvToSignedValue = CInt(lngValue)
        Exit Function
    End If
   
    ConvToSignedValue = CInt(lngValue - 65535)
End Function
Private Function ConvToUnSignedValue(intValue As Integer) As Long
    'Cheezy method for converting to unsigned integer
    If intValue >= 0 Then
        ConvToUnSignedValue = intValue
        Exit Function
    End If
   
    ConvToUnSignedValue = intValue + 65535
End Function
Private Sub Command1_Click()
    GammaDay.Red = 0
    GammaDay.Green = 0
    GammaDay.Blue = 0
    SetGamma GammaDay.Red, GammaDay.Green, GammaDay.Blue
End Sub
Private Sub Command2_Click()
    GammaNight.Red = -64
    GammaNight.Green = -64
    GammaNight.Blue = -64
    SetGamma GammaNight.Red, GammaNight.Green, GammaNight.Blue
End Sub
Private Sub Form_Load()
    SaveGamma
End Sub
Private Sub Form_Unload(Cancel As Integer)
    RestoreGamma
End Sub
附件为通过nirsoft版本生成的EXE及VS2008工程:
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值