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

prevent Windows from switching virtual desktop #43

Open mmaukii opened 1 year ago

mmaukii commented 1 year ago

thanks for your code again! When i am working with virtual desktop i really hate it, when i swith to an other program, which is opened on a allready existing desktop. Windows automatically changes the desktop to the called program and gives no notifaication about that. I prefer that an allready opened programm gets changed to the orignal one. With your code i found a hacky solution for that:

i add the last two lines in the function goToDesktopNum to write the with ahk choosen programm to an ini file.

goToDesktopNum(desktopNum) { ; Lej77 https://github.com/Grabacr07/VirtualDesktop/pull/23#issuecomment-334918711

    Gui VD_active_gui:New, % "-Border -SysMenu +Owner -Caption +HwndVD_active_gui_hwnd"
    DllCall("ShowWindow","Ptr",VD_active_gui_hwnd,"Int",1) ;you can only Show gui that's in another VD if a gui of same owned/process is already active

    this._WinActivateForceForceForce(VD_active_gui_hwnd) ;specifically for Teams.exe

    firstWindowId:=this._getFirstWindowInVD(desktopNum)

    Gui VD_animation_gui:New, % "-Border -SysMenu +Owner -Caption +HwndVD_animation_gui_hwnd"
    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",1) ;after gui on current desktop owned by current process became active window, Show gui on different desktop owned by current process
    ; loop 20 {
    ;     if (this.getCurrentDesktopNum()==desktopNum) { ; wildest hack ever..
    ;         if (firstWindowId) {
    ;             DllCall("SetForegroundWindow","Ptr",firstWindowId)
    ;         } else {
    ;             this._activateDesktopBackground()
    ;         }
    ;         Gui VD_animation_gui:Destroy
    ;         Gui VD_active_gui:Destroy
    ;         break
    ;     }
    ;     Sleep 25
    ; }
    if (firstWindowId) {
        DllCall("SetForegroundWindow","Ptr",firstWindowId)
    } else {
        this._activateDesktopBackground()
    }
    Gui VD_animation_gui:Destroy
    Gui VD_active_gui:Destroy
    choosenDesktop :=  desktopNum
    IniWrite, %choosenDesktop%, var.ini, variables, var1
}

then i have the following adapted script to check if the choosen and the actual desktop correspond. If not take the active Window an go back to the choosen desktop. Only drawback is, that you now are only able to change desktops with your ahk-code, what is for me ok. On other drawback is, that the following code has to run in an own instance. At least i did not find to run it together with the rest of the code in one script.

NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.

SingleInstance force

ListLines Off SendMode Input ; Recommended for new scripts due to its superior speed and reliability. SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory. SetBatchLines -1

KeyHistory 0

;verhindert den automatischen Wechsel auf anderen desktop wenn Programm nur dort liegt

Persistent

Include VD.ahk

VD.RegisterDesktopNotifications() VD.CurrentVirtualDesktopChanged:=Func("CurrentVirtualDesktopChanged") CurrentVirtualDesktopChanged(desktopNum_Old, desktopNum_New) { sleep 200 IniRead, choosenDesktop, ../var.ini, variables, var1 ToolTip % desktopNum_Old "-> " desktopNum_New " soll " choosenDesktop SetTimer, RemoveToolTip, -1000 if (desktopNum_New!=choosenDesktop){ ;is desktop selected dekstop sleep 200 VD.MoveWindowToDesktopNum("A",choosenDesktop), VD.goToDesktopNum(choosenDesktop) } } return

RemoveToolTip: ToolTip return

f3::Exitapp

i thought maybe this is of interest for you. Sorry for the programming skills, its hacky, but it is a solution.