Skip to content

Slion/VirtualDesktop

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

VirtualDesktop

.NET library for Windows multiple desktop operations

Windows 11 Windows 10

Build Publish License

Platform NuGet Downloads
Core NuGet Badge NuGet Downloads
Forms NuGet Badge NuGet Downloads
WPF NuGet Badge NuGet Downloads

Features

  • Switch, add, and remove a virtual desktop
  • Move any window to any virtual desktop
  • Pin any window or application for them to show on all desktops
  • Notifications when switching, deleting, or renaming virtual desktops
  • Change the wallpaper for each desktop

Sample app

samples/VirtualDesktop.Showcase

Requirements

<TargetFramework>net6.0-windows10.0.19041.0</TargetFramework>
  • .NET 6.0
  • .NET 7.0
  • .NET 8.0
  • Windows 10 build 19041 (20H1) or later

Installation

Install NuGet package(s).

PM> Install-Package VirtualDesktop

How to use

Preparation

Because of the dependency on C#/WinRT, the target framework must be set to net6.0-windows10.0.19041.0 or later.

<TargetFramework>net6.0-windows10.0.19041.0</TargetFramework>

If it does not work, try creating an app.manifest file optimized to work on Windows 10.

<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
    <application>
	    <!-- Windows 10 / 11-->
	    <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
    </application>
</compatibility>

The namespace to use is WindowsDesktop.

using WindowsDesktop;

Get instance of VirtualDesktop class

// Get all virtual desktops
var desktops = VirtualDesktop.GetDesktops();

// Get Virtual Desktop for specific window
var desktop = VirtualDesktop.FromHwnd(hwnd);

// Get the left/right desktop
var left  = desktop.GetLeft();
var right = desktop.GetRight();

Manage virtual desktops

// Create new
var desktop = VirtualDesktop.Create();

// Remove
desktop.Remove();

// Switch
desktop.GetLeft().Switch();

Subscribe virtual desktop events

// Notification of desktop switching
VirtualDesktop.CurrentChanged += (_, args) => Console.WriteLine($"Switched: {args.NewDesktop.Name}");

// Notification of desktop creating
VirtualDesktop.Created += (_, desktop) => desktop.Switch();

for WPF window

// Need to install 'VirtualDesktop.WPF' package

// Check whether a window is on the current desktop.
var isCurrent = window.IsCurrentVirtualDesktop();

// Get Virtual Desktop for WPF window
var desktop = window.GetCurrentDesktop();

// Move window to specific Virtual Desktop
window.MoveToDesktop(desktop);

// Pin window
window.Pin()

Windows version support

Since this library is using undocumented interfaces, you need to reverse engineer your Windows version to support it.

The class IDs of undocumented interfaces tend to change between different OS versions. If the demo application crashes on start-up, you will need to provide the interfaces' IDs matching your Windows version.

Here are the interfaces we need:

  • IApplicationView
  • IApplicationViewCollection
  • IObjectArray
  • IServiceProvider
  • IVirtualDesktop
  • IVirtualDesktopManager
  • IVirtualDesktopManagerInternal
  • IVirtualDesktopNotification
  • IVirtualDesktopNotificationService
  • IVirtualDesktopPinnedApps

Once you have those IDs, add them in a new setting element in app.config. Make sure to specify the correct 9 digits Windows build and cumulative update version. You can get it using one of those methods:

  • From the UI run: winver
  • From shell run: ver
  • From powershell run: cmd /c ver

Don't forget to contribute back your changes.

Publish

To publish a new release specify your version in Directory.Build.props and push the changes with a commit description such as: Release vx.y.z where x, y, z form your version number. That should publish it on NuGet providing that your secret NUGET_API_KEY is still valid.

Internals

This library is essentially a C# wrapper for IVirtualDesktopManager and related undocumented interfaces. In order to support breaking binary changes between Windows versions we perform runtime compilation of a DLL providing access to the COM interfaces matching your OS build version.

Resources

License

This library is under the MIT License.

Credits

  • Thanks @Grabacr07 for creating this great piece of software
  • All contributors for sharing your work with the community

About

.NET library for Windows multiple desktop operations

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

 

Packages

No packages published

Languages

  • C# 100.0%