首页 > 编程学习 > 在任务栏上用滚轮控制屏幕亮度

在任务栏上用滚轮控制屏幕亮度

发布时间:2022/9/22 20:01:55

用到以下项目:

Class_Monitor:https://github.com/jNizM/Class_Monitor

Beautifultooltip:https://github.com/telppa/BeautifulToolTip

文件结构

使用方法:鼠标移动到任务栏,然后使用滚轮调节屏幕亮度。

 

  1 ; FileInstall, BTT.ahk, %A_ScriptDir%, 1
  2 ; FileInstall, Gdip_All.ahk, %A_ScriptDir%, 1
  3 ; FileInstall, NonNull.ahk, %A_ScriptDir%, 1
  4 
  5 #If (MouseIsOveRControl("MSTaskListWClass1"))
  6 i := Monitor.GetBrightness().Current
  7 Wheelup::
  8 i += 5
  9 changeCurrent(i)
 10 Return
 11 WheelDown::
 12 i -= 5
 13 changeCurrent(i)
 14 Return
 15 MouseIsOveRControl(ControlClass){
 16 MouseGetPos,,,,thisControl
 17 IfEqual,thisControl,%Controlclass%
 18 return true
 19 }
 20 #If
 21 changeCurrent(i){
 22 Monitor.SetBrightness(i)
 23 btt(Monitor.GetBrightness().Current,,,,"Style2",{Transparent:v})
 24 Sleep, 150
 25 btt()
 26 Return
 27 }
 28 
 29 
 30 ; ===============================================================================================================================
 31 ; AutoHotkey wrapper for Monitor Configuration API Functions
 32 ;
 33 ; Author ....: jNizM
 34 ; Released ..: 2015-05-26
 35 ; Modified ..: 2020-07-29
 36 ; Github ....: https://github.com/jNizM/Class_Monitor
 37 ; Forum .....: https://www.autohotkey.com/boards/viewtopic.php?f=6&t=62955
 38 ; ===============================================================================================================================
 39 
 40 
 41 class Monitor
 42 {
 43 
 44     ; ===== PUBLIC METHODS ======================================================================================================
 45 
 46     GetBrightness(Display := "")
 47     {
 48         if (hMonitor := this.GetMonitorHandle(Display))
 49         {
 50             PhysicalMonitors := this.GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor)
 51             hPhysicalMonitor := this.GetPhysicalMonitorsFromHMONITOR(hMonitor, PhysicalMonitors, PHYSICAL_MONITOR)
 52             Brightness := this.GetMonitorBrightness(hPhysicalMonitor)
 53             this.DestroyPhysicalMonitors(PhysicalMonitors, PHYSICAL_MONITOR)
 54             return Brightness
 55         }
 56         return false
 57     }
 58 
 59 
 60     GetContrast(Display := "")
 61     {
 62         if (hMonitor := this.GetMonitorHandle(Display))
 63         {
 64             PhysicalMonitors := this.GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor)
 65             hPhysicalMonitor := this.GetPhysicalMonitorsFromHMONITOR(hMonitor, PhysicalMonitors, PHYSICAL_MONITOR)
 66             Contrast := this.GetMonitorContrast(hPhysicalMonitor)
 67             this.DestroyPhysicalMonitors(PhysicalMonitors, PHYSICAL_MONITOR)
 68             return Contrast
 69         }
 70         return false
 71     }
 72 
 73 
 74     GetGammaRamp(Display := "")
 75     {
 76         if (DisplayName := this.GetDisplayName(Display))
 77         {
 78             if (hDC := this.CreateDC(DisplayName))
 79             {
 80                 GammaRamp := this.GetDeviceGammaRamp(hDC)
 81                 this.DeleteDC(hDC)
 82                 return GammaRamp
 83             }
 84             this.DeleteDC(hDC)
 85         }
 86         return false
 87     }
 88 
 89 
 90     RestoreFactoryDefault(Display := "")
 91     {
 92         if (hMonitor := this.GetMonitorHandle(Display))
 93         {
 94             PhysicalMonitors := this.GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor)
 95             hPhysicalMonitor := this.GetPhysicalMonitorsFromHMONITOR(hMonitor, PhysicalMonitors, PHYSICAL_MONITOR)
 96             this.RestoreMonitorFactoryDefaults(hPhysicalMonitor)
 97             this.DestroyPhysicalMonitors(PhysicalMonitors, PHYSICAL_MONITOR)
 98             return true
 99         }
100         return false
101     }
102 
103 
104     SetBrightness(Brightness, Display := "")
105     {
106         if (hMonitor := this.GetMonitorHandle(Display))
107         {
108             PhysicalMonitors := this.GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor)
109             hPhysicalMonitor := this.GetPhysicalMonitorsFromHMONITOR(hMonitor, PhysicalMonitors, PHYSICAL_MONITOR)
110             GetBrightness    := this.GetMonitorBrightness(hPhysicalMonitor)
111             Brightness := (Brightness < GetBrightness["Minimum"]) ? GetBrightness["Minimum"]
112                         : (Brightness > GetBrightness["Maximum"]) ? GetBrightness["Maximum"]
113                         : (Brightness)
114             this.SetMonitorBrightness(hPhysicalMonitor, Brightness)
115             this.DestroyPhysicalMonitors(PhysicalMonitors, PHYSICAL_MONITOR)
116             return Brightness
117         }
118         return false
119     }
120 
121 
122     SetContrast(Contrast, Display := "")
123     {
124         if (hMonitor := this.GetMonitorHandle(Display))
125         {
126             PhysicalMonitors := this.GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor)
127             hPhysicalMonitor := this.GetPhysicalMonitorsFromHMONITOR(hMonitor, PhysicalMonitors, PHYSICAL_MONITOR)
128             GetContrast      := this.GetMonitorContrast(hPhysicalMonitor)
129             Contrast := (Contrast < GetContrast["Minimum"]) ? GetContrast["Minimum"]
130                       : (Contrast > GetContrast["Maximum"]) ? GetContrast["Maximum"]
131                       : (Contrast)
132             this.SetMonitorContrast(hPhysicalMonitor, Contrast)
133             this.DestroyPhysicalMonitors(PhysicalMonitors, PHYSICAL_MONITOR)
134             return Contrast
135         }
136         return false
137     }
138 
139 
140     SetGammaRamp(Red, Green, Blue, Display := "")
141     {
142         if (DisplayName := this.GetDisplayName(Display))
143         {
144             if (hDC := this.CreateDC(DisplayName))
145             {
146                 this.SetDeviceGammaRamp(hDC, Red, Green, Blue)
147                 this.DeleteDC(hDC)
148                 return true
149             }
150             this.DeleteDC(hDC)
151         }
152         return false
153     }
154     
155 
156     ; ===== PRIVATE METHODS =====================================================================================================
157 
158     CreateDC(DisplayName)
159     {
160         if (hDC := DllCall("gdi32\CreateDC", "str", DisplayName, "ptr", 0, "ptr", 0, "ptr", 0, "ptr"))
161             return hDC
162         return false
163     }
164 
165 
166     DeleteDC(hDC)
167     {
168         if (DllCall("gdi32\DeleteDC", "ptr", hDC))
169             return true
170         return false
171     }
172 
173 
174     DestroyPhysicalMonitors(PhysicalMonitorArraySize, PHYSICAL_MONITOR)
175     {
176         if (DllCall("dxva2\DestroyPhysicalMonitors", "uint", PhysicalMonitorArraySize, "ptr", &PHYSICAL_MONITOR))
177             return true
178         return false
179     }
180 
181 
182     EnumDisplayMonitors(hMonitor := "")
183     {
184         static EnumProc := RegisterCallback(Monitor.EnumProc)
185         static DisplayMonitors := {}
186 
187         if (MonitorNumber = "")
188             DisplayMonitors := {}
189 
190         if (DisplayMonitors.MaxIndex() = "")
191             if (DllCall("user32\EnumDisplayMonitors", "ptr", 0, "ptr", 0, "ptr", EnumProc, "ptr", &DisplayMonitors, "uint"))
192                 return (MonitorNumber = "") ? DisplayMonitors : DisplayMonitors.HasKey(MonitorNumber) ? DisplayMonitors[MonitorNumber] : false
193         return false
194     }
195 
196 
197     EnumProc(hDC, RECT, ObjectAddr)
198     {
199         DisplayMonitors := Object(ObjectAddr)
200         MonitorInfo := Monitor.GetMonitorInfo(this)
201         DisplayMonitors.Push(MonitorInfo)
202         return true
203     }
204 
205 
206     GetDeviceGammaRamp(hMonitor)
207     {
208         VarSetCapacity(GAMMA_RAMP, 1536, 0)
209         if (DllCall("gdi32\GetDeviceGammaRamp", "ptr", hMonitor, "ptr", &GAMMA_RAMP))
210         {
211             GammaRamp := []
212             GammaRamp["Red"]   := NumGet(GAMMA_RAMP,        2, "ushort") - 128
213             GammaRamp["Green"] := NumGet(GAMMA_RAMP,  512 + 2, "ushort") - 128
214             GammaRamp["Blue"]  := NumGet(GAMMA_RAMP, 1024 + 2, "ushort") - 128
215             return GammaRamp
216         }
217         return false
218     }
219 
220 
221     GetDisplayName(Display := "")
222     {
223         DisplayName := ""
224 
225         if (Enum := this.EnumDisplayMonitors()) && (Display != "")
226         {
227             for k, Mon in Enum
228                 if (InStr(Mon["Name"], Display))
229                     DisplayName := Mon["Name"]
230         }
231         if (DisplayName = "")
232             if (hMonitor := this.MonitorFromWindow())
233                 DisplayName := this.GetMonitorInfo(hMonitor)["Name"]
234 
235         return DisplayName
236     }
237 
238 
239     GetMonitorBrightness(hMonitor)
240     {
241         if (DllCall("dxva2\GetMonitorBrightness", "ptr", hMonitor, "uint*", Minimum, "uint*", Current, "uint*", Maximum))
242             return { "Minimum": Minimum, "Current": Current, "Maximum": Maximum }
243         return false
244     }
245 
246 
247     GetMonitorContrast(hMonitor)
248     {
249         if (DllCall("dxva2\GetMonitorContrast", "ptr", hMonitor, "uint*", Minimum, "uint*", Current, "uint*", Maximum))
250             return { "Minimum": Minimum, "Current": Current, "Maximum": Maximum }
251         return false
252     }
253 
254 
255     GetMonitorHandle(Display := "")
256     {
257         hMonitor := 0
258 
259         if (Enum := this.EnumDisplayMonitors()) && (Display != "")
260         {
261             for k, Mon in Enum
262                 if (InStr(Mon["Name"], Display))
263                     hMonitor := Mon["Handle"]
264         }
265         if !(hMonitor)
266             hMonitor := this.MonitorFromWindow()
267 
268         return hMonitor
269     }
270 
271 
272     GetMonitorInfo(hMonitor)
273     {
274         NumPut(VarSetCapacity(MONITORINFOEX, 40 + (32 << !!A_IsUnicode)), MONITORINFOEX, 0, "uint")
275         if (DllCall("user32\GetMonitorInfo", "ptr", hMonitor, "ptr", &MONITORINFOEX))
276         {
277             MONITORINFO := []
278             MONITORINFO["Handle"]   := hMonitor
279             MONITORINFO["Name"]     := Name := StrGet(&MONITORINFOEX + 40, 32)
280             MONITORINFO["Number"]   := RegExReplace(Name, ".*(\d+)$", "$1")
281             MONITORINFO["Left"]     := NumGet(MONITORINFOEX,  4, "int")
282             MONITORINFO["Top"]      := NumGet(MONITORINFOEX,  8, "int")
283             MONITORINFO["Right"]    := NumGet(MONITORINFOEX, 12, "int")
284             MONITORINFO["Bottom"]   := NumGet(MONITORINFOEX, 16, "int")
285             MONITORINFO["WALeft"]   := NumGet(MONITORINFOEX, 20, "int")
286             MONITORINFO["WATop"]    := NumGet(MONITORINFOEX, 24, "int")
287             MONITORINFO["WARight"]  := NumGet(MONITORINFOEX, 28, "int")
288             MONITORINFO["WABottom"] := NumGet(MONITORINFOEX, 32, "int")
289             MONITORINFO["Primary"]  := NumGet(MONITORINFOEX, 36, "uint")
290             return MONITORINFO
291         }
292         return false
293     }
294 
295 
296     GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor)
297     {
298         if (DllCall("dxva2\GetNumberOfPhysicalMonitorsFromHMONITOR", "ptr", hMonitor, "uint*", NumberOfPhysicalMonitors))
299             return NumberOfPhysicalMonitors
300         return false
301     }
302 
303 
304     GetPhysicalMonitorsFromHMONITOR(hMonitor, PhysicalMonitorArraySize, ByRef PHYSICAL_MONITOR)
305     {
306         VarSetCapacity(PHYSICAL_MONITOR, (A_PtrSize + 256) * PhysicalMonitorArraySize, 0)
307         if (DllCall("dxva2\GetPhysicalMonitorsFromHMONITOR", "ptr", hMonitor, "uint", PhysicalMonitorArraySize, "ptr", &PHYSICAL_MONITOR))
308             return NumGet(PHYSICAL_MONITOR, 0, "ptr")
309         return false
310     }
311 
312 
313     MonitorFromWindow(hWindow := 0)
314     {
315         static MONITOR_DEFAULTTOPRIMARY := 0x00000001
316 
317         if (hMonitor := DllCall("user32\MonitorFromWindow", "ptr", hWindow, "uint", MONITOR_DEFAULTTOPRIMARY))
318             return hMonitor
319         return false
320     }
321 
322 
323     RestoreMonitorFactoryDefaults(hMonitor)
324     {
325         if (DllCall("dxva2\RestoreMonitorFactoryDefaults", "ptr", hMonitor))
326             return false
327         return true
328     }
329 
330 
331     SetDeviceGammaRamp(hMonitor, Red, Green, Blue)
332     {
333         loop % VarSetCapacity(GAMMA_RAMP, 1536, 0) / 6
334         {
335             NumPut((r := (red   + 128) * (A_Index - 1)) > 65535 ? 65535 : r, GAMMA_RAMP,        2 * (A_Index - 1), "ushort")
336             NumPut((g := (green + 128) * (A_Index - 1)) > 65535 ? 65535 : g, GAMMA_RAMP,  512 + 2 * (A_Index - 1), "ushort")
337             NumPut((b := (blue  + 128) * (A_Index - 1)) > 65535 ? 65535 : b, GAMMA_RAMP, 1024 + 2 * (A_Index - 1), "ushort")
338         }
339         if (DllCall("gdi32\SetDeviceGammaRamp", "ptr", hMonitor, "ptr", &GAMMA_RAMP))
340             return true
341         return false
342     }
343 
344 
345     SetMonitorBrightness(hMonitor, Brightness)
346     {
347         if (DllCall("dxva2\SetMonitorBrightness", "ptr", hMonitor, "uint", Brightness))
348             return true
349         return false
350     }
351 
352 
353     SetMonitorContrast(hMonitor, Contrast)
354     {
355         if (DllCall("dxva2\SetMonitorContrast", "ptr", hMonitor, "uint", Contrast))
356             return true
357         return false
358     }
359 
360 }
361 
362 ; ===============================================================================================================================

 

Copyright © 2010-2022 mfbz.cn 版权所有 |关于我们| 联系方式|豫ICP备15888888号