VirtualDesktop虚拟桌面管理类

使用示例:

切换到第2个虚拟桌面, 注意要从0开始数:

desktop := VirtualDesktop.GetAt(1) ; GetAt(N) 返回第N+1个桌面对象
desktop.Show()

获取桌面总数,当前桌面是第几个:

count := VirtualDesktop.Count
index := VirtualDesktop.Current.Index
MsgBox("当前位于第" index + 1 "个桌面, 总共有" count "个桌面")

通过相对位置获取桌面对象:

rightDesktop := VirtualDesktop.Current.Right ; 获取当前桌面右边的桌面
leftDesktop := rightDesktop.Left ; 获取指定桌面左边的桌面

创建、删除虚拟桌面:

newDesktop := VirtualDesktop.Create() ; 创建桌面
newDesktop.Show()
Sleep(1000)
newDesktop.Remove() ; 删除桌面

判断窗口是否在某个桌面里:

MsgBox VirtualDesktop.Current.HasWindow(WinExist("A")) ; true
MsgBox VirtualDesktop.Current.Right.HasWindow(WinExist("A")) ; false

移动指定窗口到某个桌面,遗憾的是,只能移动本进程的窗口:

myGui := Gui()
myGui.Show("w300 h300")
Sleep(1000)
VirtualDesktop.GetAt(1).ObtainWindow(myGui.Hwnd)

VirtualDesktop Class:

class VirtualDesktop {
    static Current => ((ComCall(6, this.IVirtualDesktopManagerInternal, "ptr*", &currentDesktop := 0)), this(currentDesktop))

    static Count => (ComCall(3, this.IVirtualDesktopManagerInternal, "int*", &count := 0), count)

    static GetAt(index) {
        ComCall(7, this.IVirtualDesktopManagerInternal, "ptr*", desktops := ComValue(13, 0))
        ComCall(4, desktops, "uint", index, "ptr", this.IID_IVirtualDesktop, "ptr*", &desktop := 0)
        return VirtualDesktop(desktop)
    }

    static Create() => (ComCall(10, this.IVirtualDesktopManagerInternal, "ptr*", &newDesktop := 0), VirtualDesktop(newDesktop))

    Id => (ComCall(4, this, "ptr", id := Buffer(16)), id.ToString := (_) => (DllCall('ole32\StringFromGUID2', "ptr", _, "ptr", buf := Buffer(78), "int", 39), StrGet(buf)), id)

    Left => (ComCall(8, VirtualDesktop.IVirtualDesktopManagerInternal, "ptr", this, "uint", 3, "ptr*", &leftDesktop := 0), VirtualDesktop(leftDesktop))

    Right => (ComCall(8, VirtualDesktop.IVirtualDesktopManagerInternal, "ptr", this, "uint", 4, "ptr*", &rightDesktop := 0), VirtualDesktop(rightDesktop))

    Visible => VirtualDesktop.Current.Equals(this)

    Index {
        get {
            thisId := this.Id, thisIdH := NumGet(thisId, "int64"), thisIdL := NumGet(thisId, 8, "int64")
            loop VirtualDesktop.Count {
                id := VirtualDesktop.GetAt(A_Index - 1).Id
                if NumGet(id, "int64") == thisIdH && NumGet(id, 8, "int64") == thisIdL
                    return A_Index - 1
            }
        }
    }

    Show() => ComCall(9, VirtualDesktop.IVirtualDesktopManagerInternal, "ptr", this)

    Remove(fallbackDesktop?) => ComCall(11, VirtualDesktop.IVirtualDesktopManagerInternal, "ptr", this, "ptr", fallbackDesktop ?? VirtualDesktop.GetAt(0))

    HasWindow(hwnd) {
        ComCall(4, VirtualDesktop.IVirtualDesktopManager, "ptr", hwnd, "ptr", id1 := Buffer(16))
        return NumGet(id1, "int64") == NumGet(id2 := this.Id, "int64") && NumGet(id1, 8, "int64") == NumGet(id2, 8, "int64")
    }

    ObtainWindow(hwnd) => ComCall(5, VirtualDesktop.IVirtualDesktopManager, "ptr", hwnd, "ptr", this.Id)

    Equals(desktop) => NumGet(id1 := this.Id, "int64") == NumGet(id2 := desktop.Id, "int64") && NumGet(id1, 8, "int64") == NumGet(id2, 8, "int64")

    static __New() {
        iServiceProvider := ComObject("{C2F03A33-21F5-47FA-B4BB-156362A2F239}", "{6D5140C1-7436-11CE-8034-00AA006009FA}")
        this.IVirtualDesktopManagerInternal := ComObjQuery(iServiceProvider, "{C5E0CDCA-7B6E-41B2-9FC4-D93975CC467B}", "{F31574D6-B682-4CDC-BD56-1827860ABEC6}")
        this.IVirtualDesktopManager := ComObject("{AA509086-5CA9-4C25-8F95-589D3C07B48A}", "{A5CD92FF-29BE-454C-8D04-D82879FB3F1B}")
        NumPut("int64", 0x43fcbe7eff72ffdd, "int64", 0xe4881e6881ad039c, iid := Buffer(16))
        this.IID_IVirtualDesktop := iid
    }
    __New(ptr) {
        if !this.Ptr := ptr
            throw Error("Invalid pointer")
    }
    __Delete() => ObjRelease(this.Ptr)
}
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA捐赠
共{{data.count}}人
人已捐赠
其他函数

取时间戳函数[毫秒级]

2022-12-11 22:14:43

其他

AutoHotkey右键菜单汉化/一键更新最新版本

2022-12-12 11:12:13

6 条回复 A文章作者 M管理员
  1. dbgba
    dbgba给您捐赠了¥5
  2. ahk小明

    大佬,能否出一个V1版本的

    • Tebayaki

      v1写起来太麻烦了,不想搞

  3. ‭W16687

    太强了

个人中心
购物车
优惠劵
有新私信 私信列表
搜索