当前位置: 首页 > article >正文

qt通过调节伽马值来调节显示器亮度

代码是参考的别人 来源博客的,自己做了一点改动
头文件

#ifndef CGAMARAMP_H
#define CGAMARAMP_H
#include <windows.h>
class CGamaRamp
{
public:
    CGamaRamp();
    ~CGamaRamp();

protected:
    HMODULE hGDI32;
    HDC hScreenDC;
    typedef BOOL (WINAPI *Type_SetDeviceGammaRamp)(HDC hDC, LPVOID lpRamp);

    Type_SetDeviceGammaRamp pGetDeviceGammaRamp;
    Type_SetDeviceGammaRamp pSetDeviceGammaRamp;

public:

    BOOL LoadLibrary();

    void FreeLibrary();

    BOOL LoadLibraryIfNeeded();

    BOOL SetDeviceGammaRamp(HDC hDC, LPVOID lpRamp);

    BOOL GetDeviceGammaRamp(HDC hDC, LPVOID lpRamp);

    BOOL SetBrightness(HDC hDC, WORD wBrightness);

    int getGammaValue();
};

#endif // CGAMARAMP_H

源文件

#include "cgamaramp.h"

CGamaRamp::CGamaRamp()
{
    //Initialize all variables.
    hGDI32 = NULL;
    hScreenDC = NULL;
    pGetDeviceGammaRamp = NULL;
    pSetDeviceGammaRamp = NULL;
}

CGamaRamp::~CGamaRamp()
{
    FreeLibrary();
}

BOOL CGamaRamp::LoadLibrary()
{
    BOOL bReturn = FALSE;

    FreeLibrary();
    //Load the GDI library.
    hGDI32 = ::LoadLibrary(TEXT("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 CGamaRamp::FreeLibrary()
{
    //Free the GDI library.
    if (hGDI32 != NULL)
    {
        ::FreeLibrary(hGDI32);
        hGDI32 = NULL;
    }
}


BOOL CGamaRamp::LoadLibraryIfNeeded()
{
    BOOL bReturn = FALSE;

    if (hGDI32 == NULL)
        LoadLibrary();

    if (pGetDeviceGammaRamp != NULL && pSetDeviceGammaRamp != NULL)
        bReturn = TRUE;

    return bReturn;
}


BOOL CGamaRamp::SetDeviceGammaRamp(HDC hDC, LPVOID lpRamp)
{
    //Call to SetDeviceGammaRamp only if this function is successfully loaded.
    if (hGDI32!= NULL)
    {
        return pSetDeviceGammaRamp(hDC, lpRamp);
    }
    else
        return FALSE;
}


BOOL CGamaRamp::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 CGamaRamp::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;
}

int CGamaRamp::getGammaValue()
{
    // 获取默认显示器的设备上下文
    HDC hdc = GetDC(NULL);
    if (hdc)
    {
        // 存储伽马曲线
        WORD gammaRamp[256][3]; // 256 个级别,每个级别有 R、G 和 B 分量
        // 获取当前的伽马 Ramp
        if (GetDeviceGammaRamp(hdc, gammaRamp))
        {
            // 输出伽马值
            for (int i = 0; i < 256; i++)
            {
                if(i==255)
                {
                    return  gammaRamp[i][2] /255 -128;
                }
            }
        }
        else
        {
            return -1;
        }
    }
}

界面源文件

#include "widget.h"
#include "ui_widget.h"
#include <Windows.h>
#include<QDebug>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    ui->horizontalSlider->setRange(1, 255);
    QObject::connect(ui->horizontalSlider,SIGNAL(valueChanged(int)),this,SLOT(onSpinValueChanged(int)));
    setWindowTitle(tr("屏幕亮度工具"));
   hDC = GetDC(NULL);
   GammaRamp.LoadLibraryIfNeeded();
   int value = GammaRamp.getGammaValue();
   ui->horizontalSlider->setValue(value);

}

Widget::~Widget()
{
    delete ui;
}



void Widget::onSpinValueChanged(int i)
{
    ui->label_2->setText(QString::number(i));
    int gamma = i;
    GammaRamp.SetBrightness(hDC, gamma);

}

总结找个方法其实不是很好用,对于有的显示器,会被自动校准回去。如果想通过其它方式实现调节显示器亮度参见我的文章使用WMI改变显示器亮度


http://www.kler.cn/a/299101.html

相关文章:

  • ViEW生命周期
  • Unity3D仿星露谷物语开发7之事件创建动画
  • HTTP—02
  • 设计模式--单例模式【创建型模式】
  • Vue与React:前端框架的巅峰对决
  • 【计算机视觉基础CV】03-深度学习图像分类实战:鲜花数据集加载与预处理详解
  • oracle19.3单机升级到Oracle19.22
  • 主流无线物联网通信技术有哪些
  • 《一个操作系统的实现》--- ubuntu下bochs2.3.5的配置与使用
  • 大宗商品价格在二十多年中的时间序列变化趋势数据分析
  • 计算机毕业设计 家校互联管理系统 Java+SpringBoot+Vue 前后端分离 文档报告 代码讲解 安装调试
  • 硬件工程师笔试面试——IGBT
  • 数学基础 -- 勒让德多项式之矩阵与内积
  • 分布式技术概览
  • Netty权威指南:Netty总结-服务端创建
  • MySQL聚合统计:性能优化与高级应用
  • C++学习笔记(16)
  • 【C#Mutex】 initiallyOwned错误引起的缺陷
  • JAVA进阶学习15
  • pnpm解說
  • Selenium 实现图片验证码识别
  • 在VB.net中,TimeSpan有什么属性与方法
  • docker 构建最小镜像 - 2MB 不到
  • [Windows] MinGW 与 MSYS2
  • 基于STM32设计的水闸水文测控系统(华为云IOT)(220)
  • Android 系统级应用守护进程