Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Requires AutoHotkey v1.1
- SetTitleMatchMode 2
- SuspendMode := -1
- ; -1 = Minimize
- ; 0 = Do nothing
- ; 1 = Hide
- Exit ; End of auto-execute
- 1::SuspendResume.Window(SuspendMode, "A") ; Active window
- 2::SuspendResume.Menu()
- 8::SuspendResume.Window(-1, "— Mozilla Firefox") ; Minimize+Suspend Firefox
- 9::SuspendResume.Window( 0, "— Mozilla Firefox") ; Suspend Firefox
- 0::SuspendResume.Window( 1, "— Mozilla Firefox") ; Hide+Suspend Firefox
- class SuspendResume {
- Menu() {
- IniRead handles, % A_Temp "\sr.ini", SUSPENDED
- handles := StrSplit(handles, "`n")
- Menu srMenu, UseErrorLevel
- Menu srMenu, Add, Suspended Processes, Array ; Dummy NOP
- Menu srMenu, Icon, 1&, imageres.dll, 145
- Menu srMenu, Add ; Separator
- suspendedCount := 0
- dwh := A_DetectHiddenWindows
- DetectHiddenWindows On
- for _, line in handles {
- pair := StrSplit(line, "=", , 2)
- if (pair[1] && WinExist(pair[2] " ahk_id " pair[1])) {
- cb := ObjBindMethod(this, "_toggle", pair[1], false)
- Menu srMenu, Add, % pair[2], % cb
- WinGet exe, ProcessPath
- Menu srMenu, Icon, % pair[2], % exe
- suspendedCount++
- } else {
- IniDelete % A_Temp "\sr.ini", SUSPENDED, % pair[1]
- }
- }
- DetectHiddenWindows % dwh
- if (suspendedCount > 0) {
- Menu srMenu, Show
- } else {
- MsgBox 0x40010, Error, There are no suspended processes.
- }
- Menu srMenu, DeleteAll
- }
- Window(Mode, WinTitle*) {
- dwh := A_DetectHiddenWindows
- DetectHiddenWindows On
- hWnd := WinExist(WinTitle*)
- if (hWnd = 0) {
- throw Exception("Target not found.", -1)
- }
- WinGet exe, ProcessName
- if (exe = "Explorer.exe") {
- MsgBox 0x40030, Attention!, Shell processes shouldn't be suspended.
- return
- }
- isVisible := DllCall("IsWindowVisible", "Ptr", hWnd)
- WinGetTitle title
- IniWrite % title, % A_Temp "\sr.ini", SUSPENDED, % hWnd
- if (Mode = -1) {
- WinMinimize
- } else if (Mode = 1 && isVisible) {
- WinHide
- }
- DetectHiddenWindows % dwh
- this._toggle(hWnd, isVisible)
- }
- _toggle(hWnd, bSuspend) {
- static cache := {}
- , PROCESS_SUSPEND_RESUME := 0x0800
- dwh := A_DetectHiddenWindows
- DetectHiddenWindows On
- WinGet appPid, PID, % "ahk_id" hWnd
- if (cache.HasKey(appPid)) {
- hProc := cache[appPid]
- } else {
- hProc := DllCall("OpenProcess", "UInt", PROCESS_SUSPEND_RESUME, "Int", false, "UInt", appPid, "Ptr")
- if (hProc = 0) {
- WinGet exe, ProcessName
- throw Exception("Couldn't open process for suspension.", -1, exe)
- }
- }
- if (bSuspend) {
- DllCall("ntdll\NtSuspendProcess", "Ptr", hProc)
- cache[appPid] := hProc
- } else {
- DllCall("ntdll\NtResumeProcess", "Ptr", hProc)
- DllCall("CloseHandle", "Ptr", hProc)
- cache.Delete(appPid)
- WinExist("ahk_id" hWnd)
- WinGet state, MinMax
- if (state != -1) {
- WinShow
- WinActivate
- }
- IniDelete % A_Temp "\sr.ini", SUSPENDED, % hWnd
- }
- DetectHiddenWindows % dwh
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement