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

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

; iIndex++)
        {
            );

)
            {
                iArrayValue = ;
            }

GammaArray[][iIndex] = 
            GammaArray[][iIndex] = 
            GammaArray[][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:
);

//Wait 3 seconds:
);

//Return back to normal:
);

;
}

VC++调节显示器的亮度SetDeviceGammaRamp的更多相关文章

  1. android开发之GestureDetector手势识别(调节音量、亮度、快进和后退)

    写UI布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro ...

  2. js调节图片的亮度

    js调节图片的亮度:(使用CSS3的滤镜) 1.实现点亮图标.熄灭图标的效果 效果图: 页面代码: <!DOCTYPE html> <%@ page language="j ...

  3. ubuntu 设置显示器的亮度

    ubuntu电脑重新启动后,亮度都变成了最亮.似乎也没胡地方可以设置.只好通过写个脚本来做这个事了. # -*- coding: utf-8 -*- import dbus bus = dbus.Se ...

  4. 解码红外遥控信号——使用遥控器的按键来调节LED的亮度

    程序开始时,提示遥控键0~4的代码,然后程序通过设置LED的亮度来对被按下的按钮作出响应,以0关闭LED,1~4提供增加的亮度. 代码如下:(需要使用IRremote库,可在库管理中搜索该库进行下载后 ...

  5. WIN10 困扰多时的屏幕亮度 终于可以调节了-完美 -更新2018年2月28日

    总结:很多问题是自己认知不够造成的,  -- 问题解决在  修复经历二,可直接跳过去看  修复经历二. 首先看你屏幕亮度是集成还是独立显卡决定的(一般是集成),所以下面 修复经历一折腾独立显卡驱动没什 ...

  6. [转发]dsdt解决睡眠唤醒死机

    登录 注册 首页 热门话题 最新发布   简单模式 详细模式 dsdt解决睡眠唤醒死机 Leave a reply 首先,感谢x5115x提供了一个相对比较完整的THINKPAD T410在MAC下的 ...

  7. [转]WIN7服务一些优化方法

    本文转自:http://bbs.cfanclub.net/thread-391985-1-1.html Win7的服务,手动的一般不用管他,有些自动启动的,但对于有些用户来说是完全没用的,可以考虑禁用 ...

  8. win7 服务详解-系统优化

    Adaptive Brightness监视氛围光传感器,以检测氛围光的变化并调节显示器的亮度.如果此服务停止或被禁用,显示器亮度将不根据照明条件进行调节.该服务的默认运行方式是手动,如果你没有使用触摸 ...

  9. vc++ 调用winapi调节屏幕亮度

    !!版权声明:本文为博主原创文章,版权归原文作者和博客园共有,谢绝任何形式的 转载!! 作者:mohist ---- 已经更正文章中错误的地方, 时间: 10/10/2020--------- 自己封 ...

随机推荐

  1. CSS3文本溢出

    text-overflow: text-overflow:clip | ellipsis; clip:剪切多余的文字. ellipsis:文本溢出时显示省略标记. 要实现文本溢出剪切显示省略标记,还需 ...

  2. 利用POI进行Excel的导出

    需求:将用户的违约金信息导出为excel表格格式 步骤 1. 数据库中增加按钮的值(注意上级编号要和页面隐藏域中的相等) DZ内容(页面加载时根据SJBH查询数据库内容,读取DZ字段信息并加载样式及方 ...

  3. Name与x:Name的关系

    小序:   如果想用Google搜包含冒号的内容怎么办?比如我想搜x:Name这个字符串…… 原来,应该是这样——x::Name 这世道,连搜索也要加转义,全民程序员,要不要人活了?   正文:   ...

  4. Linux日知录(常用问题笔记)

    http://blog.csdn.net/yizhu2000/article/details/70688420)序言 日有一知,当有一录,自09年来,工作所需,接触开源平台,对Linux常有涉猎,其间 ...

  5. T-sql for xml path使用

    用法: FOR XML PATH 方法是用于将查询结果集以XML形式展示 sql: p.ContactTypeID,p.ModifiedDate,p.Name from [Person].[Conta ...

  6. C# DateTime的11种构造函数 [Abp 源码分析]十五、自动审计记录 .Net 登陆的时候添加验证码 使用Topshelf开发Windows服务、记录日志 日常杂记——C#验证码 c#_生成图片式验证码 C# 利用SharpZipLib生成压缩包 Sql2012如何将远程服务器数据库及表、表结构、表数据导入本地数据库

    C# DateTime的11种构造函数   别的也不多说没直接贴代码 using System; using System.Collections.Generic; using System.Glob ...

  7. weblogic stuck实验2014-11-14

         以往对weblogic stuck认识是: 1.会造成系统总体慢. 2.在weblogic console中线程监控中会有显示. 3.weblogic使用队列处理线程.隔一段时间会扫描线程队 ...

  8. NOIP2007:纪念品分组

    #include<stdio.h> #include<stdlib.h> int a[100]; int main(){ int i,j,k,m,n,w; scanf(&quo ...

  9. Json数组操作小记 及 JSON对象和字符串之间的相互转换

    [{"productid":"1","sortindex":"2"},{"productid":&q ...

  10. 服务发现与负载均衡 dubbo zk原理

    服务发现与负载均衡 拓展阅读 : dubbo 原理概念图 2016-03-03 杜亦舒 性能与架构 性能与架构 性能与架构 微信号 yogoup 功能介绍 网站性能提升与架构设计 内容整理自文章“实施 ...