3 ways to dim the screen

Post your working scripts, libraries and tools
User avatar
jeeswg
Posts: 6648
Joined: 19 Dec 2016, 01:58
Location: UK

3 ways to dim the screen

07 Nov 2017, 19:09

Here are 3 ways to dim the screen.
- Using powrprof.dll to change the brightness of the laptop. Note: this does not affect external devices like a TV (the other 2 do). Based on code by qwerty12.
- Using GetDeviceGammaRamp/SetDeviceGammaRamp to change the screen brightness. Note: this may be the same approach that f.lux uses. Based on code by jNizM.
- Using an all-black GUI with different levels of transparency. Note: this affects printscreens, unless you use the window message to turn it off temporarily (the other 2 do not affect printscreens).

Note: I recommend you record the brightness settings somewhere, when you first use the scripts, before using the scripts to change those values.

Code: Select all

;based on code by qwerty12:
;Set laptop brightness & show Win 10's native OSD - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=6&t=26921

q:: ;get AC/DC brightness
w:: ;set AC brightness
e:: ;set DC brightness
;note: AC is the brightness when the laptop is plugged in
DllCall("powrprof\PowerGetActiveScheme", Ptr,0, PtrP,vActivePolicyGuid, UInt)

VarSetCapacity(GUID_VIDEO_SUBGROUP, 16)
NumPut(0x7516B95F, &GUID_VIDEO_SUBGROUP, 0, "UInt"), NumPut(0x4464F776, &GUID_VIDEO_SUBGROUP, 4, "UInt")
NumPut(0x1606538C, &GUID_VIDEO_SUBGROUP, 8, "UInt"), NumPut(0x99CC407F, &GUID_VIDEO_SUBGROUP, 12, "UInt")

VarSetCapacity(GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS, 16)
NumPut(0xADED5E82, &GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS, 0, "UInt"), NumPut(0x4619B909, &GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS, 4, "UInt")
NumPut(0xD7F54999, &GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS, 8, "UInt"), NumPut(0xCB0BAC1D, &GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS, 12, "UInt")

DllCall("powrprof\PowerReadACValueIndex", Ptr,0, Ptr,vActivePolicyGuid, Ptr,&GUID_VIDEO_SUBGROUP, Ptr,&GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS, UIntP,vBrightnessAC, UInt)
DllCall("powrprof\PowerReadDCValueIndex", Ptr,0, Ptr,vActivePolicyGuid, Ptr,&GUID_VIDEO_SUBGROUP, Ptr,&GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS, UIntP,vBrightnessDC, UInt)

;e.g. 46 40
if InStr(A_ThisHotkey, "q")
	MsgBox, % "AC brightness: " vBrightnessAC "`r`n" "DC brightness: " vBrightnessDC

if InStr(A_ThisHotkey, "w")
{
	InputBox, vBrightnessAC2,, % vPrompt,,,,,,,, % vBrightnessAC
	if !(vBrightnessAC2 = vBrightnessAC)
	{
		DllCall("powrprof\PowerWriteACValueIndex", Ptr,0, Ptr,vActivePolicyGuid, Ptr,&GUID_VIDEO_SUBGROUP, Ptr,&GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS, UInt,vBrightnessAC2, UInt)
		DllCall("powrprof\PowerSetActiveScheme", Ptr,0, Ptr,vActivePolicyGuid, UInt)
	}
}

if InStr(A_ThisHotkey, "e")
{
	InputBox, vBrightnessDC2,, % vPrompt,,,,,,,, % vBrightnessDC
	if !(vBrightnessDC2 = vBrightnessDC)
	{
		DllCall("powrprof\PowerWriteDCValueIndex", Ptr,0, Ptr,vActivePolicyGuid, Ptr,&GUID_VIDEO_SUBGROUP, Ptr,&GUID_DEVICE_POWER_POLICY_VIDEO_BRIGHTNESS, UInt,vBrightnessDC2, UInt)
		DllCall("powrprof\PowerSetActiveScheme", Ptr,0, Ptr,vActivePolicyGuid, UInt)
	}
}
return

Code: Select all

;based on code by jNizM:
;GitHub - jNizM/Class_Monitor: Monitor Class (WinAPI)
;https://github.com/jNizM/Class_Monitor
;Class Monitor (Brightness, ColorTemperature) - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=6&t=7854

;e.g. f.lux on/off
;RGB: 129,69,5 ;e.g. f.lux on
;RGB: 129,129,129 ;e.g. f.lux off

q:: ;get brightness
VarSetCapacity(vData, 1536, 0)
hDC := DllCall("user32\GetDC", Ptr,0, Ptr)
DllCall("gdi32\GetDeviceGammaRamp", Ptr,hDC, Ptr,&vData)
vColR := NumGet(vData, 2, "UShort") - 128
vColG := NumGet(vData, 512+2, "UShort") - 128
vColB := NumGet(vData, 1024+2, "UShort") - 128
DllCall("user32\ReleaseDC", Ptr,0, Ptr,hDC)
MsgBox, % Format("RGB: {:i},{:i},{:i}", vColR, vColG, vColB) ;e.g. RGB: 127,127,125
MsgBox, % Format("red: {:i}`r`n" "green: {:i}`r`n" "blue: {:i}", vColR, vColG, vColB)
return

w:: ;set brightness
e:: ;set brightness (default values)
vColRGB := "1,1,1"
vColRGB := "32,32,32"
vColRGB := "64,64,64"
vColRGB := "128,128,128"
vColRGB := "255,255,255"
if InStr(A_ThisHotkey, "e")
	vColRGB := "127,127,125"
oArray := StrSplit(vColRGB, ",")
vColR := oArray.1+128, vColG := oArray.2+128, vColB := oArray.3+128, oArray := ""
VarSetCapacity(vData, 1536, 0)
Loop, % 1536 / 6
{
	vIndex := A_Index-1
	NumPut((vR := vColR*vIndex) > 65535 ? 65535 : vR, vData, 2*vIndex, "UShort")
	NumPut((vG := vColG*vIndex) > 65535 ? 65535 : vG, vData, 512 + 2*vIndex, "UShort")
	NumPut((vB := vColB*vIndex) > 65535 ? 65535 : vB, vData, 1024 + 2*vIndex, "UShort")
}
hDC := DllCall("user32\GetDC", Ptr,0, Ptr)
DllCall("gdi32\SetDeviceGammaRamp", Ptr,hDC, Ptr,&vData)
DllCall("user32\ReleaseDC", Ptr,0, Ptr,hDC)
return

Code: Select all

;'z dimmer.ahk' by jeeswg

OnMessage(0x5555, "MsgMonitor")
OnMessage(0x5556, "MsgMonitor2")
OnMessage(0x5557, "MsgMonitor3")

;handle command line parameter / request brightness
vBright = %1%
if (vBright = "")
{
	vBright2 := 50
	InputBox, vBright,, % "brightness (%) (0=dark, 100=bright)",,,,,,,, % vBright2
	if ErrorLevel
		return
}
WinGet, hWnd, ID, A

;convert % brightness to value for darkness (opacity of black screen)
;0% bright -> 255/255 black
;50% bright -> 128/255 black
;100% bright -> 0/255 black

vNum := Round(((100-vBright)/100) * 255)
if (vNum <= 0)
	ExitApp
else if (vNum >= 255)
	vNum := 255

;specify a number between 0 (invisible) and 255 (opaque)
;bigger number = darker screen

Gui, Color, 000000
;WS_EX_TRANSPARENT := 0x20 (click-through)
Gui, -Caption +AlwaysOnTop +E0x20 +HwndhGui +ToolWindow
Gui, Show, % Format("x0 y0 w{} h{}", A_ScreenWidth, A_ScreenHeight), dimmer
WinSet, Transparent, % vNum, % "ahk_id " hGui
WinActivate, % "ahk_id " hWnd
return

;==================================================

;hide window (turn dimmer off)
MsgMonitor(wParam, lParam, uMsg)
{
	global
	Gui, Hide
}

;show window (turn dimmer on)
MsgMonitor2(wParam, lParam, uMsg)
{
	global
	WinGet, hWnd, ID, A
	Gui, Show
	WinActivate, % "ahk_id " hWnd
}

;return dimmer level
MsgMonitor3(wParam, lParam, msg)
{
	global
	return vBright
}

;==================================================

/*
;lines required for other script
;e.g. turn dimmer on/off while do a printscreen

vDHW := A_DetectHiddenWindows
DetectHiddenWindows, On
vTMM := A_TitleMatchMode
SetTitleMatchMode, 2
if hWnd := WinExist("\z dimmer.ahk - AutoHotkey ahk_class AutoHotkey")
	SendMessage, 0x5555,,,, % "ahk_id " hWnd ;turn dimmer off
DetectHiddenWindows, % vDHW
SetTitleMatchMode, % vTMM

vDHW := A_DetectHiddenWindows
DetectHiddenWindows, On
vTMM := A_TitleMatchMode
SetTitleMatchMode, 2
if hWnd := WinExist("\z dimmer.ahk - AutoHotkey ahk_class AutoHotkey")
	PostMessage, 0x5556,,,, % "ahk_id " hWnd ;turn dimmer on
DetectHiddenWindows, % vDHW
SetTitleMatchMode, % vTMM
*/

;==================================================
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Scripts and Functions”

Who is online

Users browsing this forum: Bluscream and 39 guests