Draw border around Active Window

Post your working scripts, libraries and tools for AHK v1.1 and older
forAHK
Posts: 6
Joined: 16 May 2022, 22:43

Draw border around Active Window

Post by forAHK » 15 Nov 2022, 00:05

Draw border around Active Window

Code: Select all

#Persistent

; windows in windows 10 has thick invisible boarder which is about 4 pixels wide.

SetTimer, DrawRect, 50
border_thickness = 4

; set the color of the border
border_color = ffae00


DrawRect:

; Get the current window's position 
WinGetPos, x, y, w, h, A
; To avoid the error message
if (x="")
    return

;Gui, +Lastfound +AlwaysOnTop +Toolwindow
Gui, +Lastfound +AlwaysOnTop 

; set the background for the GUI window 
Gui, Color, %border_color%

; remove thick window border of the GUI window
Gui, -Caption

; Retrieves the minimized/maximized state for a window.
WinGet, notMedium , MinMax, A

if (notMedium==0){
; 0: The window is neither minimized nor maximized.

	offset:=0
	outerX:=offset
	outerY:=offset
	outerX2:=w-offset
	outerY2:=h-offset

	innerX:=border_thickness+offset
	innerY:=border_thickness+offset
	innerX2:=w-border_thickness-offset
	innerY2:=h-border_thickness-offset

	newX:=x
	newY:=y
	newW:=w
	newH:=h

	WinSet, Region, %outerX%-%outerY% %outerX2%-%outerY% %outerX2%-%outerY2% %outerX%-%outerY2% %outerX%-%outerY%    %innerX%-%innerY% %innerX2%-%innerY% %innerX2%-%innerY2% %innerX%-%innerY2% %innerX%-%innerY% 

	Gui, Show, w%newW% h%newH% x%newX% y%newY% NoActivate, GUI4Boarder
	return
} else {
    WinSet, Region, 0-0 w0 h0
    return
}

return

fenchai
Posts: 295
Joined: 28 Mar 2016, 07:57

Re: Draw border around Active Window

Post by fenchai » 15 Nov 2022, 09:25

nice tip, I can use this for all pinned windows maybe.

Coriel-11
Posts: 45
Joined: 18 Aug 2017, 09:02

Re: Draw border around Active Window

Post by Coriel-11 » 24 Nov 2022, 06:09

This is just what I need,
BUT...
I get "Error: Target label does not exist" on "SetTimer, DrawRect, 50"
(I just added it towards the top of my main script)
Matt

gregster
Posts: 9169
Joined: 30 Sep 2013, 06:48
Location: currently afk

Re: Draw border around Active Window

Post by gregster » 24 Nov 2022, 19:10

Coriel-11 wrote:
24 Nov 2022, 06:09
I get "Error: Target label does not exist" on "SetTimer, DrawRect, 50"
(I just added it towards the top of my main script)
Matt
Make sure to copy the whole code from the codebox (it's scrollable): Push 'Select all', then copy.

Coriel-11
Posts: 45
Joined: 18 Aug 2017, 09:02

Re: Draw border around Active Window

Post by Coriel-11 » 29 Nov 2022, 06:13

gregster wrote:
24 Nov 2022, 19:10
Make sure to copy the whole code from the codebox (it's scrollable): Push 'Select all', then copy.
I cannot believe I asked such a stupid question :headwall:
I was obviously having a long day!
Thanks for answering it so gracefully
Matt

ntepa
Posts: 448
Joined: 19 Oct 2022, 20:52

Re: Draw border around Active Window

Post by ntepa » 01 Dec 2022, 05:38

Here's another way to draw a border around a window. It's only supported by Windows 11.

Code: Select all

F1::
    hwnd := WinExist("A")
    DrawBorder(hwnd, 0x00FF00, toggle:=!toggle)
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)
}

whatEver911
Posts: 1
Joined: 03 Jan 2023, 09:50

Re: Draw border around Active Window

Post by whatEver911 » 03 Jan 2023, 10:01

forAHK wrote:
15 Nov 2022, 00:05
Draw border around Active Window

Code: Select all

#Persistent

; windows in windows 10 has thick invisible boarder which is about 4 pixels wide.

SetTimer, DrawRect, 50
border_thickness = 4

; set the color of the border
border_color = ffae00


DrawRect:

; Get the current window's position 
WinGetPos, x, y, w, h, A
; To avoid the error message
if (x="")
    return

;Gui, +Lastfound +AlwaysOnTop +Toolwindow
Gui, +Lastfound +AlwaysOnTop 

; set the background for the GUI window 
Gui, Color, %border_color%

; remove thick window border of the GUI window
Gui, -Caption

; Retrieves the minimized/maximized state for a window.
WinGet, notMedium , MinMax, A

if (notMedium==0){
; 0: The window is neither minimized nor maximized.

	offset:=0
	outerX:=offset
	outerY:=offset
	outerX2:=w-offset
	outerY2:=h-offset

	innerX:=border_thickness+offset
	innerY:=border_thickness+offset
	innerX2:=w-border_thickness-offset
	innerY2:=h-border_thickness-offset

	newX:=x
	newY:=y
	newW:=w
	newH:=h

	WinSet, Region, %outerX%-%outerY% %outerX2%-%outerY% %outerX2%-%outerY2% %outerX%-%outerY2% %outerX%-%outerY%    %innerX%-%innerY% %innerX2%-%innerY% %innerX2%-%innerY2% %innerX%-%innerY2% %innerX%-%innerY% 

	Gui, Show, w%newW% h%newH% x%newX% y%newY% NoActivate, GUI4Boarder
	return
} else {
    WinSet, Region, 0-0 w0 h0
    return
}

return
Is there any way to hide the GUI4Broader from Windows? I find it cumbersome especially when I switch windows with Alt+Tab, it is not needed.
I tried other border drawers today and some of them don't show a program, however, they have bugs.

User avatar
rommmcek
Posts: 1480
Joined: 15 Aug 2014, 15:18

Re: Draw border around Active Window

Post by rommmcek » 07 Jan 2023, 22:58

Try adding:

Code: Select all

~!TAb:: 
    SetTimer, DrawRect, Off
    KeyWait Alt
    SetTimer, DrawRect, 50
 Return

User avatar
yfjuu6
Posts: 134
Joined: 28 Apr 2023, 15:28

Re: Draw border around Active Window

Post by yfjuu6 » 28 Nov 2023, 04:29

ntepa wrote:
01 Dec 2022, 05:38
Here's another way to draw a border around a window. It's only supported by Windows 11.

Code: Select all

F1::
    hwnd := WinExist("A")
    DrawBorder(hwnd, 0x00FF00, toggle:=!toggle)
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)
}
Hy ntepa!
Do you have the V2 of this?
in V1 It works well But in V2 I've got this error :
Error: This variable has not been assigned a value.
DrawBorder(hwnd, 0x00FF00, toggle := !toggle)

ntepa
Posts: 448
Joined: 19 Oct 2022, 20:52

Re: Draw border around Active Window

Post by ntepa » 28 Nov 2023, 16:58

yfjuujy6 wrote:
28 Nov 2023, 04:29
Do you have the V2 of this?
v2:

Code: Select all

F1:: {
    static toggle := 0
    hwnd := WinExist("A")
    DrawBorder(hwnd, 0x00FF00, toggle:=!toggle)
}
The function DrawBorder is the same.

xavierarmand
Posts: 2
Joined: 18 Jan 2024, 16:02

Re: Draw border around Active Window

Post by xavierarmand » 21 Feb 2024, 01:16

Is is great. Windows get lost in dark mode. ... Is there a way to define outlining particular window be it active or not?

zhotkey
Posts: 14
Joined: 21 Jun 2017, 08:22

Re: Draw border around Active Window

Post by zhotkey » 21 Feb 2024, 12:44

The border doesn't show if the window is maximized.

Post Reply

Return to “Scripts and Functions (v1)”