admin管理员组

文章数量:1619183

前言

	电脑没找到调节亮度在哪里,网上查找了一些资料模仿写了一个简单的,记录一下方便以后使用。

提示:以下是本篇文章正文内容,下面案例可供参考

1.效果展示(尝试了GIF、录屏、截图都没法记录屏幕亮度变化的画面,这里只能用手机录像)

手机拍摄亮度调节


文章目录

  • 前言
    • 1.效果展示(**尝试了GIF、录屏、截图都没法记录屏幕亮度变化的画面,这里只能用手机录像**)
    • 2.CGammaRamp.h
    • 3.CGammaRamp.cpp
    • 4.MainWindow.h
    • 5.MainWindow.cpp
    • 6.gdi32.dll电脑API下载



2.CGammaRamp.h

代码如下(示例):


#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


*/

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


3.CGammaRamp.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


*/

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(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 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;
}


4.MainWindow.h

代码如下(示例):

---
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <windows.h>
#include <QSettings>
#include <QDebug>
#include <QCloseEvent>
#include <QDir>
#include <QTimer>
#include <QFileInfo>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    //设置为开机启动
    void SetBootFromBoot(bool isBoot);
private slots:
    //定义接收的槽函数
    void onSpinValueChanged(int i);

    void on_pushButton_clicked();

    void on_checkBox_clicked();

private:
    Ui::MainWindow *ui;
    void closeEvent(QCloseEvent *event);
};
#endif // MAINWINDOW_H


5.MainWindow.cpp

代码如下(示例):

---
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "gammaramp.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    SetBootFromBoot(true);
    ui->spinBox->setRange(0, 255);
    ui->horizontalSlider->setRange(0, 255);

    //使spinbox和horizontalSlider数据同步
    QObject::connect(ui->spinBox,SIGNAL(valueChanged(int)),ui->horizontalSlider,SLOT(setValue(int)));
    QObject::connect(ui->horizontalSlider,SIGNAL(valueChanged(int)),ui->spinBox,SLOT(setValue(int)));
    setWindowTitle(tr("屏幕亮度工具"));

    //关联槽函数,把spinbox值传到onspinvaluechanged
    QObject::connect(ui->spinBox, SIGNAL(valueChanged(int)), this, SLOT(onSpinValueChanged(int)));

    QSettings *configIniRead    = new QSettings(QString("%1/%2").arg(qApp->applicationDirPath()).arg("Qt5.5.ini"), QSettings::IniFormat);
    int value                   = configIniRead->value("brightness/value").toString().toInt();
    int isChecked               = configIniRead->value("brightness/isChecked").toString().toInt();
    qDebug()<< "value = " << value << "isChecked = " << isChecked;
    ui->horizontalSlider->setValue(value);
    ui->spinBox->setValue(ui->horizontalSlider->value());
    if(isChecked == 1)
    {
        ui->checkBox->setChecked(true);
        QTimer::singleShot(2000, this, [=](){this->close();});
    }
    delete configIniRead;
}

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

//获取spinbox的值
void MainWindow::onSpinValueChanged(int i)
{
    int gamma = i;
    CGammaRamp GammaRamp;
    GammaRamp.SetBrightness(NULL, gamma);
}

//假装还原到屏幕正常亮度,这个还可以写得更好,有兴趣的可以试试
void MainWindow::on_pushButton_clicked()
{
    CGammaRamp GammaRamp;
    GammaRamp.SetBrightness(NULL, ui->horizontalSlider->maximum()/2);//手动赋的值...
    ui->horizontalSlider->setValue(ui->horizontalSlider->maximum()/2);
}

//设置是否开机自启动
void MainWindow::SetBootFromBoot(bool isBoot)
{
    QSettings reg("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat);
    QString strAppPath = QDir::toNativeSeparators(QCoreApplication::applicationFilePath());
    QString strAppName = QFileInfo(strAppPath).baseName();

    #ifdef Q_OS_WIN32
        if(isBoot)
        {
            reg.setValue(strAppName, strAppPath);
            qDebug() << "------------------------------------------------------------------" << "SET";
        }
        else
        {
            reg.remove(strAppName);
            qDebug() << "------------------------------------------------------------------" << "NOTSET";
        }
    #endif
}
void MainWindow::closeEvent(QCloseEvent *event)
{
    QSettings settings(QString("%1/%2").arg(qApp->applicationDirPath()).arg("Qt5.5.ini"), QSettings::IniFormat);
    settings.setValue("brightness/value", ui->horizontalSlider->value());
    QMainWindow::closeEvent(event);
}

void MainWindow::on_checkBox_clicked()
{
    if(ui->checkBox->isChecked())
    {
        QSettings settings(QString("%1/%2").arg(qApp->applicationDirPath()).arg("Qt5.5.ini"), QSettings::IniFormat);
        settings.setValue("brightness/isChecked", 1);
    }
    else
    {
        QSettings settings(QString("%1/%2").arg(qApp->applicationDirPath()).arg("Qt5.5.ini"), QSettings::IniFormat);
        settings.setValue("brightness/isChecked", 0);
    }
}


6.gdi32.dll电脑API下载

gdi32.dll配置文件下载

本文标签: 亮度电脑屏幕QT