Processing Ajax...

Title
Close Dialog

Message

Confirm
Close Dialog

Confirm
Close Dialog

Move Window to Virtual Desktop 2 (Win 10)

Description
Note: The DLLs provided in this function were not compiled by Binary Fortress Software. Use at your own risk.
Windows 1803 use VirtualDesktop-Windows-v1803.dll, rename to VirtualDesktop.dll
Language
C#.net
Minimum Version
Created By
Ben Richards
Contributors
-
Date Created
Jun 6, 2018
Date Last Modified
Jun 6, 2018

Scripted Function (Macro) Code

using System;
using System.Drawing;
using System.Collections.Generic;
using WindowsDesktop;

// Windows < v1803
// Add to reference | Disk:\YourPathToDll\VirtualDesktop.dll from (https://github.com/Grabacr07/VirtualDesktop/tree/master)

// Compiled binaries
// https://www.dropbox.com/s/qgdpr0q3xxqvyvz/VirtualDesktop.dll?dl=0
// Windows < 1803 use VirtualDesktop.dll
// Windows > 1803 use VirtualDesktop-Windows-v1803.dll, rename to VirtualDesktop.dll

public static class DisplayFusionFunction
{
    public static void Run(IntPtr windowHandle)
    {
        var vDesktop = new VirtualDesktops();

        /// Get a dictionary of all desktops
        var desktops = vDesktop.GetAllDesktops();

        /// Get desired desktop and move the applicatio to it
        var deskSecond = desktops["Desktop 2"];
        vDesktop.SwitchToDesktop(deskSecond);
        vDesktop.MoveWindowToDesktop(windowHandle, deskSecond);
        BFS.Window.Focus(windowHandle);
    }
}

class VirtualDesktops
{
    // Create new virtual desktop
    public void Create()
    {
        VirtualDesktop.Create();
    }

    // Remove current virtual desktop
    public void Remove()
    {
        VirtualDesktop.Current.Remove();
    }

    // Switch to the left virtual desktop relative to the current
    public void SwitchToLeft()
    {
        VirtualDesktop.Current.GetLeft().Switch();
    }

    // Switch to the right virtual desktop relative to the current
    public void SwitchToRight()
    {
        VirtualDesktop.Current.GetRight().Switch();
    }

    // Switch to desired virtual desktop by key
    public void SwitchToDesktop(VirtualDesktop vDesktop)
    {
        vDesktop.Switch();
    }

    // Move the window to the left virtual desktop relative to the current
    public void MoveWindowToLeft(IntPtr wHandle)
    {
        var left = VirtualDesktop.Current.GetLeft();
        VirtualDesktopHelper.MoveToDesktop(wHandle, left);
    }


    // Move the window to the right virtual desktop relative to the current
    public void MoveWindowToRight(IntPtr wHandle)
    {
        var right = VirtualDesktop.Current.GetRight();
        VirtualDesktopHelper.MoveToDesktop(wHandle, right);
    }

    // Move the window from either virtual desktop to the current
    public void MoveWindowToCurrent(IntPtr wHandle)
    {
        var currentDesktop = VirtualDesktop.Current;

        if(!VirtualDesktopHelper.IsCurrentVirtualDesktop(wHandle))
            VirtualDesktopHelper.MoveToDesktop(wHandle, currentDesktop);
    }

    // Move the window to specified virtual desktop
    public void MoveWindowToDesktop(IntPtr wHandle, VirtualDesktop virtualDesktop)
    {
        VirtualDesktopHelper.MoveToDesktop(wHandle, virtualDesktop);
    }

    // Pin window. Show this window on all desktops
    public void TogglePin(IntPtr wHandle)
    {
        (VirtualDesktop.IsPinnedWindow(wHandle) ? VirtualDesktop.UnpinWindow :
            (Action<IntPtr>)VirtualDesktop.PinWindow)(wHandle);
    }

    // Pin app. Show windows from this app on all desktops
    public void TogglePinApp(IntPtr wHandle)
    {
        var appId = ApplicationHelper.GetAppId(wHandle);
        (VirtualDesktop.IsPinnedApplication(appId) ? VirtualDesktop.UnpinApplication :
            (Action<string>)VirtualDesktop.PinApplication)(appId);
    }

    // Returns a virtual desktop that window is located
    public VirtualDesktop GetCurrentWindowDesktop(IntPtr wHandle)
    {
        return VirtualDesktop.FromHwnd(wHandle);
    }

    // Return all the virtual desktops of currently valid
    public Dictionary<string, VirtualDesktop> GetAllDesktops()
    {
        var desktopArray = VirtualDesktop.GetDesktops();
        var desktopDictionary = new Dictionary<string, VirtualDesktop>();

        // To simplify selection by key, used (j+1)
        for (var j = 0; j < desktopArray.Length; j++)
            desktopDictionary.Add(string.Format("Desktop {0}", j+1), desktopArray[j]);

        return desktopDictionary;
    }
}