首页 > 解决方案 > 如何让 `ifWinActive` 和 `WinActivateBottom` 只使用当前桌面?

问题描述

我有一个autohotkey脚本可以搜索特定窗口是否存在,如果存在,它将激活它。

我希望它只在当前桌面中搜索(我使用的是 Windows 10)。

你有什么建议吗?

我的脚本:

#c::
IfWinExist ,ahk_class ConsoleWindowClass
{
 ifWinActive
 WinActivatebottom ,ahk_class ConsoleWindowClass
 else
  WinActivate
 return
}

标签: autohotkey

解决方案


#c:: WinActivateBottomOnCurrentVirtualDesktop("ConsoleWindowClass")

WinActivateBottomOnCurrentVirtualDesktop(Class){
    IfWinExist, ahk_class %Class%
    {
        list := ""
        LastWin := ""
        ; get a list of those windows on the current desktop
        WinGet, id, list, ahk_class %Class%
        Loop, %id%
        {
            this_ID := id%A_Index%
            If IsWindowOnCurrentVirtualDesktop(this_ID)
                LastWin := this_ID ; retrieves the bottommost matching window ID
        }
        WinActivate, ahk_id %LastWin%
    }
}


; https://autohotkey.com/boards/viewtopic.php?p=64295#p64295
; Indicates whether the provided window is on the currently active virtual desktop:

IsWindowOnCurrentVirtualDesktop(hWnd) {
    onCurrentDesktop := ""
    CLSID := "{aa509086-5ca9-4c25-8f95-589d3c07b48a}" 
    IID := "{a5cd92ff-29be-454c-8d04-d82879fb3f1b}" 
    IVirtualDesktopManager := ComObjCreate(CLSID, IID)  
    Error := DllCall(NumGet(NumGet(IVirtualDesktopManager+0), 3*A_PtrSize), "Ptr", IVirtualDesktopManager, "Ptr", hWnd, "IntP", onCurrentDesktop)
    ObjRelease(IVirtualDesktopManager)  
    if !(Error=0)
        return false, ErrorLevel := true
    return onCurrentDesktop, ErrorLevel := false
}

推荐阅读