Slion / VirtualDesktop

C# wrapper for the Virtual Desktop API on Windows 11.
MIT License
21 stars 9 forks source link

VirtualDesktop

VirtualDesktop is C# wrapper for IVirtualDesktopManager on Windows 11 (and Windows 10).

Build Publish License

Platform NuGet
Core NuGet Badge
Forms NuGet Badge
WPF NuGet Badge

Features

Sample app

samples/VirtualDesktop.Showcase

Requirements

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

Installation

Install NuGet package(s).

PM> Install-Package VirtualDesktop

How to use

Preparation

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

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

If it doesn't work, try creating an app.manifest file and optimize 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

The class IDs of some of the undocumented interfaces we use tend to change a lot between different versions of Windows. If the demo application crashes on start-up chances are all you need to do is provide the proper IDs for the version of Windows you are running on.

Open regedit and export this path into a file: \HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Interface. Open the resulting reg file and search it for matches against the whole word of each interface name we need:

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

Make sure 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.

Resources

License

This library is under the MIT License.