Skip to content

Instantly share code, notes, and snippets.

@Dragorn421
Created February 20, 2020 15:19
Show Gist options
  • Save Dragorn421/794793a1eb2be07e4b8432f153016ea1 to your computer and use it in GitHub Desktop.
Save Dragorn421/794793a1eb2be07e4b8432f153016ea1 to your computer and use it in GitHub Desktop.
Black background for windows
; AutoHotKey script - https://www.autohotkey.com/
; add black background to *focused* window
; press and hold B then press + on numpad : add borders
; B and - on numpad : remove borders
SetupBackground()
{
global
BACKGROUND_PARTS := 2
guiIds := []
guiWinIds := []
; covers whole screen behind window
guiId := "bg_full"
guiIds.Push(guiId)
; -Caption hides title bar
; +ToolWindow hides window in task bar
Gui, %guiId%:New, -Caption +ToolWindow
Gui, %guiId%:Color, "Black"
Gui, %guiId%:+LastFound
WinGet, guiWinId, ID
guiWinIds.Push(guiWinId)
; bottom part, covers taskbar
guiId := "bg_bottom"
guiIds.Push(guiId)
Gui, %guiId%:New, -Caption +ToolWindow +AlwaysOnTop
Gui, %guiId%:Color, "Black"
Gui, %guiId%:+LastFound
WinGet, guiWinId, ID
guiWinIds.Push(guiWinId)
}
ShowBackground()
{
global
WinGetPos, x,y,w,h, ahk_id %targetWindow%
; A_ScreenWidth and A_ScreenHeight will only work for main monitor
Gui, bg_full:Show, X0 Y0 W%A_ScreenWidth% H%A_ScreenHeight%
botY := y + h
botH := A_ScreenHeight - botY
Gui, bg_bottom:Show, X0 Y%botY% W%A_ScreenWidth% H%botH%
WinActivate, ahk_id %targetWindow%
backgroundShown := true
; store if window was AlwaysOnTop
WinGet, ExStyle, ExStyle, ahk_id %targetWindow%
if (ExStyle & 0x8)
targetWindowWasAlwaysOnTop := true
else
{
targetWindowWasAlwaysOnTop := false
WinSet, AlwaysOnTop, On, ahk_id %targetWindow%
}
}
HideBackground()
{
global
Loop, %BACKGROUND_PARTS% {
guiId := guiIds[A_Index]
Gui, %guiId%:Hide
}
backgroundShown := false
if(not targetWindowWasAlwaysOnTop)
WinSet, AlwaysOnTop, Off, ahk_id %targetWindow%
}
IsBackgroundActive()
{
global
anyActive := false
Loop, %BACKGROUND_PARTS% {
anyActive := anyActive or WinActive("ahk_id " . guiWinIds[A_Index])
}
return anyActive
}
UpdateBackground()
{
global
if(not targetWindow)
{
SetTimer,, Delete
}
else
{
; for some reason task bar sometimes takes priority over AlwaysOnTop
guiWinId := guiWinIds[2]
WinSet, AlwaysOnTop, on, ahk_id %guiWinId%
; if target window is focused, do nothing
if(WinActive("ahk_id " . targetWindow))
{
targetWindowLastActive := 2000 / UPDATE_DELAY
; make sure background is shown
if(not backgroundShown)
ShowBackground()
}
else if(WinExist("ahk_id " . targetWindow))
{
; "fix" immediate focus change after hiding background still keeping target window on top
if (targetWindowLastActive > 0)
{
WinSet, Top,, A
targetWindowLastActive := targetWindowLastActive - 1
}
; if target window exists but background is focused, focus target window
if(IsBackgroundActive())
WinActivate, ahk_id %targetWindow%
; if target window exists and neither it nor background is focused, hide background temporarily
else
HideBackground()
}
; if target window no longer exists, end
else
Reset()
}
}
Reset()
{
global
; show title bar if it was present at first
if (targetWindowHadTitleBar)
WinSet, Style, +0xC00000, ahk_id %targetWindow%
HideBackground()
targetWindow := false
targetWindowLastActive := 0
}
ExitFunc()
{
Reset()
}
; INIT
UPDATE_DELAY := 10
SetupBackground()
targetWindow := false
backgroundShown := false
OnExit("ExitFunc")
~B & NumpadAdd::
if(not targetWindow)
{
WinGet, targetWindow, ID, A
; hide title bar
WinGet, Style, Style, ahk_id %targetWindow%
if (Style & 0xC00000)
{
targetWindowHadTitleBar := true
WinSet, Style, -0xC00000, ahk_id %targetWindow%
}
else
targetWindowHadTitleBar := false
ShowBackground()
SetTimer, UpdateBackground, %UPDATE_DELAY%
}
return
~B & NumpadSub::
Reset()
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment