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(@"\\.\root\wmi");
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)?

上一篇:win32汇编MessageBox


下一篇:[Win32]创建线程学习笔记