1

I am a autohotkey novice, but i've found this kind of useful script for putting a red border around my active window making it more noticeable. I've installed autohotkey and run the script, however I have two issues:

  1. It doesnt work when maximised nor when the window is snapped to one side
  2. The border is not even

I have attached some pics showing the problem: picutre showing problems

If someone could please help & fix this script, or provide detailed instructions of how to fix it (please bear in mind I am a novice) I would be grateful.

#Persistent

SetTimer, DrawRect, 50
border_thickness = 10
border_color = FF0000

DrawRect:
WinGetPos, x, y, w, h, A
if (x="")
    return
Gui, +Lastfound +AlwaysOnTop +Toolwindow
iw:= w+4
ih:= h + 4
w:=w+ 8
h:=h + 8
x:= x - border_thickness
y:= y - border_thickness
Gui, Color, FF0000
Gui, -Caption
WinSet, Region, 0-0 %w%-0 %w%-%h% 0-%h% 0-0 %border_thickness%-%border_thickness% %iw%-%border_thickness% %iw%-%ih% %border_thickness%-%ih% %border_thickness%-%border_thickness%
Gui, Show, w%w% h%h% x%x% y%y% NoActivate, Table awaiting Action
return
3

I would recommend renaming the variables to make sense. The WinSet statement draws two boxes in pairs of x/y coordinates so naming the variables in x/y coordinate pairs and assigning them prior to the WinSet statement is less confusing

In this particular example you are also drawing the border around the outside of the window. When the outside of the window is on the edge of the screen, the border is off-screen, so it will not be visible. If you want to draw it on-screen when the border is at (or even off of) the edge of the screen, you will need to draw the border inside (or partially inside) the window boundary vs. drawing it outside the window boundary.

That's only a partial solution though because in a maximized state, the window is even further off the screen than just being at the edge(s). In that case you have to detect a maximized condition and offset the border even further than it would normally be by a certain number of pixels.

I tweaked renamed the variables and tweaked your code to give an example. You can change the different 'border type' to see the effects... the rest is just math if you want to draw it differently.

#Persistent

SetTimer, DrawRect, 50
border_thickness = 5
border_color = FF0000

DrawRect:
WinGetPos, x, y, w, h, A
if (x="")
    return
Gui, +Lastfound +AlwaysOnTop +Toolwindow

borderType:="inside"                ; set to inside, outside, or both

if (borderType="outside") { 
    outerX:=0
    outerY:=0
    outerX2:=w+2*border_thickness
    outerY2:=h+2*border_thickness

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

    newX:=x-border_thickness
    newY:=y-border_thickness
    newW:=w+2*border_thickness
    newH:=h+2*border_thickness

} else if (borderType="inside") {   
    WinGet, myState, MinMax, A
    if (myState=1)
        offset:=8
    else 
        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



} else if (borderType="both") { 
    outerX:=0
    outerY:=0
    outerX2:=w+2*border_thickness
    outerY2:=h+2*border_thickness

    innerX:=border_thickness*2
    innerY:=border_thickness*2
    innerX2:=w
    innerY2:=h

    newX:=x-border_thickness
    newY:=y-border_thickness
    newW:=w+4*border_thickness
    newH:=h+4*border_thickness
}



Gui, Color, %border_color%
Gui, -Caption

;WinSet, Region, 0-0 %w%-0 %w%-%h% 0-%h% 0-0 %border_thickness%-%border_thickness% %iw%-%border_thickness% %iw%-%ih% %border_thickness%-%ih% %border_thickness%-%border_thickness%
 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%w% h%h% x%x% y%y% NoActivate, Table awaiting Action
Gui, Show, w%newW% h%newH% x%newX% y%newY% NoActivate, Table awaiting Action
return

Also, just for info, there are also ways to hook the windows callback events for window move events so you don't have to use a 50ms timer that's always updating. In that case it would only update when the window moves, and the timer comes with visible lag and processing overhead. But the timer is the easier solution by far to implement out of the gate (as you have done already), and in many cases it's not really worth the extra complexity to hook the window events if you don't need to. Just letting you know that's possible though.

3
  • 1
    This is amazing! Thank you so much, not just for the script but also for explaining everything. Legend. – John Mar 22 '17 at 13:19
  • 1
    MUCH easier to understand and more flexible.Only improvement - you defined "border_color" at the top but then used "Gui, Color, FF0000" at the end. Shouldn't this be "Gui, Color, %border_color%"? – Hornblower409 Feb 18 '19 at 19:13
  • 1
    This is very good, and very useful, although I note that the border it draws is itself a clickable object. Is there any way to script it so that it passes the click through to the window underneath, instead of intercepting it? Right now, if you click on the border itself, that window loses focus. – Len Mar 11 '20 at 5:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.