Skip to content

Instantly share code, notes, and snippets.

@BenGoldberg1
Created February 26, 2021 02:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BenGoldberg1/74b0d7e55851059b73df88351b839f7b to your computer and use it in GitHub Desktop.
Save BenGoldberg1/74b0d7e55851059b73df88351b839f7b to your computer and use it in GitHub Desktop.
Borderless window mode which cleans up after itself
;;; Known issues:
;;;
;;; - Weird results for windows with custom decorations such as
;;; Chrome, or programs with a Ribbon interface.
;;; - Emacs will be maximized behind instead of in front of
;;; the taskbar. Workaround: WinHide ahk_class Shell_TrayWnd
global id2undoer := Object()
#SingleInstance force
; #notrayicon ; uncomment if you don't want an H in you tasktray
#Warn All
#NoEnv
Class Undoer {
__New(id) {
;MsgBox "In __New, id is ", %id%
this.id := id
WinGet, s, Style, A
this.s := s
WinGetPos, x, y, w, h, ahk_id %id%
this.x := x, this.y := y, this.w := w, this.h := h
return this
}
; Originally, this was __Delete, but sadly, it
; does not get called when I wanted it to.
Undo() {
id := this.id
;MsgBox, "In Undo, id is ", %id%
s := this.s
WinSet, Style, %s%, ahk_id %id%
x := this.x, y := this.y, w := this.w, h := this.h
WinMove, ahk_id %id%,, %x%, %y%, %w%, %h%
}
}
ToggleFakeFullscreen()
{
CoordMode Screen, Window
static UNDECORATED := -0xC40000
WinGet, id, ID, A
if ( id2undoer.HasKey(id) )
{
id2undoer[id].Undo()
id2undoer.Delete(id)
}
else
{
id2undoer[id] := new Undoer(id)
WinSet, Style, %UNDECORATED%, ahk_id %id%
SysGet, mon, Monitor, % GetMonitorActiveWindow()
WinMove, A,, %monLeft%, %monTop%, % monRight-monLeft, % monBottom-monTop
}
}
GetMonitorAtPos(x,y)
{
;; Monitor number at position x,y or -1 if x,y outside monitors.
SysGet monitorCount, MonitorCount
i := 0
while(i < monitorCount)
{
SysGet area, Monitor, %i%
if ( areaLeft <= x && x <= areaRight && areaTop <= y && y <= areaBottom )
{
return i
}
msgbox "x", x, "L", areaLeft, "R", areaRight
msgbox "y", y, "T", areaTop, "B", areaBottom
i := i+1
}
msgbox "fallthrough"
return -1
}
GetMonitorActiveWindow(){
;; Get Monitor number at the center position of the Active window.
WinGetPos x,y,width,height, A
return GetMonitorAtPos(x+width/2, y+height/2)
}
MyOnExit(){
for id, info in id2undoer {
info.Undo()
}
id2undoer := Object()
return 0
}
OnExit( "MyOnExit", 1 )
; Set up hot keys so that either alt-f11 or win-f11 will
; Toggle whether a program is in borderless windowed mode,
!F11::ToggleFakeFullscreen()
#F11::ToggleFakeFullscreen()
; And a hot key so that typing alt-win-f11 or win-alt-f11
; will cause this autohotkey script to exit.
!#F11::ExitApp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment