VC++调节显示器的亮度SetDeviceGammaRamp

出处:http://www.nirsoft.net/vc/change_screen_brightness.html

SetDeviceGammaRamp API函数位于Gdi32.ll中,接收一个256*3 RGB值的数组。
增加这个数组中的值会使屏幕更亮,而减少这些值会使屏幕变暗。
可以通过增加或减少的红/绿/蓝成分的值来显示影响。例如:在所有RGB值中增加蓝色分量将增加整个显示的蓝色。
下面的类封装调用GetDeviceGammaRampSetDeviceGammaRamp,并设置屏幕的亮度,提供setbrightness功能。

 

 

 C++ GammaRamp.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 
#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_GetDeviceGammaRamp)(HDC hDC, LPVOID lpRamp);
    
typedef  BOOL (WINAPI *Type_SetDeviceGammaRamp)(HDC hDC, LPVOID lpRamp);

    Type_GetDeviceGammaRamp 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 //#ifndef GAMMARAMP_H_

 

 C++ GammaRamp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
 
#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_GetDeviceGammaRamp)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;
}

 

 C++ Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 
#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 ;
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值