CodeQuickTester icon indicating copy to clipboard operation
CodeQuickTester copied to clipboard

enclosing a task manager

Open superuser7777 opened this issue 3 years ago • 0 comments

How about enclosing a task manager function?

#SingleInstance, FORCE
#NoEnv
#NoTrayIcon

SetTitleMatchMode, 1
DetectHiddenWindows, On

;Valiables Setup
    app_title = AhkTaskMan

    ; column number
    col_file      =   1
    col_pid       =   2
    col_mem       =   3
    col_exe       =   4
    col_ver       =   5
    col_hwnd      =   6
    col_dir       =   7

    ; header label
    label_file    =  ; スクリプト
    label_pid     =  ; PID
    label_mem     =  ; Mem(kb)
    label_exe     =  ; プロセス名
    label_ver     =  ; バージョン
    label_hwnd    =  ; ウィンドウハンドル
    label_dir     =  ; スクリプト位置

    ; column options
    opt_file      =   120
    opt_pid       =   50
    opt_mem       =   70 Integer
    opt_exe       =   120
    opt_ver       =   90
    opt_hwnd      =   70
    opt_dir       =   200

    refresh_dur   =   5000 ; (ms)

    use_comma     =   1

    refresh_timer =   0


Init:
    Gosub, InitGui
    Gosub, ListViewColumnSetUp
    Gosub, SB_Show
    monitor:=true
    Gosub, RefleshView
    monitor:=refresh_timer
Return

RefleshView:
    Thread, Interrupt, 1000
    Thread, Priority, 10000
    If !(monitor)
        Return
    pidList := "|"
    row:=LV_GetCount()
    ; reverse loop for deleting row(s)
    While (row>0) {
        LV_GetText(pid, row, col_pid)
        pidList.=pid . "|"
        Process, Exist, %pid%
        If (ErrorLevel!=pid)
            LV_Delete(row)
        else
            LV_Modify(row, "Col" col_mem, GetMem(pid))
        row--
    }
    WinGet, list, List, ahk_class AutoHotkey,,%A_ScriptFullPath%
    Loop, % list {
        hwnd:=list%A_Index%
        IfWinNotExist, ahk_id %hwnd%
            continue
        WinGet, pid, PID
        If pid && InStr(pidList, "|" pid "|")
            continue
        WinGetTitle, title
        If RegExMatch(title, "(?P<path>.+) - AutoHotkey v(?P<ver>\d(?:\.\w+)+)$", $) {
            path := $path, ver:=$ver
        } else {
            path := title, ver:=""
        }
        WinGet, exe, ProcessName
        mem := GetMem(pid)
        SplitPath, path, file, dir
        val%col_pid%  := pid
        val%col_file% := file
        val%col_mem%  := mem
        val%col_exe%  := exe
        val%col_ver%  := ver
        val%col_dir%  := dir
        val%col_hwnd% := hwnd
        LV_Add("", val1, val2, val3, val4, val5, val6, val7)
    }
    SB_SetText("Process:" A_Tab A_Tab LV_GetCount() " ", 2)
Return


InitGui:
    
    Gui, +LastFound +Resize
    
    Gui, Margin, 0, 0
    Gui, Add, ListView,  vLV gLvEvent r8 w620 grid AltSubmit -Multi
    Gui, Add, StatusBar, vSB gSbEvent 0x800
    SB_SetParts(100, 100)

    Menu, Tray, NoStandard
    Menu, Tray, Add,     &Restore, RestoreGui
    Menu, Tray, Add,     E&xit,    EndApp
    Menu, Tray, Default, &Restore
    
    Menu, M1, Add, Show &Main Window       , ShowMainWindow
    Menu, M1, Add
    Menu, M1, Add, &Reload Script          , BypassMenu
    Menu, M1, Add, &Suspend Hotkeys        , BypassMenu
    Menu, M1, Add, &Pause                  , BypassMenu
    Menu, M1, Add, E&xit (Terminate Script), BypassMenu
    Menu, M1, Add,
    Menu, M1, Add, &Kill Process           , KillProcess
    Menu, M1, Add,
    Menu, M1, Add, &Cancel                 , MenuCancel
    
    Gui, Show,, %app_title%
Return

ListViewColumnSetUp:
    col_headers = 
        (LTrim Join|
            file
            pid
            mem
            exe
            ver
            hwnd
            dir
        )
    Loop, 7
    {
        idx:=A_Index
        Loop, PARSE, col_headers, |, %A_Tab%%A_Space%
        {
            If (col_%A_LoopField%==idx) 
            {
                LV_InsertCol(idx, opt_%A_LoopField%, label_%A_LoopField% ? label_%A_LoopField% : A_LoopField)
            }
        }
    }
Return

RestoreGui:
    Menu, Tray, NoIcon
    Gui, Show
Return

EndApp:
GuiClose:
    ExitApp
Return

GuiSize:
    If (A_EventInfo==0 || A_EventInfo==2) 
    {
        GuiControlGet, p, POS, SB
        GuiControl, Move, LV, % "w" A_GuiWidth " h" (A_GuiHeight-pH)
    }
    else If (A_EventInfo==1) 
    {
        Menu, Tray, Icon
        Gui, Hide
    }
Return

SB_Show:
    If (monitor) 
    {
        SB_SetIcon(A_Windir . "\system32\shell32.dll", 138, 1)
        SB_SetText("monitoring...")
        SetTimer, RefleshView, %refresh_dur%
    } 
    else 
    {
        SB_SetIcon(A_Windir . "\system32\shell32.dll", 4*28-2, 1)
        SB_SetText("Idle")
        SetTimer, RefleshView, Off
    }
Return

KillProcess:
    Thread, NoTimers, true
    SetTimer, RefleshView, Off
    Gui, +OwnDialogs
    MsgBox, 305, Alert, Are you sure?, 500
    IfMsgBox, OK
    {
        Process, Close, %pid%
        Sleep, 500
        Gosub, RefleshView
        SetTimer, RefleshView, %refresh_dur%
    }
Return


ShowMainWindow:
    WinShow, ahk_id %hwnd%
Return

BypassMenu:
    Thread, NoTimers, true
    SetTimer, RefleshView, Off
    WinMenuSelectItem, ahk_id %hwnd%,, File, %A_ThisMenuItem%
    Sleep, 500
    Gosub, RefleshView
    SetTimer, RefleshView, %refresh_dur%
Return

MenuCancel:
    ; dummy
Return

LvEvent:
    Thread, NoTimers, true
    If (A_GuiEvent="RightClick" && row:=LV_GetNext(0, "F")) 
    {
        LV_GetText(path, row, col_file)
        LV_GetText(exe , row, col_exe )
        LV_GetText(ver , row, col_ver )
        LV_GetText(pid , row, col_pid )
        LV_GetText(dir , row, col_dir )
        LV_GetText(mem , row, col_mem )
        LV_GetText(hwnd, row, col_hwnd)
        Menu, M1, Show
    }
Return

SbEvent:
    If (A_EventInfo==1 && A_GuiEvent="DoubleClick") {
        monitor:=!monitor
        Gosub, SB_Show
    }
Return

GetMem(pid) {
    Global use_comma
    mem := GetProcessMemoryState(pid)
    Return use_comma ? comma(mem) : mem
}

Comma(value) {
    return RegExReplace(value, "(\d{1,3})(?=(?:\d\d\d)+(?!\d))", "$1,", "", -1)
}

GetProcessMemoryState(pid,type=8){
    pMemSt:=SAlloc(40)
    hProcess:=DllCall("OpenProcess"
        , "UInt", 0x001F0FFF
        , "UInt", 0
        , "UInt", pid
        , "UInt")
    If (hProcess) {
        If DllCall("psapi.dll\GetProcessMemoryInfo", "UInt",hProcess, "UInt",pMemst, "UInt",40) {
            val:=SGetInt(pMemSt,type*4)
        }
        releaseProcessHandle(hProcess)
    }
    SFree(pMemSt)
    Return val
}

SAlloc(size){
    Return DllCall("GlobalAlloc","UInt",0x40,"UInt",size,"UInt")
}
SFree(ptr){
    DllCall("GlobalFree","UInt",ptr)
}

SGetInt(pStruct,offset){
    DllCall("RtlMoveMemory", "UIntP",val, "UInt",pStruct+offset, "Int",4)
    Return val
}
releaseProcessHandle(hProcess){
    DllCall("psapi.dll\CloseProcess","Int",hProcess)
}

superuser7777 avatar Oct 26 '21 03:10 superuser7777