! ! Copyright statement: This article is an original articleby the blogger. The copyright belongs to the original author and the blog garden. Any form of reprinting is strictly prohibited! !
Author: mohist
—- The errors in the article have been corrected. Time: 10/10/2020———
I encapsulated a class myself for easy use in the future.
I completed the test on win7 and found that it works. The blog post was written on a Mac. There may be errors in the code. Please forgive me and I will put pictures.
1. Include header files
#include <physicalmonitorenumerationapi.h> #include <highlevelmonitorconfigurationapi.h>
2. Include system lib library files
#pragma comment(lib, "Dxva2.lib")
3. .h
#pragma once // #include <physicalmonitorenumerationapi.h> #include <highlevelmonitorconfigurationapi.h> class pm_monitor_brightness { public: pm_monitor_brightness(void); virtual ~pm_monitor_brightness(); /* @brief: Increase brightness @param: const unsigned int offset - step size, the maximum is 100, if it exceeds 100, the brightness is set to the maximum @return: int 0 - Success 1 - Failed, setup failed */ int increase(const unsigned int offset = 10); /* @brief: Reduce brightness @param: const unsigned int offset - step size, maximum is 100, if it exceeds 100, set the brightness to low @return: int 0 - Success 1 - Failed, setup failed */ int decrease(const unsigned int offset = 10); private: //Set screen brightness void set_monitor_brightness(const unsigned int bright); void init(); private: enum { max_offset_100 = 100, }; struct pm_brightness_val { //Current brightness value int _cur; //Display maximum brightness int _max; //Minimum value of display brightness int _min; void zero() { memset(this, 0, sizeof(pm_brightness_val)); } pm_brightness_val() { zero(); } }; // BOOL _is_init_success; HANDLE _handle_cur_monitor; HMONITOR_monitor; DWORD _physical_monitor_number; LPPHYSICAL_MONITOR _physical_monitor; // pm_brightness_val _brightness_val; };
4. .cpp
#include "pm_monitor_brightness.h" pm_monitor_brightness::pm_monitor_brightness() : _is_init_success(FALSE) , _handle_cur_monitor(NULL) , _monitor(NULL) , _physical_monitor_number(0) , _physical_monitor(NULL) { init(); } pm_monitor_brightness::~pm_monitor_brightness() { if (NULL != _physical_monitor) DestroyPhysicalMonitors(_physical_monitor_number, _physical_monitor); if (NULL != _physical_monitor) { free(_physical_monitor); _physical_monitor = NULL; } } int pm_monitor_brightness::increase(const unsigned int offset/*=10*/) { pm_brightness_val & amp;param = _brightness_val; // No initialization successful if (!_is_init_success) return 1; // The maximum value has been reached if (param._cur == param._max) return 0; param._cur = (max_offset_100 == offset) ? param._max : (param._cur + offset); if (param._cur > param._max) param._cur = param._max; set_monitor_brightness(param._cur); return 0; } int pm_monitor_brightness::decrease(const unsigned int offset/* = 10 */) { pm_brightness_val & amp;param = _brightness_val; // No initialization successful if (!_is_init_success) return 1; // The maximum value has been reached if (param._cur == param._min) return 0; param._cur = (max_offset_100 == offset) ? param._max : (param._cur - offset); if (param._cur < param._min) param._cur = param._min; set_monitor_brightness(param._cur); return 0; } //Set screen brightness void pm_monitor_brightness::set_monitor_brightness(const unsigned int bright) { if (_is_init_success) SetMonitorBrightness(_handle_cur_monitor, bright); } void pm_monitor_brightness::init() { _monitor = MonitorFromWindow(NULL, MONITOR_DEFAULTTOPRIMARY); _is_init_success = GetNumberOfPhysicalMonitorsFromHMONITOR(_monitor, &_physical_monitor_number); if (_is_init_success) { _physical_monitor = (LPPHYSICAL_MONITOR)malloc(_physical_monitor_number * sizeof(LPPHYSICAL_MONITOR)); if (NULL != _physical_monitor) { _is_init_success = GetPhysicalMonitorsFromHMONITOR(_monitor, _physical_monitor_number, _physical_monitor); if (_is_init_success) { DWORD max_val = 0; DWORD cur_val = 0; DWORD min_val = 0; HANDLE ppp = _physical_monitor[0].hPhysicalMonitor; _is_init_success = GetMonitorBrightness(ppp, & amp;min_val, & amp;cur_val, & amp;max_val); _brightness_val._cur = cur_val; _brightness_val._max = max_val; _brightness_val._min = min_val; _handle_cur_monitor = ppp; } } } }
————— End————–
Attached code picture: