zoukankan      html  css  js  c++  java
  • win32-改变显示器的亮度

    调用SetMonitorBrightness

    代码示例:

    #pragma comment(lib, "dxva2.lib")
    #include <windows.h>
    #include <lowlevelmonitorconfigurationapi.h>
    #include <physicalmonitorenumerationapi.h>
    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string>
    #include <highlevelmonitorconfigurationapi.h>
    
    using namespace std;
    
    int main()
    {
        HWND hWnd = GetDesktopWindow();
        HMONITOR hMonitor = MonitorFromWindow(hWnd, MONITOR_DEFAULTTOPRIMARY);
        cout << "Monitor: " << hMonitor << endl;
    
        DWORD cPhysicalMonitors;
        BOOL bSuccess = GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, &cPhysicalMonitors);
        cout << "GetNumber: " << bSuccess << ", number of physical monitors: " << cPhysicalMonitors << endl;
    
        LPPHYSICAL_MONITOR pPhysicalMonitors = (LPPHYSICAL_MONITOR)malloc(cPhysicalMonitors * sizeof(PHYSICAL_MONITOR));
        bSuccess = GetPhysicalMonitorsFromHMONITOR(hMonitor, cPhysicalMonitors, pPhysicalMonitors);
        cout << "GetPhysicalMonitor: " << bSuccess << endl
            << "Handle: " << pPhysicalMonitors->hPhysicalMonitor << endl
            << "Description: ";
        wcout << (WCHAR*)(pPhysicalMonitors->szPhysicalMonitorDescription);
    
        DWORD MinimumBrightness;
        DWORD CurrentBrightness;
        DWORD MaximumBrightness;
        BOOL err = GetMonitorBrightness(pPhysicalMonitors->hPhysicalMonitor, &MinimumBrightness, &CurrentBrightness, &MaximumBrightness);
    
        err = SetMonitorBrightness(pPhysicalMonitors->hPhysicalMonitor, 50);
    
        int err1 = GetLastError();
        DWORD MonitorCapabilities;
        DWORD SupportedColorTemperatures;
        err = GetMonitorCapabilities(pPhysicalMonitors->hPhysicalMonitor, &MonitorCapabilities, &SupportedColorTemperatures);
        
        DestroyPhysicalMonitors(cPhysicalMonitors, pPhysicalMonitors);
        free(pPhysicalMonitors);
    
    }

    上面代码只对台式机的显示器起作用,如果要改变笔记本电脑的显示器亮度,请改用 WmiSetBrightness 

    样例:

    ManagementClass mclass = new ManagementClass("WmiMonitorBrightnessMethods");
    mclass.Scope = new ManagementScope(@"\.
    ootwmi");
    ManagementObjectCollection instances = mclass.GetInstances();
    
    // I assume you get one instance per monitor
    foreach(ManagementObject instance in instances)
    {
        ulong timeout = 1; // in seconds
        ushort brightness = 50; // in percent
        object[] args = new object[] { timeout, brightness };
        instance.InvokeMethod("WmiSetBrightness", args);
    }

    链接: What API call would I use to change brightness of laptop (.NET)?

  • 相关阅读:
    python django 创建app失败 升级sqlite3 或者修改数据库连接为mysql
    Decima json 化
    openpyxl excel操作
    centos添加python虚拟环境
    oracle的参数文件:pfile和spfile
    修改 db_unique_name
    工具 --- Vim
    部署 --- Nginx
    工具 --- Git使用
    django 之(五) --- RESTApi总结
  • 原文地址:https://www.cnblogs.com/strive-sun/p/13214738.html
Copyright © 2011-2022 走看看