Advertisement
pk3456

Toggle Borderless Fullscreen

Mar 27th, 2023 (edited)
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Autohotkey 8.09 KB | Source Code | 0 0
  1. !Enter::FullScreen("A") ; press alt enter to fullscreen current window
  2.  
  3. ; toggle fullscreen
  4. ; parameters are same as WinExist
  5. Fullscreen(winTitle*) {
  6.     static prevX             := "AHK:Fullscreen.prevX"
  7.     static prevY             := "AHK:Fullscreen.prevY"
  8.     static prevW             := "AHK:Fullscreen.prevW"
  9.     static prevH             := "AHK:Fullscreen.prevH"
  10.     static prevStyle         := "AHK:Fullscreen.prevStyle"
  11.     static IsFullscreen      := "AHK:Fullscreen.IsFullscreen"
  12.     static IsSwitching       := "AHK:Fullscreen.SwitchingMonitor"
  13.     static SPI_GETWORKAREA   := 0x30
  14.     static SPI_SETWORKAREA   := 0x2F
  15.     static WS_EX_TRANSPARENT := 0x00000020
  16.     static WS_EX_LAYERED     := 0x00080000
  17.     static WS_EX_COMPOSITED  := 0x02000000
  18.     static WS_SIZEBOX        := 0x00040000
  19.     static WS_SYSMENU        := 0x00080000
  20.     static WS_CAPTION        := 0x00C00000
  21.     static WS_MAXIMIZE       := 0x01000000
  22.     static g := (
  23.         g := Gui("+AlwaysOnTop -Caption +ToolWindow -DPIScale +Border +E" WS_EX_TRANSPARENT|WS_EX_COMPOSITED|WS_EX_LAYERED),
  24.         g.MarginX := 5,
  25.         g.BackColor := 0x1A1A1A,
  26.         g.SetFont("s18 cWhite", "Segoe UI SemiBold"),
  27.         g.Add("Text", "vText w230 h35 Center"),
  28.         g
  29.     )
  30.  
  31.     if !hwnd := WinExist(winTitle*)
  32.         return
  33.  
  34.     if WinExist("ahk_id " hwnd " ahk_exe ApplicationFrameHost.exe")
  35.     {
  36.         WinActivate hwnd
  37.         Send "#+{Enter}"
  38.         return
  39.     }
  40.     ; static _ := CreateF11Group()
  41.     ; CreateF11Group() {
  42.     ;     GroupAdd "FullscreenF11", "ahk_class ahk_class Chrome_WidgetWin_1"
  43.     ;     GroupAdd "FullscreenF11", "ahk_class ahk_class MozillaWindowClass"
  44.     ;     return
  45.     ; }
  46.     ; if WinExist("ahk_id " hwnd " ahk_group FullscreenF11")
  47.     ; {
  48.     ;     WinActivate(hwnd)
  49.     ;     Send "{F11}"
  50.     ;     return
  51.     ; }
  52.  
  53.     ; create hotkey to move fullscreen window to next monitor
  54.     HotIfWinActive "ahk_id " hwnd
  55.     Hotkey "$#+Left", MoveWindowToNextMonitor, "On"
  56.     Hotkey "$#+Right", MoveWindowToNextMonitor, "On"
  57.  
  58.     ; create a hotkey to keep the window on top of tray if LWin is pressed
  59.     HotIf MouseIsOverFullScreenWindow
  60.     Hotkey "~LWin", KeepWindowOnTopOfTray, "On"
  61.  
  62.     mon := GetNearestMonitor(hwnd)
  63.     monSwitch := WinGetProp(hwnd, IsSwitching)
  64.     SetWinDelay -1
  65.  
  66.     if !WinGetProp(hwnd, IsFullscreen) {
  67.         if mon.Primary {
  68.             NumPut("int", A_ScreenWidth, "int", A_ScreenHeight, rc := Buffer(16, 0), 8)
  69.             DllCall("SystemParametersInfo", "uint", SPI_SETWORKAREA, "UInt", 0, "Ptr", rc, "UInt", 0)
  70.             MoveTrayBehindWindow(hwnd)
  71.         } else {
  72.             RestoreWorkingArea()
  73.         }
  74.         wp := GetWindowPlacement(hwnd)
  75.         if !monSwitch {
  76.             WinGetPos(, &winY,,, hwnd)
  77.             WinGetClientPos(, &clientY,,, hwnd)
  78.             WinSetProp(hwnd, prevStyle, WinGetStyle(hwnd))
  79.             WinSetStyle "-" ((clientY-winY>8) ? WS_CAPTION|WS_SIZEBOX : WS_SYSMENU), hwnd ; remove title bar
  80.         }
  81.         WinMove(mon.Left, mon.Top, mon.Width, mon.Height, hwnd)
  82.         WinSetStyle("+" WS_MAXIMIZE, hwnd)
  83.         SetWindowPlacement(hwnd, wp.X, wp.Y, wp.W, wp.H)
  84.         WinSetProp(hwnd, IsFullscreen, 1, prevX, wp.X, prevY, wp.Y, prevW, wp.W, prevH, wp.H)
  85.         g["Text"].Text := "FullScreen Enabled"
  86.     } else {
  87.         ; turn off hotkeys
  88.         HotIfWinActive "ahk_id " hwnd
  89.         Hotkey "$#+Left", "Off"
  90.         Hotkey "$#+Right", "Off"
  91.  
  92.         HotIf MouseIsOverFullScreenWindow
  93.         Hotkey "~LWin", "Off"
  94.  
  95.         RestoreWorkingArea()
  96.         X := WinGetProp(hwnd, prevX), Y := WinGetProp(hwnd, prevY), W := WinGetProp(hwnd, prevW), H := WinGetProp(hwnd, prevH)
  97.         style := WinGetProp(hwnd, prevStyle)
  98.         WinSetStyle(style, hwnd)
  99.         if !WinGetMinMax(hwnd) = 1
  100.             WinMove(X, Y, W, H, hwnd)
  101.         else if mon.Primary {
  102.             WinGetPos ,,,&H, GetTray()
  103.             WinMove(,,,mon.Height - H,hwnd)
  104.         }
  105.         ; Put tray on top
  106.         DllCall("SetWindowPos", "ptr", GetTray(), "int", -1, "int", 0, "int", 0, "int", 0, "int", 0, "uint", 0x4000|0x0010|0x0002|0x0001) ; SWP_ASYNCWINDOWPOS|SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOSIZE
  107.         WinSetProp hwnd, IsFullscreen, 0
  108.         g["Text"].Text := "FullScreen Disabled"
  109.     }
  110.     if !monSwitch {
  111.         g.Show("NA x" Mon.Left + Mon.Width//2 - 210/2 " y" Mon.Top + 30)
  112.         SetTimer () => g.Hide(), -1000
  113.     }
  114.  
  115.     static RestoreWorkingArea() {
  116.         DllCall("SystemParametersInfo", "uint", SPI_GETWORKAREA, "uint", 0, "ptr", rc := Buffer(16), "uint", 0)
  117.         if NumGet(rc, 12, "int") = A_ScreenHeight { ; if working area height is same as A_ScreenHeight
  118.             WinGetPos(,,, &H, GetTray())
  119.             NumPut("int", A_ScreenHeight - H, rc, 12)
  120.             DllCall("SystemParametersInfo", "uint", SPI_SETWORKAREA, "uint", 0, "ptr", rc, "uint", 0)
  121.         }
  122.     }
  123.  
  124.     static KeepWindowOnTopOfTray(*) {
  125.         hwnd := WinExist() ; Get last found window from HotIf
  126.         KeyWait "LWin"
  127.         sleep 250
  128.         tray := GetTray()
  129.         for win in WinGetList() {
  130.             if win = hwnd
  131.                 break
  132.             if win = tray {
  133.                 MoveTrayBehindWindow(hwnd)
  134.                 break
  135.             }
  136.         }
  137.     }
  138.  
  139.     ; get hwnd of taskbar
  140.     static GetTray() => DllCall("FindWindow", "str", "Shell_TrayWnd", "str", "")
  141.  
  142.     ; keep window above taskbar when fullscreen
  143.     static MoveTrayBehindWindow(hwnd) {
  144.         DllCall("SetWindowPos",
  145.         "ptr", GetTray(),
  146.         "ptr", hwnd,
  147.         "int", 0, "int", 0, "int", 0, "int", 0,
  148.         "uint", 0x4000|0x0010|0x0002|0x0001) ; SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOSIZE
  149.     }
  150.  
  151.     static MouseIsOverFullScreenWindow(*) {
  152.         MouseGetPos ,,&hwnd
  153.         if WinGetProp(hwnd, IsFullscreen)
  154.             return WinExist(hwnd)
  155.     }
  156.  
  157.     static MoveWindowToNextMonitor(ThisHotkey) {
  158.         hwnd := WinExist()
  159.         Send "{Blind}#+" (InStr(ThisHotkey, "Left") ? "{Left}" : "{Right}")
  160.         Sleep 250
  161.         WinSetProp hwnd, IsFullscreen, 0, IsSwitching, 1
  162.         FullScreen hwnd
  163.         WinSetProp hwnd, IsSwitching, 0
  164.     }
  165.  
  166.     ; save properties on a window
  167.     static WinSetProp(hwnd, propValue*) {
  168.         if propValue.Length & 1
  169.             throw Error("Invalid number of parameters.", -1)
  170.         loop propValue.Length // 2 {
  171.             prop := propValue[A_Index*2 - 1], value := propValue[A_Index*2]
  172.             if value > 0xFFFFFFFF
  173.                 throw Error("value is too big", -1, value)
  174.             DllCall("SetProp", "ptr", hwnd, "str", prop, "int", value)
  175.         }
  176.     }
  177.     static WinGetProp(hwnd, name) {
  178.         return DllCall("GetProp", "ptr", hwnd, "str", name) & 0xFFFFFFFF
  179.     }
  180.  
  181.     static GetNearestMonitor(hwnd) {
  182.         static MONITOR_DEFAULTTONEAREST  := 0x00000002
  183.         NumPut("uint", 40, monInfo := Buffer(40))
  184.         DllCall("GetMonitorInfo", "ptr", DllCall("MonitorFromWindow", "ptr", hwnd, "uint", MONITOR_DEFAULTTONEAREST), "ptr", monInfo)
  185.         return {
  186.             Left    : L := NumGet(monInfo, 4, "int"),
  187.             Top     : T := NumGet(monInfo, 8, "int"),
  188.             Width   : NumGet(monInfo, 12, "Int") - L,
  189.             Height  : NumGet(monInfo, 16, "int") - T,
  190.             Primary : NumGet(monInfo, 36, "uint")
  191.         }
  192.     }
  193.  
  194.     static GetWindowPlacement(hwnd) {
  195.         NumPut("uint", 44, WP := Buffer(44, 0))
  196.         DllCall("GetWindowPlacement", "ptr", hwnd, "ptr", WP)
  197.         return {
  198.             SW : NumGet(WP, 8, "uint"), ; showCmds - current show state of the window
  199.             X  : X := NumGet(WP, 28, "Int"),
  200.             Y  : Y := NumGet(WP, 32, "Int"),
  201.             W  : NumGet(WP, 36, "Int") - X,
  202.             H  : NumGet(WP, 40, "Int") - Y,
  203.         }
  204.     }
  205.  
  206.     static SetWindowPlacement(hwnd, X, Y, W, H) {
  207.         NumPut("uint", 44, WP := Buffer(44, 0))
  208.         DllCall("GetWindowPlacement", "Ptr", hwnd, "Ptr", WP)
  209.         , NumPut("int", X, WP, 28)
  210.         , NumPut("int", Y, WP, 32)
  211.         , NumPut("int", W + X, WP, 36)
  212.         , NumPut("int", H + Y, WP, 40)
  213.         DllCall("SetWindowPlacement", "ptr", hwnd, "ptr", WP)
  214.     }
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement