Advertisement
anonymous1184

SuspendResume.ahk

Aug 8th, 2023 (edited)
1,419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Requires AutoHotkey v1.1
  2.  
  3. SetTitleMatchMode 2
  4.  
  5. SuspendMode := -1
  6. ; -1 = Minimize
  7. ;  0 = Do nothing
  8. ;  1 = Hide
  9.  
  10. Exit ; End of auto-execute
  11.  
  12. 1::SuspendResume.Window(SuspendMode, "A") ; Active window
  13. 2::SuspendResume.Menu()
  14.  
  15. 8::SuspendResume.Window(-1, "— Mozilla Firefox") ; Minimize+Suspend Firefox
  16. 9::SuspendResume.Window( 0, "— Mozilla Firefox") ; Suspend Firefox
  17. 0::SuspendResume.Window( 1, "— Mozilla Firefox") ; Hide+Suspend Firefox
  18.  
  19. class SuspendResume {
  20.  
  21.     Menu() {
  22.         IniRead handles, % A_Temp "\sr.ini", SUSPENDED
  23.         handles := StrSplit(handles, "`n")
  24.         Menu srMenu, UseErrorLevel
  25.         Menu srMenu, Add, Suspended Processes, Array ; Dummy NOP
  26.         Menu srMenu, Icon, 1&, imageres.dll, 145
  27.         Menu srMenu, Add ; Separator
  28.         suspendedCount := 0
  29.         dwh := A_DetectHiddenWindows
  30.         DetectHiddenWindows On
  31.         for _, line in handles {
  32.             pair := StrSplit(line, "=", , 2)
  33.             if (pair[1] && WinExist(pair[2] " ahk_id " pair[1])) {
  34.                 cb := ObjBindMethod(this, "_toggle", pair[1], false)
  35.                 Menu srMenu, Add, % pair[2], % cb
  36.                 WinGet exe, ProcessPath
  37.                 Menu srMenu, Icon, % pair[2], % exe
  38.                 suspendedCount++
  39.             } else {
  40.                 IniDelete % A_Temp "\sr.ini", SUSPENDED, % pair[1]
  41.             }
  42.         }
  43.         DetectHiddenWindows % dwh
  44.         if (suspendedCount > 0) {
  45.             Menu srMenu, Show
  46.         } else {
  47.             MsgBox 0x40010, Error, There are no suspended processes.
  48.         }
  49.         Menu srMenu, DeleteAll
  50.     }
  51.  
  52.     Window(Mode, WinTitle*) {
  53.         dwh := A_DetectHiddenWindows
  54.         DetectHiddenWindows On
  55.         hWnd := WinExist(WinTitle*)
  56.         if (hWnd = 0) {
  57.             throw Exception("Target not found.", -1)
  58.         }
  59.         WinGet exe, ProcessName
  60.         if (exe = "Explorer.exe") {
  61.             MsgBox 0x40030, Attention!, Shell processes shouldn't be suspended.
  62.             return
  63.         }
  64.         isVisible := DllCall("IsWindowVisible", "Ptr", hWnd)
  65.         WinGetTitle title
  66.         IniWrite % title, % A_Temp "\sr.ini", SUSPENDED, % hWnd
  67.         if (Mode = -1) {
  68.             WinMinimize
  69.         } else if (Mode = 1 && isVisible) {
  70.             WinHide
  71.         }
  72.         DetectHiddenWindows % dwh
  73.         this._toggle(hWnd, isVisible)
  74.     }
  75.  
  76.     _toggle(hWnd, bSuspend) {
  77.         static cache := {}
  78.             , PROCESS_SUSPEND_RESUME := 0x0800
  79.         dwh := A_DetectHiddenWindows
  80.         DetectHiddenWindows On
  81.         WinGet appPid, PID, % "ahk_id" hWnd
  82.         if (cache.HasKey(appPid)) {
  83.             hProc := cache[appPid]
  84.         } else {
  85.             hProc := DllCall("OpenProcess", "UInt", PROCESS_SUSPEND_RESUME, "Int", false, "UInt", appPid, "Ptr")
  86.             if (hProc = 0) {
  87.                 WinGet exe, ProcessName
  88.                 throw Exception("Couldn't open process for suspension.", -1, exe)
  89.             }
  90.         }
  91.         if (bSuspend) {
  92.             DllCall("ntdll\NtSuspendProcess", "Ptr", hProc)
  93.             cache[appPid] := hProc
  94.         } else {
  95.             DllCall("ntdll\NtResumeProcess", "Ptr", hProc)
  96.             DllCall("CloseHandle", "Ptr", hProc)
  97.             cache.Delete(appPid)
  98.             WinExist("ahk_id" hWnd)
  99.             WinGet state, MinMax
  100.             if (state != -1) {
  101.                 WinShow
  102.                 WinActivate
  103.             }
  104.             IniDelete % A_Temp "\sr.ini", SUSPENDED, % hWnd
  105.         }
  106.         DetectHiddenWindows % dwh
  107.     }
  108.  
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement