모니터의 밝기를 System.Management 로 관리하기

2021. 2. 18. 21:46·Language/C#

Windows10 에서 모니터의 밝기를 조정하고 싶었습니다.

Windows10 Desktop 뿐만아닌 IoT 버전에서도 적용하고 싶었고, 많은 시행착오끝에 성공했습니다.

아래 소스를 공개합니다.

 

NuGet에서 System.Management를 설치해주셔야합니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
public class MonitorBirghtness
{
    protected Thread mThread = null;
    protected bool initBirghtness = false;
    protected bool needMiniumBirghtness = false;
    protected bool needMaxinumBirghtness = false;
    protected bool needRecoveryBrightness = false;
    protected bool needSetBrightness = false;
    protected int setBrightness = 80;
    protected int reverseBirghtness = 80;
    protected int baseBrightness = 80;
    public void bneedMiniumBirghtness() => needMiniumBirghtness = true;
    public void bneedRecoveryBrightness() => needRecoveryBrightness = true;
    public void ineedSetBrightness(int i)
    {
        needSetBrightness = true;
        setBrightness = i;
    }
    public MonitorBirghtness(int _baseBrightness)
    {
        mThread = new Thread(run);
        mThread.Start();
        baseBrightness = _baseBrightness;
    }
    protected void run()
    {
        while (true)
        {
            try
            {
                if (!initBirghtness)
                {
                    initBirghtness = true;
                    SetBrightness((byte)baseBrightness);
                }
 
                if (needSetBrightness)
                {
                    needSetBrightness = false;
                    SetBrightness((byte)setBrightness);
                }
 
                if (needMiniumBirghtness)
                {
                    needMiniumBirghtness = false;
                    reverseBirghtness = baseBrightness;
                    SetBrightness(0x01);
                }
 
                if (needRecoveryBrightness)
                {
                    needRecoveryBrightness = false;
                    SetBrightness((byte)reverseBirghtness);
                }
            }
            catch (Exception ex)
            {
                ILog logger = LogManager.GetLogger(this.GetType());
                logger.Fatal("Brightness Manager Exception >>> " + ex.ToString() + " -> " + ex.StackTrace);
            }
            Thread.Sleep(1000);
        }
    }
 
    //get the actual percentage of brightness
    public static int GetCurrentBrightness()
    {
        //define scope (namespace)
        System.Management.ManagementScope s = new System.Management.ManagementScope("root\\WMI");
        //define query
        System.Management.SelectQuery q = new System.Management.SelectQuery("WmiMonitorBrightness");
        //output current brightness
        System.Management.ManagementObjectSearcher mos = new System.Management.ManagementObjectSearcher(s, q);
        System.Management.ManagementObjectCollection moc = mos.Get();
        //store result
        byte curBrightness = 0;
        foreach (System.Management.ManagementObject o in moc)
        {
            curBrightness = (byte)o.GetPropertyValue("CurrentBrightness");
            break; //only work on the first object
        }
        moc.Dispose();
        mos.Dispose();
        return (int)curBrightness;
    }
 
    //array of valid brightness values in percent
    public static byte[] GetBrightnessLevels()
    {
        //define scope (namespace)
        System.Management.ManagementScope s = new System.Management.ManagementScope("root\\WMI");
        //define query
        System.Management.SelectQuery q = new System.Management.SelectQuery("WmiMonitorBrightness");
        //output current brightness
        System.Management.ManagementObjectSearcher mos = new System.Management.ManagementObjectSearcher(s, q);
        byte[] BrightnessLevels = new byte[0];
 
        System.Management.ManagementObjectCollection moc = mos.Get();
        //store result
        foreach (System.Management.ManagementObject o in moc)
        {
            BrightnessLevels = (byte[])o.GetPropertyValue("Level");
            break; //only work on the first object
        }
        moc.Dispose();
        mos.Dispose();
 
        return BrightnessLevels;
    }
 
    protected static void SetBrightness(byte targetBrightness)
    {
        //define scope (namespace)
        System.Management.ManagementScope s = new System.Management.ManagementScope("root\\WMI");
        //define query
        System.Management.SelectQuery q = new System.Management.SelectQuery("WmiMonitorBrightnessMethods");
        //output current brightness
        System.Management.ManagementObjectSearcher mos = new System.Management.ManagementObjectSearcher(s, q);
        System.Management.ManagementObjectCollection moc = mos.Get();
        foreach (System.Management.ManagementObject o in moc)
        {
            o.InvokeMethod("WmiSetBrightness", new object[] { uint.MaxValue, targetBrightness });
            //note the reversed order - won't work otherwise!
            break; //only work on the first object
        }
 
        moc.Dispose();
        mos.Dispose();
    }
}
Colored by Color Scripter
cs

setBrightness, reverseBirghtness, baseBrightness 등의 인자에는 1부터 100까지만 허용됩니다.

 

Github : github.com/Kuass/Windows10-Brightness-Control

반응형
저작자표시 비영리 (새창열림)
'Language/C#' 카테고리의 다른 글
  • String 으로된 Byte Array를 Byte[] 로 리턴받기
  • [정보] 신선한 충격을 받았다. (알고리즘/코드 최적화)
  • CRC-16/ARC,AUC-CCITT,MODBUS 계산 소스코드
  • String을 BCD로 변환하거나 BCD를 String으로 변환하기(이진화 십진법)
Kua
Kua
정보 공유, 개인 정리 공간 입니다.
  • Kua
    Kua's Miscellaneous
    Kua
    • 분류 전체보기 (185)
      • 대문 (2)
      • Tips (25)
        • Chrome (2)
        • Windows (4)
        • IDE (3)
        • 기타 (16)
      • CodingTest (44)
      • Language (20)
        • PHP (5)
        • C# (7)
        • Java (1)
        • Kotlin (7)
      • Framework & Runtime (16)
        • SpringBoot (12)
        • Node.js (2)
        • Vue.js (1)
        • Gradle (1)
      • DevOps (13)
        • Linux (1)
        • Docker (4)
        • Kubernetes (2)
        • Apache Kafka (1)
        • AWS (1)
      • 일상다반사 (53)
        • 도서 (1)
        • 개발 (8)
        • 후기 - IT (7)
        • 후기 - 일상 (13)
        • 차가리 (4)
        • 방송통신대학교 (4)
        • 음식 (2)
      • Games (12)
        • Minecraft (7)
        • VR (2)
        • 그외 (3)
  • 최근 글

  • 인기 글

  • 태그

    minecraft
    Windows
    갤럭시
    Silver5
    codingtest
    코딩테스트
    spring
    Plugin
    error
    c#
    Kotlin
    알고리즘
    Spring Boot
    백준
    bronze1
    bronze2
    java
    후기
    Algorithm
    github
  • 전체
    오늘
    어제
  • hELLO· Designed By정상우.v4.10.0
Kua
모니터의 밝기를 System.Management 로 관리하기
상단으로

티스토리툴바