Skip to content

Instantly share code, notes, and snippets.

@ericreeves
Last active February 16, 2025 23:18
Show Gist options
  • Save ericreeves/fd426cc0457a5a47058e1ad1a29d9bd6 to your computer and use it in GitHub Desktop.
Save ericreeves/fd426cc0457a5a47058e1ad1a29d9bd6 to your computer and use it in GitHub Desktop.
AHK Script for Active Window Border in Windows 11 22H2
;
; Inspiration / Code Jacked from the following resources:
; https://www.reddit.com/r/windowsporn/comments/x6299x/a_small_effect_on_window_switching/
; https://superuser.com/questions/1190658/fine-tune-this-red-border-around-active-window-ahk-script/1191059#1191059?newreg=d3acdcdab8714e76a5efeca9996e792f
; https://www.autohotkey.com/boards/viewtopic.php?f=6&t=110505
; https://discord.com/channels/898554690126630914/898556726108901386/1053662963585781760 # Komorebi Discord
;
#NoEnv
#SingleInstance, Force
#Persistent
SendMode, Input
SetBatchLines, -1
SetWorkingDir, %A_ScriptDir%
Gui +LastFound
hWnd := WinExist()
DllCall("RegisterShellHookWindow", UInt,hWnd)
MsgNum := DllCall("RegisterWindowMessage", Str,"SHELLHOOK")
OnMessage(MsgNum, "ShellMessage")
Return
ShellMessage(wParam,lParam) {
Local k
if (wParam = 32772){
SetTimer, DrawActive, -1
}
}
DrawActive:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Border Color Configuration
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
border_color := "0x6238FF"
; Start by removing the borders from all windows, since we do not know which window was previously active
WinGet, WindowHandles, List
loop % WindowHandles
{
DrawBorder(WindowHandles%A_Index%, , 0)
}
; Draw the border around the active window
hwnd := WinExist("A")
DrawBorder(hwnd, border_color, 1)
Return
DrawBorder(hwnd, color:=0xFF0000, enable:=1) {
static DWMWA_BORDER_COLOR := 34
static DWMWA_COLOR_DEFAULT := 0xFFFFFFFF
R := (color & 0xFF0000) >> 16
G := (color & 0xFF00) >> 8
B := (color & 0xFF)
color := (B << 16) | (G << 8) | R
DllCall("dwmapi\DwmSetWindowAttribute", "ptr", hwnd, "int", DWMWA_BORDER_COLOR, "int*", enable ? color : DWMWA_COLOR_DEFAULT, "int", 4)
}
@sitiom
Copy link

sitiom commented Apr 17, 2023

AHKv2 version:

#Requires AutoHotkey v2.0
#SingleInstance Force

myGui := Gui()
myGui.Opt("+LastFound")
hWnd := WinExist()
DllCall("RegisterShellHookWindow", "UInt", hWnd)
MsgNum := DllCall("RegisterWindowMessage", "Str", "SHELLHOOK")
OnMessage(MsgNum, ShellMessage)
Persistent

ShellMessage(wParam,lParam, msg, hwnd) {
    if (wParam = 32772){
        SetTimer(DrawActive,-1)
    }
}

DrawActive()
{
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ; Border Color Configuration
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    border_color := "0x6238FF"
    ; Start by removing the borders from all windows, since we do not know which window was previously active
    windowHandles := WinGetList(,,,)
    For handle in windowHandles
    {
        DrawBorder(handle, , 0)
    }
    ; Draw the border around the active window
    hwnd := WinExist("A")
    DrawBorder(hwnd, border_color, 1)
}

DrawBorder(hwnd, color:=0xFF0000, enable:=1) {
    static DWMWA_BORDER_COLOR := 34
    static DWMWA_COLOR_DEFAULT	:= 0xFFFFFFFF
    R := (color & 0xFF0000) >> 16
    G := (color & 0xFF00) >> 8
    B := (color & 0xFF)
    color := (B << 16) | (G << 8) | R
    DllCall("dwmapi\DwmSetWindowAttribute", "ptr", hwnd, "int", DWMWA_BORDER_COLOR, "int*", enable ? color : DWMWA_COLOR_DEFAULT, "int", 4)
}

@jo-project
Copy link

Is there a way to change the thickness?

@emvaized
Copy link

emvaized commented Aug 27, 2023

Thanks for the script!

For those who want to use system accent color, I recommend to try this snippet — just copy the GetSysColor(n) method from there, and change in this script border_color := "0x6238FF" to border_color := GetSysColor(10).

I also noticed that when new window is created, sometimes I have to refocus it in order for border to appear. I fixed it by adding 300ms delay before border gets drawn.

@icescoop12
Copy link

icescoop12 commented Dec 10, 2023

Is there a way to change the thickness?

@jo-project did you find a way?
thanks

@mmikeww
Copy link

mmikeww commented Feb 9, 2024

@emvaized

I fixed it by adding 300ms delay before border gets drawn.

SetTimer, DrawActive, -300

@downspire
Copy link

Is there any chance this could be updated to work with Windows 24H2 (OS Build 26100.2605)?

@mariusrueve
Copy link

@downspire https://gist.github.com/mariusrueve/c6b1af96c1d5b5792c150e52c8500288

I had a shot at it and this is currently working for me

@downspire
Copy link

@downspire https://gist.github.com/mariusrueve/c6b1af96c1d5b5792c150e52c8500288

I had a shot at it and this is currently working for me

This does work but it does not have the same visual appearance as the original script. Either way thank you for your time.

@ericreeves
Copy link
Author

Check out both "cute-borders" and "tacky-borders".

They both accomplish active window highlighting, and both have far more configuration options. They can both do a different color per app. tacky-borders is even more flexible with the line thickness, and even has animated borders.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment