ahk整体代码.md 4.5 KB

#ahk

!c::hyf_onekeyWindow("C:\Program Files\Google\Chrome\Application\chrome.exe", "Chrome_WidgetWin_1", "\S") ;注意修改路径
!`::hyf_onekeyWindow("C:\Users\isscuss\AppData\Local\Obsidian\Obsidian.exe", "Chrome_WidgetWin_1", "\S") ;注意修改路径
!s::hyf_onekeyWindow("C:\Program Files\Seafile\bin\seafile-applet.exe", "Qt5151QWindowIcon", "\S") ;注意修改路径
!n::hyf_onekeyWindow("C:\Program Files\Notepad++\notepad++.exe", "Notepad++", "\S") ;注意修改路径
 
hyf_onekeyWindow(exePath, titleClass := "", titleReg := "")
{ ;有些窗口用Ahk_exe exeName判断不准确,所以自定义个titleClass
    SplitPath, exePath, exeName, , , noExt
    If !hyf_processExist(exeName)
    {
        ;hyf_tooltip("启动中,请稍等...")
        Run,% exePath
        ;打开后自动运行 TODO
        funcName := noExt . "_runDo"
        If IsFunc(funcName)
        {
            ;hyf_tooltip("已自动执行函数:" . funcName)
            Func(funcName).Call()
        }
        Else If titleClass
        {
            WinWait, Ahk_class %titleClass%, , 1
            WinActivate Ahk_class %titleClass%
        }
    }
    Else If WinActive("Ahk_exe " . exeName)
    {
        funcName := noExt . "_hideDo"
        If IsFunc(funcName)
            Func(funcName).Call()
        WinHide
        ;激活鼠标所在窗口 TODO
        MouseGetPos, , , idMouse
        WinActivate Ahk_id %idMouse%
    }
    Else
    {
        If titleReg
            titleClass := "Ahk_id " . hyf_getMainIDOfProcess(exeName, titleClass, titleReg)
        Else If titleClass
            titleClass := "Ahk_class " . titleClass
        Else
            titleClass := "Ahk_exe " . exeName
        WinShow %titleClass%
        WinActivate %titleClass%
        funcName := noExt . "_activeDo"
        If IsFunc(funcName)
        {
            ;hyf_tooltip("已自动执行函数:" . funcName)
            Func(funcName).Call()
        }
    }
}
 
hyf_processExist(n) ;判断进程是否存在(返回PID)
{ ;n为进程名
    Process, Exist, %n% ;比IfWinExist可靠
    Return ErrorLevel
}
 
hyf_tooltip(str, t := 1, ExitScript := 0, x := "", y := "")  ;提示t秒并自动消失
{
    t *= 1000
    ToolTip, %str%, %x%, %y%
    SetTimer, hyf_removeToolTip, -%t%
    If ExitScript
    {
        Gui, Destroy
        Exit
    }
}
 
hyf_getMainIDOfProcess(exeName, cls, titleReg := "") ;获取类似chrome等多进程的主程序ID
{
    DetectHiddenWindows, On
    WinGet, arr, List, Ahk_exe %exeName%
    Loop,% arr
    {
        n := arr%A_Index%
        WinGetClass, classLoop, Ahk_id %n%
        ;MsgBox,% A_Index . "/" . arr . "`n" . classLoop . "`n" . cls
        If (classLoop = cls)
        {
            If !StrLen(titleReg) ;不需要判断标题
                Return n
            WinGetTitle, titleLoop, Ahk_id %n%
            ;MsgBox,% A_Index . "/" . arr . "`n" . classLoop . "`n" . titleLoop
            If (titleLoop ~= titleReg)
                Return n
        }
        Continue
    }
    Return False
}
 
hyf_removeToolTip() ;清除ToolTip
{
    ToolTip
}
!w::
WinGet, active_id, PID, A
run, taskkill /PID %active_id% /F,,Hide
return

!u::
Gui, Destroy
	Gui +AlwaysOnTop
	
	counter := 1
	array := []
	has = 0
	
	WinGet, vWinList, List, ahk_class UnityContainerWndClass

	Loop, %vWinList%
	{
		hWnd := vWinList%A_Index%
		has := 1
		array[counter] := hWnd
		WinGetTitle, title, ahk_id %hWnd%
		
		text = %title%
		
		inCount := 1
		
		Loop, Parse, text, `-
		{

			if (inCount == 3)
			{
				Gui Add, Button, gChoice,%counter%.%A_LoopField%
				break
			}
			
			inCount := inCount + 1
		}
		
		counter := counter + 1


	} 

	if (has == 1)
	{
		Gui, Show, AutoSize Center
	}
	else
	{
		hyf_onekeyWindow("C:\Program Files\Unity\Editor\Unity.exe", "WinNativeWebViewWindowClass", "\S")
	}
	
	
Return


!r::
Gui, Destroy
	Gui +AlwaysOnTop
	WinGet, vWinList, List, ahk_class SunAwtFrame
	has = 0
	counter := 1
	array := []
	Loop, %vWinList%
	{
		hWnd := vWinList%A_Index%
		has := 1
		array[counter] := hWnd
		WinGetTitle, title, ahk_id %hWnd%
		
		text = %title%
		
		Loop, Parse, text, `,
		{
			Gui Add, Button, gChoice,%counter%.%A_LoopField%
			break
		}
		counter := counter + 1
	} 

	if (has == 1)
	{
		Gui, Show, AutoSize Center
	}
	else
	{
		hyf_onekeyWindow("C:\Program Files\JetBrains\JetBrains Rider 2020.2.5\bin\rider64.exe", "SunAwtFrame", "\S")
	}

Return

Choice:
	text = %A_GuiControl%
	Loop, Parse, text, `.
	{
		id := % array[A_LoopField]
		WinGetTitle, title, ahk_id %id%
		WinActivate,ahk_id %id%
		break
	}
	Gui, Destroy
Return

!Esc::
Gui, Destroy
Return