FuPeiJiang / VD.ahk

Windows Virtual Desktop, AutoHotkey, Windows 11 support, Windows Server 2022, switch desktop, move window(wintitle) to current desktop; createDesktop, PinWindow, getCount, getDesktopNumOfWindow -> mute all windows in Virtual Desktop
MIT License
319 stars 45 forks source link

Virtual desktop Tasksbar icons #56

Open anxxx opened 9 months ago

anxxx commented 9 months ago

I'm not sure if this is the right place or if anyone is interested but but I've made a small addition to FuPeiJiang's excellent code to switch the AutoHotkey icon to show the number of the virtual desktop on the taskbar.

Prerequisite is to show the AutoHotkey icon on the taskbar - this can be done in Settings > Personalisation > Taskbar.

You then need an icon that shows the number of each virtual desktop. I use six virtual desktops and have six icons stored in a folder referenced by the code). The icons are named 1.ico, 2.ico etc. Copies are attached
icons.zip.

I am sure there must be an easier way of running the code so would welcome any suggestions but I have added a function to obtain the desktop number:

+++++++++++++++++++ ChangeIcon() ;define function { Desktop := VD.getCurrentDesktopNum() TraySetIcon("D:\Data\AutoHotkey2\icons" Desktop ".ico") } ChangeIcon ; call function +++++++++++++++++++

And have then added the ChangeIcon function to each of the keyboard shortcuts. For example the Ctrl + Win +Numpad shortcut

++++++++++++++++++++ ;#useful stuff

^#numpad1:: { VD.goToDesktopNum(1) ChangeIcon ; function added } ^#numpad2:: { VD.goToDesktopNum(2) ChangeIcon ; function added }

++++++++++++++++++ etc. etc.

I'm sure it must be possible to trigger the ChangeIcon function with every desktop change without the necessity for adding the code to each of the keyboard shortcuts but my AutoHotkey skills are rather limited.

vlachig commented 9 months ago

it must be possible to trigger the ChangeIcon function with every desktop change without the necessity for adding the code to each of the keyboard shortcut.

You don't need the ChangeIcon function, if you add ++++++++++++++++++++ Menu,TRAY, Icon, Path of the IconFile%desktopNum%.ico ++++++++++++++++++++ at the end of the function goToDesktopNum(desktopNum) and your icon files are numbered.

anxxx commented 9 months ago

You don't need the ChangeIcon function, if you add ++++++++++++++++++++ Menu,TRAY, Icon, Path of the IconFile%desktopNum%.ico ++++++++++++++++++++ at the end of the function goToDesktopNum(desktopNum) and your icon files are numbered.

Thanks vlachig

That sounds good but I can't get it to work. Could you possibly give me the exact line of code?

I had already tried ++++++++++++++++++++++ ^#numpad1::VD.goToDesktopNum(1), TraySetIcon("D:\Data\AutoHotkey2\icons\1.ico") ++++++++++++++++++++++ which worked fine for VD.goToDesktopNum(1) etc.

but didn't work for moving to a relative desktop with the right and left arrow keys e.g.

#!left::VD.goToDesktopNum(VD.MoveWindowToRelativeDesktopNum("A", -1))

I am also not not sure that your suggestion would work for that either as as the desktop number is not specifically specified .

Thanks again

vlachig commented 9 months ago

In the file _VD.ahk search for the function goToDesktopNum(desktopNum) and replace it by this one:

    goToDesktopNum(desktopNum) { ; Lej77 https://github.com/Grabacr07/VirtualDesktop/pull/23#issuecomment-334918711
        firstWindowId:=this._getFirstWindowInVD(desktopNum)
        Gui VD_animation_gui:New, % "-Border -SysMenu +Owner -Caption +HwndVD_animation_gui_hwnd_tmp"
        VD_animation_gui_hwnd:=VD_animation_gui_hwnd_tmp+0
        IVirtualDesktop := this._GetDesktops_Obj().GetAt(desktopNum)
        GetId:=this._vtable(IVirtualDesktop, 4)
        VarSetCapacity(GUID_Desktop, 16)
        DllCall(GetId, "Ptr", IVirtualDesktop, "Ptr", &GUID_Desktop)
        DllCall(this.MoveWindowToDesktop, "Ptr", this.IVirtualDesktopManager, "Ptr", VD_animation_gui_hwnd, "Ptr", &GUID_Desktop)
        DllCall("ShowWindow","Ptr",VD_animation_gui_hwnd,"Int",4) ;after gui on current desktop owned by current process became active window, Show gui on different desktop owned by current process
        this._WinActivate_NewProcess(VD_animation_gui_hwnd)
        loop 20 {
            if (this.getCurrentDesktopNum()==desktopNum) { ; wildest hack ever..
                if (firstWindowId) {
                    DllCall("SetForegroundWindow","Ptr",firstWindowId)
                } else {
                    this._activateDesktopBackground()
                }
                break
            }
            Sleep 25
        }
        Gui VD_animation_gui:Destroy
    Menu,TRAY, Icon, D:\Data\AutoHotkey2\icons\%desktopNum%.ico
    }
vlachig commented 9 months ago

Another solution:

 ; === auto-execute section ===  (top of the script)

; ...
SetTimer Change_TRAY_Icon, 500
; ...

       RETURN   ; === end of auto-execute section ===

; ...

    Change_TRAY_Icon:
Nr := VD.getCurrentDesktopNum()
If (CurrentDesktopNr != Nr)
{   
    Menu,TRAY, Icon, D:\Data\AutoHotkey2\icons\%Nr%.ico
    CurrentDesktopNr := Nr
}
return