MsgBox % PIDfromAnyID()                                       ; Own PID          
MsgBox % PIDfromAnyID( "winlogon.exe" )                       ; process name
MsgBox % PIDfromAnyID( 4 )                                    ; validating raw PID

MsgBox % PIDfromAnyID( "MyScript.ahk" )                       ; PID of a running script
MsgBox % PIDfromAnyID( "MyScript.ahk ahk_class AutoHotkey" )  ; precise than above
MsgBox % PIDfromAnyID( "Torrent" )                            ; part of window title

MsgBox % PIDfromAnyID( A_ScripthWnd )                         ; Own PID

MouseGetPos,,, OutputVarWin 
MsgBox % PIDfromAnyID( OutputVarWin )                         ; PID of Window under mouse 


; https://www.autohotkey.com/boards/viewtopic.php?f=6&t=3533
PIDfromAnyID( anyID="" ) { ; SKAN, 10-May-2014. http://ahkscript.org/boards/viewtopic.php?p=17974#p17974
  Process, Exist, %anyID%
  IfGreater, ErrorLevel, 0, Return ErrorLevel
  DetectHiddenWindows, % SubStr( ( ADHW := A_DetectHiddenWindows ) . "On", -1 )
  SetTitleMatchMode,   % SubStr( ( ATMM := A_TitleMatchMode )   . "RegEx", -4 ) 
  WinGet, PID, PID, ahk_id %anyID%
  IfEqual, PID,, WinGet, PID, PID, %anyID% 
  DetectHiddenWindows, %ADHW%
  SetTitleMatchMode, %ATMM%
  Return PID ? PID : 0    
}

 

声明:站内资源为整理优化好的代码上传分享与学习研究,如果是开源代码基本都会标明出处,方便大家扩展学习路径。请不要恶意搬运,破坏站长辛苦整理维护的劳动成果。本站为爱好者分享站点,所有内容不作为商业行为。如若本站上传内容侵犯了原著者的合法权益,请联系我们进行删除下架。