0

I have an issue with HWiNFO where the widgets do not show on all desktop even if I right click and select the option to show apps from the window on all desktops: https://www.hwinfo.com/forum/threads/widgets-not-showing-on-all-screens-on-windows-10.7833/#post-33664

Yes, I am using the method guided here: How to show a window on all virtual desktops

The only way to make the widgets show is to right click each one and then select "Show this window on all desktops." However, this resets on each restart and I have to do this again on every restart on all my dozen widgets.

Is it possible to perform a command-line action to do this in batch to the widgets? I would like to build an AHK script or something to be able to do this.

Med
  • 389

1 Answers1

1

Another AHK solution:

#Persistent
VD.init()

SetTimer, Show_HWiNFO, 100 Return

Show_HWiNFO: VD.PinWindow("ahk_exe HWiNFO64.EXE") return

; VD.ahk ; https://github.com/FuPeiJiang/VD.ahk ; https://www.autohotkey.com/boards/viewtopic.php?f=6&t=83381&p=442769#p442769

class VD { init() { splitByDot:=StrSplit(A_OSVersion, ".") buildNumber:=splitByDot[3] if (buildNumber < 22000) { ; Windows 10 IID_IVirtualDesktopManagerInternal_:="{F31574D6-B682-4CDC-BD56-1827860ABEC6}" ;https://github.com/MScholtes/VirtualDesktop/blob/812c321e286b82a10f8050755c94d21c4b69812f/VirtualDesktop.cs#L177-L191 IID_IVirtualDesktop_:="{FF72FFDD-BE7E-43FC-9C03-AD81681E88E4}" ;https://github.com/MScholtes/VirtualDesktop/blob/812c321e286b82a10f8050755c94d21c4b69812f/VirtualDesktop.cs#L149-L150 ;conditionally assign method to method this.dll_GetCurrentDesktop:=this._dll_GetCurrentDesktop_Win10 this._dll_GetDesktops:=this._dll_GetDesktops_Win10 this._dll_SwitchDesktop:=this._dll_SwitchDesktop_Win10 this._dll_CreateDesktop:=this._dll_CreateDesktop_Win10 this._dll_GetName:=this._dll_GetName_Win10 } else { ; Windows 11 IID_IVirtualDesktopManagerInternal:="{B2F925B9-5A0F-4D2E-9F4D-2B1507593C10}" ;https://github.com/MScholtes/VirtualDesktop/blob/812c321e286b82a10f8050755c94d21c4b69812f/VirtualDesktop11.cs#L163-L185 IID_IVirtualDesktop_:="{536D3495-B208-4CC9-AE26-DE8111275BF8}" ;https://github.com/MScholtes/VirtualDesktop/blob/812c321e286b82a10f8050755c94d21c4b69812f/VirtualDesktop11.cs#L149-L150 ;conditionally assign method to method this._dll_GetCurrentDesktop:=this._dll_GetCurrentDesktop_Win11 this._dll_GetDesktops:=this._dll_GetDesktops_Win11 this._dll_SwitchDesktop:=this._dll_SwitchDesktop_Win11 this._dll_GetName:=this._dll_GetName_Win11 } ;---------------------- this.IVirtualDesktopManager := ComObjCreate("{AA509086-5CA9-4C25-8F95-589D3C07B48A}", "{A5CD92FF-29BE-454C-8D04-D82879FB3F1B}") this.GetWindowDesktopId := this._vtable(this.IVirtualDesktopManager, 4)

    IServiceProvider := ComObjCreate(&quot;{C2F03A33-21F5-47FA-B4BB-156362A2F239}&quot;, &quot;{6D5140C1-7436-11CE-8034-00AA006009FA}&quot;)

    this.IVirtualDesktopManagerInternal := ComObjQuery(IServiceProvider, &quot;{C5E0CDCA-7B6E-41B2-9FC4-D93975CC467B}&quot;, IID_IVirtualDesktopManagerInternal_)

    ; this.GetCount := this._vtable(this.IVirtualDesktopManagerInternal, 3 ; int GetCount();
    this.MoveViewToDesktop := this._vtable(this.IVirtualDesktopManagerInternal, 4) ; void MoveViewToDesktop(object pView, IVirtualDesktop desktop);
    this.CanViewMoveDesktops := this._vtable(this.IVirtualDesktopManagerInternal, 5) ; bool CanViewMoveDesktops(IApplicationView view);
    this.GetCurrentDesktop := this._vtable(this.IVirtualDesktopManagerInternal, 6) ; IVirtualDesktop GetCurrentDesktop(); || IVirtualDesktop GetCurrentDesktop(IntPtr hWndOrMon);
    if (buildNumber &lt; 22000) {
        ;Windows 10 https://github.com/MScholtes/VirtualDesktop/blob/812c321e286b82a10f8050755c94d21c4b69812f/VirtualDesktop.cs#L177-L191

        this.GetDesktops := this._vtable(this.IVirtualDesktopManagerInternal, 7) ; void GetDesktops(out IObjectArray desktops);
        ; this.GetAdjacentDesktop := this._vtable(this.IVirtualDesktopManagerInternal, 8) ; int GetAdjacentDesktop(IVirtualDesktop from, int direction, out IVirtualDesktop desktop);
        this.SwitchDesktop := this._vtable(this.IVirtualDesktopManagerInternal, 9) ; void SwitchDesktop(IVirtualDesktop desktop);
        this.Ptr_CreateDesktop := this._vtable(this.IVirtualDesktopManagerInternal, 10) ; IVirtualDesktop CreateDesktop();
        this.Ptr_RemoveDesktop := this._vtable(this.IVirtualDesktopManagerInternal, 11) ; void RemoveDesktop(IVirtualDesktop desktop, IVirtualDesktop fallback);
        this.FindDesktop := this._vtable(this.IVirtualDesktopManagerInternal, 12) ; IVirtualDesktop FindDesktop(ref Guid desktopid);
    } else if (buildNumber &lt; 22489) {
        ;Windows 11 https://github.com/MScholtes/VirtualDesktop/blob/812c321e286b82a10f8050755c94d21c4b69812f/VirtualDesktop11.cs#L163-L185

        this.GetDesktops := this._vtable(this.IVirtualDesktopManagerInternal, 7) ; void GetDesktops(IntPtr hWndOrMon, out IObjectArray desktops);
        ; this.GetAdjacentDesktop := this._vtable(this.IVirtualDesktopManagerInternal, 8) ; int GetAdjacentDesktop(IVirtualDesktop from, int direction, out IVirtualDesktop desktop);
        this.SwitchDesktop := this._vtable(this.IVirtualDesktopManagerInternal, 9) ; void SwitchDesktop(IntPtr hWndOrMon, IVirtualDesktop desktop);
        this.Ptr_CreateDesktop := this._vtable(this.IVirtualDesktopManagerInternal, 10) ; IVirtualDesktop CreateDesktop(IntPtr hWndOrMon);
        ; this.MoveDesktop := this._vtable(this.IVirtualDesktopManagerInternal, 11) ; void MoveDesktop(IVirtualDesktop desktop, IntPtr hWndOrMon, int nIndex);
        this.Ptr_RemoveDesktop := this._vtable(this.IVirtualDesktopManagerInternal, 12) ; void RemoveDesktop(IVirtualDesktop desktop, IVirtualDesktop fallback);
        this.FindDesktop := this._vtable(this.IVirtualDesktopManagerInternal, 13) ; IVirtualDesktop FindDesktop(ref Guid desktopid);
    } else {
        ;Windows 11 Insider build 22489 https://github.com/MScholtes/VirtualDesktop/blob/9f3872e1275408a0802bdbe46df499bb7645dc87/VirtualDesktop11Insider.cs#L163-L186

        ; this.GetAllCurrentDesktops := this._vtable(this.IVirtualDesktopManagerInternal, 7) ; IObjectArray GetAllCurrentDesktops();
        this.GetDesktops := this._vtable(this.IVirtualDesktopManagerInternal, 8) ; void GetDesktops(IntPtr hWndOrMon, out IObjectArray desktops);
        ; this.GetAdjacentDesktop := this._vtable(this.IVirtualDesktopManagerInternal, 9) ; int GetAdjacentDesktop(IVirtualDesktop from, int direction, out IVirtualDesktop desktop);
        this.SwitchDesktop := this._vtable(this.IVirtualDesktopManagerInternal, 10) ; void SwitchDesktop(IntPtr hWndOrMon, IVirtualDesktop desktop);
        this.Ptr_CreateDesktop := this._vtable(this.IVirtualDesktopManagerInternal, 11) ; IVirtualDesktop CreateDesktop(IntPtr hWndOrMon);
        ; this.MoveDesktop := this._vtable(this.IVirtualDesktopManagerInternal, 12) ; void MoveDesktop(IVirtualDesktop desktop, IntPtr hWndOrMon, int nIndex);
        this.Ptr_RemoveDesktop := this._vtable(this.IVirtualDesktopManagerInternal, 13) ; void RemoveDesktop(IVirtualDesktop desktop, IVirtualDesktop fallback);
        this.FindDesktop := this._vtable(this.IVirtualDesktopManagerInternal, 14) ; IVirtualDesktop FindDesktop(ref Guid desktopid);
    }

    ;https://github.com/MScholtes/VirtualDesktop/blob/812c321e286b82a10f8050755c94d21c4b69812f/VirtualDesktop.cs#L225-L234
    this.IVirtualDesktopPinnedApps := ComObjQuery(IServiceProvider, &quot;{B5A399E7-1C87-46B8-88E9-FC5747B171BD}&quot;, &quot;{4CE81583-1E4C-4632-A621-07A53543148F}&quot;)

    ; this.IsAppIdPinned := this._vtable(this.IVirtualDesktopPinnedApps, 3) ; bool IsAppIdPinned(string appId);
    ; this.PinAppID := this._vtable(this.IVirtualDesktopPinnedApps, 4) ; void PinAppID(string appId);
    ; this.UnpinAppID := this._vtable(this.IVirtualDesktopPinnedApps, 5) ; void UnpinAppID(string appId);
    this.IsViewPinned := this._vtable(this.IVirtualDesktopPinnedApps, 6) ; bool IsViewPinned(IApplicationView applicationView);
    this.PinView := this._vtable(this.IVirtualDesktopPinnedApps, 7) ; void PinView(IApplicationView applicationView);
    this.UnpinView := this._vtable(this.IVirtualDesktopPinnedApps, 8) ; void UnpinView(IApplicationView applicationView);


    ImmersiveShell := ComObjCreate(&quot;{C2F03A33-21F5-47FA-B4BB-156362A2F239}&quot;, &quot;{00000000-0000-0000-C000-000000000046}&quot;)
    ; if !(IApplicationViewCollection := ComObjQuery(ImmersiveShell,&quot;{1841C6D7-4F9D-42C0-AF41-8747538F10E5}&quot; ) ) ; doesn't work
    ; SAME CLSID and IID ?
    ; wait it's not CLSID:
    ; SID
    ; A service identifier in the same form as IID. When omitting this parameter, also omit the comma.
    this.IApplicationViewCollection := ComObjQuery(ImmersiveShell,&quot;{1841C6D7-4F9D-42C0-AF41-8747538F10E5}&quot;,&quot;{1841C6D7-4F9D-42C0-AF41-8747538F10E5}&quot; )
    if (!this.IApplicationViewCollection) ; 1607-1809
    {
        MsgBox IApplicationViewCollection interface not supported.
    }
    this.GetViewForHwnd := this._vtable(this.IApplicationViewCollection, 6) ; (IntPtr hwnd, out IApplicationView view);


    ;----------------------

    ; VarSetCapacity(IID_IVirtualDesktop, 16)
    ; this will never be garbage collected
    this.Ptr_IID_IVirtualDesktop := DllCall( &quot;GlobalAlloc&quot;, &quot;UInt&quot;,0x40, &quot;UInt&quot;, 16, &quot;Ptr&quot;)
    DllCall(&quot;Ole32.dll\CLSIDFromString&quot;, &quot;Str&quot;, IID_IVirtualDesktop_, &quot;Ptr&quot;, this.Ptr_IID_IVirtualDesktop)

    ;----------------------

}
;dll methods start
_dll_GetCurrentDesktop_Win10() {
    IVirtualDesktop_ofCurrentDesktop := 0
    DllCall(this.GetCurrentDesktop, &quot;UPtr&quot;, this.IVirtualDesktopManagerInternal, &quot;Ptr*&quot;, IVirtualDesktop_ofCurrentDesktop)
    return IVirtualDesktop_ofCurrentDesktop
}
_dll_GetCurrentDesktop_Win11() {
    IVirtualDesktop_ofCurrentDesktop := 0
    DllCall(this.GetCurrentDesktop, &quot;UPtr&quot;, this.IVirtualDesktopManagerInternal, &quot;Ptr&quot;, 0, &quot;Ptr*&quot;, IVirtualDesktop_ofCurrentDesktop)
    return IVirtualDesktop_ofCurrentDesktop
}
_dll_GetDesktops_Win10() {
    IObjectArray := 0
    DllCall(this.GetDesktops, &quot;UPtr&quot;, this.IVirtualDesktopManagerInternal, &quot;Ptr*&quot;, IObjectArray)
    return IObjectArray
}
_dll_GetDesktops_Win11() {
    IObjectArray := 0
    DllCall(this.GetDesktops, &quot;UPtr&quot;, this.IVirtualDesktopManagerInternal, &quot;Ptr&quot;, 0, &quot;UPtr*&quot;, IObjectArray)
    return IObjectArray
}
_dll_SwitchDesktop_Win10(IVirtualDesktop) {
    DllCall(this.SwitchDesktop, &quot;ptr&quot;, this.IVirtualDesktopManagerInternal, &quot;UPtr&quot;, IVirtualDesktop)
}
_dll_SwitchDesktop_Win11(IVirtualDesktop) {
    DllCall(this.SwitchDesktop, &quot;ptr&quot;, this.IVirtualDesktopManagerInternal, &quot;Ptr&quot;, 0, &quot;UPtr&quot;, IVirtualDesktop)
}
_dll_CreateDesktop_Win10() {
    IVirtualDesktop_ofNewDesktop:=0
    DllCall(this.Ptr_CreateDesktop, &quot;UPtr&quot;, this.IVirtualDesktopManagerInternal, &quot;Ptr*&quot;, IVirtualDesktop_ofNewDesktop)
    return IVirtualDesktop_ofNewDesktop
}
_dll_CreateDesktop_Win11() {
    IVirtualDesktop_ofNewDesktop:=0
    DllCall(this.Ptr_CreateDesktop, &quot;UPtr&quot;, this.IVirtualDesktopManagerInternal, &quot;Ptr&quot;, 0, &quot;Ptr*&quot;, IVirtualDesktop_ofNewDesktop)
    return IVirtualDesktop_ofNewDesktop
}
_dll_GetName_Win10(IVirtualDesktop) {
    QueryInterface:=this._vtable(IVirtualDesktop, 0)
    VarSetCapacity(CLSID, 16)
    DllCall(&quot;Ole32.dll\CLSIDFromString&quot;, &quot;Str&quot;,&quot;{31EBDE3F-6EC3-4CBD-B9FB-0EF6D09B41F4}&quot;, &quot;Ptr&quot;,&amp;CLSID)
    DllCall(QueryInterface, &quot;UPtr&quot;,IVirtualDesktop, &quot;Ptr&quot;,&amp;CLSID, &quot;Ptr*&quot;, IVirtualDesktop2)

    GetName:=this._vtable(IVirtualDesktop2,5)
    DllCall(GetName, &quot;UPtr&quot;, IVirtualDesktop2, &quot;Ptr*&quot;, Handle_DesktopName)
    if (Handle_DesktopName==0) {
        return &quot;&quot; ;you can't have empty desktopName so this can represent error
    }
    Ptr_DesktopName:=DllCall(&quot;combase\WindowsGetStringRawBuffer&quot;, &quot;Ptr&quot;,Handle_DesktopName, &quot;UInt*&quot;,length, &quot;Ptr&quot;)
    desktopName:=StrGet(Ptr_DesktopName+0,&quot;UTF-16&quot;)
    return desktopName
}
/* _dll_GetName_Win10(IVirtualDesktop) {
    GetId := this._vtable(IVirtualDesktop, 4)
    VarSetCapacity(GUID_Desktop, 16)
    DllCall(GetId, &quot;UPtr&quot;,IVirtualDesktop, &quot;Ptr&quot;,&amp;GUID_Desktop)

    strGUID:=this._string_from_GUID(GUID_Desktop)
    KeyName:=&quot;HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VirtualDesktops\Desktops\&quot; strGUID
    ;RegRead gives &quot;&quot; on key not found, convenient
    RegRead, desktopName, % KeyName, % &quot;Name&quot;
    ; I don't like this because registry can be edited, then it wouldn't reflect the desktopName
    return desktopName
}
*/
_dll_GetName_Win11(IVirtualDesktop) {
    GetName:=this._vtable(IVirtualDesktop,6)
    DllCall(GetName, &quot;UPtr&quot;, IVirtualDesktop, &quot;Ptr*&quot;, Handle_DesktopName)
    if (Handle_DesktopName==0) {
        return &quot;&quot; ;you can't have empty desktopName so this can represent error
    }
    Ptr_DesktopName:=DllCall(&quot;combase\WindowsGetStringRawBuffer&quot;, &quot;Ptr&quot;,Handle_DesktopName, &quot;UInt*&quot;,length, &quot;Ptr&quot;)
    desktopName:=StrGet(Ptr_DesktopName+0,&quot;UTF-16&quot;)
    return desktopName
}
;dll methods end

;actual methods start
getCount() { ;how many virtual desktops you now have
    return this._GetDesktops_Obj().GetCount()
}

goToDesktopNum(desktopNum) {
    IVirtualDesktop:=this._GetDesktops_Obj().GetAt(desktopNum)
    this._SwitchIVirtualDesktop(IVirtualDesktop)

    if (this._isWindowFullScreen(&quot;A&quot;)) {
        timerFunc := ObjBindMethod(this, &quot;_pleaseSwitchDesktop&quot;, desktopNum) ;https://www.autohotkey.com/docs/commands/SetTimer.htm#ExampleClass
        SetTimer % timerFunc, -50
    }

}

getNameFromDesktopNum(desktopNum) {
    IVirtualDesktop:=this._GetDesktops_Obj().GetAt(desktopNum)
    _name:=this._dll_GetName(IVirtualDesktop)
    return _name
    ; if (_name==&quot;&quot;) {
        ; return &quot;Desktop &quot; _name
    ; }
}

getDesktopNumOfWindow(wintitle) {
    found:=this._getFirstValidWindow(wintitle)
    if (!found) {
        return -1 ;for false
    }
    theHwnd:=found[1]

    desktopNum_ofWindow:=this._desktopNum_from_Hwnd(theHwnd)
    return desktopNum_ofWindow ; 0 for &quot;Show on all desktops&quot;
}

goToDesktopOfWindow(wintitle, activateYourWindow:=true) {
    found:=this._getFirstValidWindow(wintitle)
    if (!found) {
        return -1 ;for false
    }
    theHwnd:=found[1]

    desktopNum_ofWindow:=this._desktopNum_from_Hwnd(theHwnd)
    this.goToDesktopNum(desktopNum_ofWindow)

    if (activateYourWindow) {
        WinActivate, ahk_id %theHwnd%
    }
}

MoveWindowToDesktopNum(wintitle, desktopNum)
{
    found:=this._getFirstValidWindow(wintitle)
    if (!found) {
        return -1 ;for false
    }
    theHwnd:=found[1]
    thePView:=found[2]

    IVirtualDesktop:=this._GetDesktops_Obj().GetAt(desktopNum)

    this._MoveView_to_IVirtualDesktop(thePView, IVirtualDesktop)
}

MoveWindowToRelativeDesktopNum(wintitle, relative_desktopNum)
{
    found:=this._getFirstValidWindow(wintitle)
    if (!found) {
        return -1 ;for false
    }
    theHwnd:=found[1]
    thePView:=found[2]

    desktopNum_ofWindow:=this._desktopNum_from_Hwnd(theHwnd)

    Desktops_Obj:=this._GetDesktops_Obj()
    count_Desktops:=Desktops_Obj.GetCount()

    absolute_desktopNum:=desktopNum_ofWindow + relative_desktopNum
    ;// The 1-based indices wrap around on the first and last desktop.
    ;// say count_Desktops:=3
    absolute_desktopNum:=Mod(absolute_desktopNum, count_Desktops)
    ; 4 -&gt; 1
    if (absolute_desktopNum &lt;= 0) {
        ; 0 -&gt; 3
        absolute_desktopNum:=absolute_desktopNum + count_Desktops
    }

    IVirtualDesktop:=Desktops_Obj.GetAt(absolute_desktopNum)

    this._MoveView_to_IVirtualDesktop(thePView, IVirtualDesktop)
    return absolute_desktopNum
}

MoveWindowToCurrentDesktop(wintitle, activateYourWindow:=true)
{
    found:=this._getFirstValidWindow(wintitle)
    if (!found) {
        return -1 ;for false
    }
    theHwnd:=found[1]
    thePView:=found[2]

    currentDesktopNum:=this.getCurrentDesktopNum()
    IVirtualDesktop:=this._GetDesktops_Obj().GetAt(currentDesktopNum)

    this._MoveView_to_IVirtualDesktop(thePView, IVirtualDesktop)

    if (activateYourWindow) {
        WinActivate % &quot;ahk_id &quot; theHwnd
    }
}

getCurrentDesktopNum() {
    IVirtualDesktop_ofCurrentDesktop:=this._dll_GetCurrentDesktop()

    desktopNum:=this._desktopNum_from_IVirtualDesktop(IVirtualDesktop_ofCurrentDesktop)
    return desktopNum
}

createDesktop(goThere:=false) {
    IVirtualDesktop_ofNewDesktop:=this._dll_CreateDesktop()

    if (goThere) {
        ;we could assume that it's the rightmost desktop:
        ; desktopNum:=this.getCount()
        ;but I'm not risking it
        desktopNum:=this._desktopNum_from_IVirtualDesktop(IVirtualDesktop_ofNewDesktop)
        this.goToDesktopNum(desktopNum)
    }
}

createUntil(howMany, goToLastlyCreated:=false) {
    howManyThereAlreadyAre:=this.getCount()
    if (howManyThereAlreadyAre&gt;=howMany) {
        return
    }

    ;this will create until one less than wanted
    loop % howMany - howManyThereAlreadyAre - 1 {
        this.createDesktop(false)
    }
    this.createDesktop(goToLastlyCreated)
}

removeDesktop(desktopNum, fallback_desktopNum:=false) {
    ;FALLBACK IS ONLY USED IF YOU ARE CURRENTLY ON THE VD BEING DELETED
    ;but we NEED a fallback, regardless, so I'm not checking if you are currently on the vd being deleted

    Desktops_Obj:=this._GetDesktops_Obj()

    ;if no fallback,
    if (!fallback_desktopNum) {

        ;look left
        if (desktopNum &gt; 1) {
            fallback_desktopNum:=desktopNum - 1
        }
        ;look right
        else if (desktopNum &lt; Desktops_Obj.GetCount()) {
            fallback_desktopNum:=desktopNum + 1
        }
        ;no fallback to go to
        else {
            return false
        }
    }

    IVirtualDesktop:=Desktops_Obj.GetAt(desktopNum)
    IVirtualDesktop_fallback:=Desktops_Obj.GetAt(fallback_desktopNum)

    DllCall(this.Ptr_RemoveDesktop, &quot;UPtr&quot;, this.IVirtualDesktopManagerInternal, &quot;Ptr&quot;, IVirtualDesktop, &quot;Ptr&quot;, IVirtualDesktop_fallback)
}

IsWindowPinned(wintitle) {
    found:=this._getFirstValidWindow(wintitle)
    if (!found) {
        return -1 ;for false
    }
    thePView:=found[2]

    viewIsPinned:=this._IsViewPinned(thePView)
    return viewIsPinned
}
TogglePinWindow(wintitle) {
    found:=this._getFirstValidWindow(wintitle)
    if (!found) {
        return -1 ;for false
    }
    thePView:=found[2]

    viewIsPinned:=this._IsViewPinned(thePView)
    if (viewIsPinned) {
        DllCall(this.UnPinView, &quot;UPtr&quot;, this.IVirtualDesktopPinnedApps, &quot;Ptr&quot;, thePView)
    } else {
        DllCall(this.PinView, &quot;UPtr&quot;, this.IVirtualDesktopPinnedApps, &quot;Ptr&quot;, thePView)
    }

}
PinWindow(wintitle) {
    found:=this._getFirstValidWindow(wintitle)
    if (!found) {
        return -1 ;for false
    }
    thePView:=found[2]

    DllCall(this.PinView, &quot;UPtr&quot;, this.IVirtualDesktopPinnedApps, &quot;Ptr&quot;, thePView)
}
UnPinWindow(wintitle) {
    found:=this._getFirstValidWindow(wintitle)
    if (!found) {
        return -1 ;for false
    }
    thePView:=found[2]

    DllCall(this.UnPinView, &quot;UPtr&quot;, this.IVirtualDesktopPinnedApps, &quot;Ptr&quot;, thePView)
}

;actual methods end

;internal methods start
_MoveView_to_IVirtualDesktop(thePView, IVirtualDesktop) {
    DllCall(this.MoveViewToDesktop, &quot;ptr&quot;, this.IVirtualDesktopManagerInternal, &quot;Ptr&quot;, thePView, &quot;Ptr&quot;, IVirtualDesktop)
    DllCall(this.MoveViewToDesktop, &quot;ptr&quot;, this.IVirtualDesktopManagerInternal, &quot;Ptr&quot;, thePView, &quot;Ptr&quot;, IVirtualDesktop)
    this._activateWindowUnder()
}
_SwitchIVirtualDesktop(IVirtualDesktop) {
    ;activate taskbar before ;https://github.com/mzomparelli/zVirtualDesktop/issues/59#issuecomment-317613971
    WinActivate, ahk_class Shell_TrayWnd
    WinWaitActive, ahk_class Shell_TrayWnd

    this._dll_SwitchDesktop(IVirtualDesktop)
    this._dll_SwitchDesktop(IVirtualDesktop)
    this._activateWindowUnder()
}

_pleaseSwitchDesktop(desktopNum) {
    ;IVirtualDesktop should be calculated again because IVirtualDesktop could have changed
    ;what we want is the same desktopNum
    IVirtualDesktop:=this._GetDesktops_Obj().GetAt(desktopNum)
    this._SwitchIVirtualDesktop(IVirtualDesktop)
    ;this method is goToDesktopNum(), but without the recursion, to prevent recursion
}

_activateWindowUnder() {
    ; if this doesn't work
    ;try https://www.autohotkey.com/boards/viewtopic.php?t=28760#p326541
    found:=this._getFirstWindowUnder()
    if (!found) {
        return -1 ;for false
    }
    theHwnd:=found[1]
    ; MsgBox % theHwnd

    WinGet, OutputVar_MinMax, MinMax, % &quot;ahk_id &quot; theHwnd
    if (!(OutputVar_MinMax==-1)) {
        WinActivate, ahk_id %theHwnd%
    }
}

_getFirstWindowUnder() {
    returnValue:=false
    WinGet, outHwndList, List
    loop % outHwndList {
        if (pView:=this._isValidWindow(outHwndList%A_Index%)) {
            returnValue:=[outHwndList%A_Index%, pView]
            break
        }
    }
    return returnValue
}

_getFirstValidWindow(wintitle) {

    bak_DetectHiddenWindows:=A_DetectHiddenWindows
    bak_TitleMatchMode:=A_TitleMatchMode
    DetectHiddenWindows, on
    SetTitleMatchMode, 2
    WinGet, outHwndList, List, % wintitle

    returnValue:=false
    loop % outHwndList {
        if (pView:=this._isValidWindow(outHwndList%A_Index%)) {
            returnValue:=[outHwndList%A_Index%, pView]
            break
        }
    }

    SetTitleMatchMode % bak_TitleMatchMode
    DetectHiddenWindows % bak_DetectHiddenWindows
    return returnValue
}

_view_from_Hwnd(theHwnd) {
    pView := 0
    DllCall(this.GetViewForHwnd, &quot;UPtr&quot;, this.IApplicationViewCollection, &quot;Ptr&quot;, theHwnd, &quot;Ptr*&quot;, pView)
    return pView
}

_IVirtualDesktop_from_Hwnd(theHwnd) {
    VarSetCapacity(GUID_Desktop, 16)
    HRESULT := DllCall(this.GetWindowDesktopId, &quot;UPtr&quot;, this.IVirtualDesktopManager, &quot;Ptr&quot;, theHwnd, &quot;Ptr&quot;, &amp;GUID_Desktop)
    if (!(HRESULT==0)) {
        return false
    }

    IVirtualDesktop_ofWindow:=0
    DllCall(this.FindDesktop, &quot;UPtr&quot;, this.IVirtualDesktopManagerInternal, &quot;Ptr&quot;, &amp;GUID_Desktop, &quot;Ptr*&quot;, IVirtualDesktop_ofWindow)

    return IVirtualDesktop_ofWindow
}

_desktopNum_from_IVirtualDesktop(IVirtualDesktop) {
    Desktops_Obj:=this._GetDesktops_Obj()
    Loop % Desktops_Obj.GetCount()
    {
        IVirtualDesktop_ofDesktop:=Desktops_Obj.GetAt(A_Index)

        if (IVirtualDesktop_ofDesktop == IVirtualDesktop) {
            return A_Index
        }
    }
    return 0 ;for &quot;Show on all desktops&quot;
}

_desktopNum_from_Hwnd(theHwnd) {
    IVirtualDesktop:=this._IVirtualDesktop_from_Hwnd(theHwnd)
    desktopNum:=this._desktopNum_from_IVirtualDesktop(IVirtualDesktop)
    return desktopNum
}

_GetDesktops_Obj() {
    IObjectArray:=this._dll_GetDesktops()
    return new this.IObjectArray_Wrapper(IObjectArray, this.Ptr_IID_IVirtualDesktop)
}

_IsViewPinned(thePView) {
    viewIsPinned:=0
    DllCall(this.IsViewPinned, &quot;UPtr&quot;, this.IVirtualDesktopPinnedApps, &quot;Ptr&quot;, thePView, &quot;Int*&quot;,viewIsPinned)
    return viewIsPinned
}

;internal methods end

;utility methods start
_isWindowFullScreen( winTitle ) {
    ;checks if the specified window is full screen

    winID := WinExist( winTitle )

    If ( !winID )
        Return false

    WinGet style, Style, ahk_id %WinID%
    WinGetPos ,,,winW,winH, %winTitle%
    ; 0x800000 is WS_BORDER.
    ; 0x20000000 is WS_MINIMIZE.
    ; no border and not minimized
    Return ((style &amp; 0x20800000) or winH &lt; A_ScreenHeight or winW &lt; A_ScreenWidth) ? false : true
}

_isValidWindow(hWnd) ;returns pView if succeeded
{
    ; DetectHiddenWindows, on ;this is needed, but for optimization the caller will do it

    returnValue:=false
    breakToReturnFalse:
    loop 1 {
        ; WinGetTitle, title, ahk_id %hWnd%
        WinGetTitle, title, % &quot;ahk_id &quot; hwnd
        if (!title) {
            break breakToReturnFalse
        }

        WinGet, dwStyle, Style, ahk_id %hWnd%
        if ((dwStyle&amp;0x08000000) || !(dwStyle&amp;0x10000000)) {
            break breakToReturnFalse
        }
        WinGet, dwExStyle, ExStyle, ahk_id %hWnd%
        if (dwExStyle &amp; 0x00000080) {
            break breakToReturnFalse
        }
        WinGetClass, szClass, ahk_id %hWnd%
        if (szClass = &quot;TApplication&quot;) {
            break breakToReturnFalse
        }

        pView:=this._view_from_Hwnd(hWnd)
        if (!pView) {
            break breakToReturnFalse
        }
        pfCanViewMoveDesktops := 0
        DllCall(this.CanViewMoveDesktops, &quot;UPtr&quot;, this.IVirtualDesktopManagerInternal, &quot;Ptr&quot;, pView, &quot;int*&quot;, pfCanViewMoveDesktops) ; return value BOOL
        if (!pfCanViewMoveDesktops) {
            break breakToReturnFalse
        }

        returnValue:=pView
    }
    ; DetectHiddenWindows, off ;this is needed, but for optimization the caller will do it
    return returnValue
}
_IsViewActive(Ptr_View) {
    found:=this._getFirstValidWindow(wintitle)
    if (!found) {
        return false
    }
    thePView:=found[2]

    return thePView == Ptr_View
}
;-------------------
_vtable(ppv, index) {
    Return NumGet(NumGet(0+ppv)+A_PtrSize*index)
}
_string_from_GUID(Byref byref_GUID) {
    VarSetCapacity(strGUID, 38 * 2) ;38 is StrLen(&quot;{FF72FFDD-BE7E-43FC-9C03-AD81681E88E4}&quot;)
    DllCall(&quot;Ole32.dll\StringFromGUID2&quot;, &quot;UPtr&quot;, &amp;byref_GUID, &quot;UPtr&quot;, &amp;strGUID, &quot;Int&quot;, 38 + 1)
    return StrGet(&amp;strGUID, &quot;UTF-16&quot;)
}

class IObjectArray_Wrapper {
    __New(IObjectArray, Ptr_IID_Interface) {
        this.IObjectArray:=IObjectArray
        this.Ptr_IID_Interface:=Ptr_IID_Interface

        this.Ptr_GetAt:=VD._vtable(IObjectArray,4)
    }
    __Delete() {
        ;IUnknown::Release
        Ptr_Release:=VD._vtable(this.IObjectArray,2)
        DllCall(Ptr_Release, &quot;UPtr&quot;, this.IObjectArray)
    }
    GetAt(oneBasedIndex) {
        Ptr_Interface:=0
        DllCall(this.Ptr_GetAt, &quot;UPtr&quot;, this.IObjectArray, &quot;UInt&quot;, oneBasedIndex - 1, &quot;Ptr&quot;, this.Ptr_IID_Interface, &quot;Ptr*&quot;, Ptr_Interface)
        return Ptr_Interface
    }
    GetCount() {
        Ptr_GetCount:=VD._vtable(this.IObjectArray,3)
        Count := 0
        DllCall(Ptr_GetCount, &quot;UPtr&quot;, this.IObjectArray, &quot;UInt*&quot;, Count)
        return Count
    }

}
;utility methods end

}

Relax
  • 3,785