1.关于
- 代码是通过测试的,测试环境: win7 + MFC
- 为什么要发在这里? 区别于上一篇随笔。 MD排版更顺眼
- demo 会放到 这里
- 更正了上一篇随笔中的代码错误
2.头文件
#include <physicalmonitorenumerationapi.h>
#include <highlevelmonitorconfigurationapi.h>
3.lib库文件
#pragma comment(lib, "Dxva2.lib")
4.类头文件
#pragma once
//
#include <physicalmonitorenumerationapi.h>
#include <highlevelmonitorconfigurationapi.h>
class pm_monitor_brightness
{
public:
pm_monitor_brightness(void);
virtual ~pm_monitor_brightness();
/*
@brief: 增加亮度
@param:const unsigned int offset - 步长,最大为100, 若超过100,将亮度设置为最大
@return: int
0 - 成功
1 - 失败,设置失败
*/
int increase(const unsigned int offset = 10);
/*
@brief: 降低亮度
@param:const unsigned int offset - 步长,最大为100, 若超过100,将亮度设置为低
@return: int
0 - 成功
1 - 失败,设置失败
*/
int decrease(const unsigned int offset = 10);
private:
// 设置屏幕亮度
void set_monitor_brightness(const unsigned int bright);
void init();
private:
enum
{
max_offset_100 = 100,
};
struct pm_brightness_val
{
// 当前亮度值
int _cur;
// 显示器最大亮度
int _max;
// 显示器亮度最低值
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;
};
5.类源文件
#include "stdafx.h"
#include "pm_monitor_brightness.h"
#include <winuser.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& param = _brightness_val;
// 没有初始化成功
if (!_is_init_success)
return 1;
// 已经打到最大值
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& param = _brightness_val;
// 没有初始化成功
if (!_is_init_success)
return 1;
// 已经打到最大值
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;
}
// 设置屏幕亮度
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, &min_val, &cur_val, &max_val);
_brightness_val._cur = cur_val;
_brightness_val._max = max_val;
_brightness_val._min = min_val;
_handle_cur_monitor = ppp;
}
}
else
{
_is_init_success = false;
}
}
}
6.调用
_ctrl_brightness.increase(10);
_ctrl_brightness.decrease(10);
7.更正后,编译结果