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

!!版权声明:本文为博主原创文章,版权归原文作者和博客园共有,谢绝任何形式的 转载!!

作者:mohist

 

---- 已经更正文章中错误的地方, 时间: 10/10/2020---------

 

 

自己封装了一个类,方便以后使用。

自己是在win7上完成的测试,发现可以。博文是在Mac上写的,代码可能存在错误,请见谅,我会放上图片。

 

1、包含头文件

#include <physicalmonitorenumerationapi.h>
#include <highlevelmonitorconfigurationapi.h>

2、包含系统 lib 库文件

#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: 增加亮度
    @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;

};

  

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

  

--------------- 完结---------------

附 代码图片:

 

 

 

 

 

 

posted @ 2019-05-25 14:27  mohist  阅读(2279)  评论(3编辑  收藏  举报