help me fix my script

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
rubaboo
Posts: 10
Joined: 02 Jan 2018, 21:18

help me fix my script

21 Apr 2018, 00:49

I cobbled together something (copy/paste from a bunch of scripts I googled up), and, well, it doesn't work. I don't know what I'm doing [wrong] :?
What the script is supposed to do is draw a super-thick (enough to cover the rest of the desktop spannig two displays) semi-transparent border around an active window. Like Ghoster, but a different approach.
Please help me get the script to a working condition. I think it's worth mentioning here that my secondary display is in portrait mode, with the upper edge higher than that of the primary display, which results in negative Y-coordinates of any point on that screen that is above the upper edge of the primary display.

Ideally (but that is beyond my current skill level), it would be awesome if the border only covered the display the window is on, and there was another border around a window that was last active on the other display.

Code: Select all

#Persistent

OnExit, ExitSub

Menu, Tray, Icon, %A_WorkingDir%\icons\activeWinHighlight.ahk.ico

border_thickness = 2730 ; 1680+1050, enough for two displays, one in portrait mode
border_color = 000000
transparency = 88
border_type = outside                ; set to inside, outside, or both

Gui, +HwndRG +Toolwindow +Disabled -SysMenu -Caption +E0x20
Gui, Color, %border_color%
WinSet,Transparent,%transparency%, ahk_id %RG%

SetTimer, R, 80
Return

R:

KeyWait, LButton               ; in case a window is being moved/resized

; See if there is a change in the active window
if (id <> WinExist("A"))
{
  id := WinExist("A")
  WinGet m, MinMax             ; 1=maximized, 0=restored, -1=minimized
  WinGetPos, x, y, w, h, A
  WinGet xs, ExStyle
  Goto D
}
else
{
  WinGet mc, MinMax            ; 1=maximized, 0=restored, -1=minimized
  if (m <> mc)
  {
    m := mc
    WinGetPos, x, y, w, h, A
    WinGet xs, ExStyle
    Goto D
  }
  else
  {
    WinGetPos, xc, yc, wc, hc, A
    if (x <> xc or y <> yc or w <> wc or h <> hc)
    {
      x := xc
      y := yc
      w := wc
      h := hc
      WinGet xs, ExStyle
      Goto D
    }
    else
    {
      WinGet xsc, ExStyle
      if (xs <> xsc)
      {
        xs := xsc
        Goto D
      }

    }
  }
}

Return

D:

if w > 0
{
  if (border_type="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 (border_type="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 (border_type="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, Hide ; Do I need this? 
  WinSet, Region, %outerX%-%outerY% %outerX2%-%outerY% %outerX2%-%outerY2% %outerX%-%outerY2% %outerX%-%outerY%   %innerX%-%innerY% %innerX2%-%innerY% %innerX2%-%innerY2% %innerX%-%innerY2% %innerX%-%innerY%, ahk_id %RG%
  Gui, +AlwaysOnTop
  Gui, Show, w%newW% h%newH% x%newX% y%newY% NA
  Gui, -AlwaysOnTop
}

Return

^+q::Goto ExitSub

ExitSub:
Gui, Destroy
ExitApp
rubaboo
Posts: 10
Joined: 02 Jan 2018, 21:18

Re: help me fix my script

21 Apr 2018, 13:54

Actually, I was pretty close. I was just missing +LastFound before WinGet in a couple of spots.

This seems to work reasonably well, and with less flickering than Ghoster:

Code: Select all

#Persistent

OnExit, ExitSub

Menu, Tray, Icon, %A_WorkingDir%\icons\activeWinHighlight.ahk.ico

border_thickness = 2730
border_color = 000000
transparency = 88
border_type = outside                ; set to inside, outside, or both

Gui, +HwndRG +LastFound +Toolwindow +Disabled -SysMenu -Caption +E0x20
Gui, Color, %border_color%
WinSet,Transparent,%transparency%

SetTimer, R, 180
Return

R:

KeyWait, LButton               ; in case a window is being moved/resized
KeyWait, RButton
KeyWait, LWin                  ; when a window is moved using KDE Mover-Sizer, the above does not work, so we wait for Mover-Sizer hotkey as well, which is currently set to LWin

if (id <> WinExist("A"))
{
  id := WinExist("A")
  WinGet m, MinMax             ; 1=maximized, 0=restored, -1=minimized
  WinGetPos, x, y, w, h, A
  WinGet xs, ExStyle
  Goto D
}
else
{
  WinGet mc, MinMax            ; 1=maximized, 0=restored, -1=minimized
  if (m <> mc)
  {
    m := mc
    WinGetPos, x, y, w, h, A
    WinGet xs, ExStyle
    Goto D
  }
  else
  {
    WinGetPos, xc, yc, wc, hc, A
    if (x <> xc or y <> yc or w <> wc or h <> hc)
    {
      x := xc
      y := yc
      w := wc
      h := hc
      WinGet xs, ExStyle
      Goto D
    }
    else
    {
      WinGet xsc, ExStyle
      if (xs <> xsc)
      {
        xs := xsc
        Goto D
      }

    }
  }
}

Return

D:

WinGet pid, PID
WinGet p, ProcessName
WinGetTitle, t
If RegExMatch(p, "i)^explorer.exe$") && RegExMatch(t, "^$|^Program Manager$|^Start menu$")   ; ^$ is equivalent to \A\z, unless "m)" option is used; it means empty title, which can be Taskbar or hidden notification icons
   || RegExMatch(p, "^AutoHotkey.exe$") && RegExMatch(t, "^DrawOnScreen.ahk$")
{
  Gui, Hide
  Return
}

if w > 0
{
  if (border_type="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 (border_type="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 (border_type="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, Hide
  Gui, +LastFound +AlwaysOnTop
  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% NA
  Gui, -AlwaysOnTop
}

Return

^+q::Goto ExitSub

ExitSub:
Gui, Destroy
ExitApp  ; A script with an OnExit subroutine will not terminate unless the subroutine uses ExitApp.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: mankvl, OrangeCat, sanmaodo, zerox and 316 guests