Permalink
Browse files

version 2.0 (draft)

- Major changes in management for com objects.
- Add support moving the window of another process.
  • Loading branch information...
1 parent 9d84dd6 commit 634d886dbbabafeeb1da6e6aa82eb73f54e853d7 @Grabacr07 committed Aug 1, 2016
View
@@ -7,8 +7,7 @@ VirtualDesktop is C# wrapper for [IVirtualDesktopManager](https://msdn.microsoft
* Switch, add, and remove a Virtual Desktop.
* Move the window in the same process to any Virtual Desktop.
-* **[CANNOT]** Move the window of another process to any Virtual Desktop.
-(alternate method: [https://github.com/tmyt/VDMHelper](https://github.com/tmyt/VDMHelper))
+* Move the window of another process to any Virtual Desktop (Support in version 2.0 or later).
## Installation
@@ -1,11 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
+using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows;
-using System.Windows.Interop;
using WindowsDesktop;
-using WindowsDesktop.Interop;
namespace VirtualDesktopShowcase
{
@@ -14,6 +13,11 @@ partial class MainWindow
public MainWindow()
{
this.InitializeComponent();
+
+ foreach (var id in VirtualDesktop.GetDesktops().Select(x => x.Id))
+ {
+ System.Diagnostics.Debug.WriteLine(id);
+ }
}
private void CreateNew(object sender, RoutedEventArgs e)
@@ -61,5 +65,9 @@ private void Pin(object sender, RoutedEventArgs e)
{
this.TogglePin();
}
+
+
+ [DllImport("user32.dll")]
+ private static extern IntPtr GetForegroundWindow();
}
}
@@ -1,17 +1,15 @@
using System.Reflection;
-using System.Resources;
-using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Windows;
[assembly: AssemblyTitle("VirtualDesktop.Showcase")]
-[assembly: AssemblyDescription("")]
-[assembly: AssemblyConfiguration("")]
-[assembly: AssemblyCompany("")]
-[assembly: AssemblyProduct("VirtualDesktop.Showcase")]
-[assembly: AssemblyCopyright("Copyright © 2015")]
-[assembly: AssemblyTrademark("")]
-[assembly: AssemblyCulture("")]
+[assembly: AssemblyCompany("grabacr.net")]
+[assembly: AssemblyProduct("VirtualDesktop")]
+[assembly: AssemblyDescription("C# wrapper for IVirtualDesktopManager on Windows 10.")]
+[assembly: AssemblyCopyright("Copyright © 2015 Manato KAMEYA")]
+
[assembly: ComVisible(false)]
+[assembly: Guid("5B4544B8-3EF0-4E9F-8D60-DD605AD99725")]
[assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)]
+
[assembly: AssemblyVersion("1.0.0.0")]
@@ -10,5 +10,5 @@
[assembly: ComVisible(false)]
[assembly: Guid("9dd597c6-065a-4764-a96c-1b18c4eded78")]
-[assembly: AssemblyVersion("1.0.3")]
-[assembly: AssemblyInformationalVersion("1.0.3")]
+[assembly: AssemblyVersion("2.0.0")]
+[assembly: AssemblyInformationalVersion("2.0.0")]
@@ -13,7 +13,7 @@
<language>en-US</language>
<tags>Windows Windows10 Desktop VirtualDesktop WPF</tags>
<dependencies>
- <dependency id="VirtualDesktop" version="1.0.3" />
+ <dependency id="VirtualDesktop" version="2.0.0" />
</dependencies>
</metadata>
</package>
Oops, something went wrong.
@@ -0,0 +1,31 @@
+using System;
+
+namespace WindowsDesktop.Internal
+{
+ public class Disposable
+ {
+ public static IDisposable Create(Action dispose)
+ {
+ return new AnonymousDisposable(dispose);
+ }
+
+ private class AnonymousDisposable : IDisposable
+ {
+ private bool _isDisposed;
+ private readonly Action _dispose;
+
+ public AnonymousDisposable(Action dispose)
+ {
+ this._dispose = dispose;
+ }
+
+ public void Dispose()
+ {
+ if (this._isDisposed) return;
+
+ this._isDisposed = true;
+ this._dispose();
+ }
+ }
+ }
+}
@@ -0,0 +1,92 @@
+using System;
+using System.Collections.Concurrent;
+using System.Collections.Generic;
+using System.Linq;
+
+namespace WindowsDesktop.Interop
+{
+ public static class ComObjects
+ {
+ private static IDisposable _listener;
+ private static readonly ConcurrentDictionary<Guid, IVirtualDesktop> _virtualDesktops = new ConcurrentDictionary<Guid, IVirtualDesktop>();
+
+ internal static IVirtualDesktopManager VirtualDesktopManager { get; private set; }
+ internal static VirtualDesktopManagerInternal VirtualDesktopManagerInternal { get; private set; }
+ internal static IVirtualDesktopNotificationService VirtualDesktopNotificationService { get; private set; }
+ internal static IVirtualDesktopPinnedApps VirtualDesktopPinnedApps { get; private set; }
+ internal static IApplicationViewCollection ApplicationViewCollection { get; private set; }
+
+ internal static void Initialize()
+ {
+ VirtualDesktopManager = GetVirtualDesktopManager();
+ VirtualDesktopManagerInternal = VirtualDesktopManagerInternal.GetInstance();
+ VirtualDesktopNotificationService = GetVirtualDesktopNotificationService();
+ VirtualDesktopPinnedApps = GetVirtualDesktopPinnedApps();
+ ApplicationViewCollection = GetApplicationViewCollection();
+
+ _virtualDesktops.Clear();
+ _listener = VirtualDesktop.RegisterListener();
+ }
+
+ internal static void Register(IVirtualDesktop vd)
+ {
+ _virtualDesktops.AddOrUpdate(vd.GetID(), vd, (guid, desktop) => vd);
+ }
+
+ internal static IVirtualDesktop GetVirtualDesktop(Guid id)
+ {
+ return _virtualDesktops.GetOrAdd(id, x => VirtualDesktopManagerInternal.FindDesktop(ref x));
+ }
+
+ internal static void Terminate()
+ {
+ _listener?.Dispose();
+ }
+
+
+ #region public methods
+
+ public static IVirtualDesktopManager GetVirtualDesktopManager()
+ {
+ var vdmType = Type.GetTypeFromCLSID(CLSID.VirtualDesktopManager);
+ var instance = Activator.CreateInstance(vdmType);
+
+ return (IVirtualDesktopManager)instance;
+ }
+
+ public static IVirtualDesktopNotificationService GetVirtualDesktopNotificationService()
+ {
+ var shellType = Type.GetTypeFromCLSID(CLSID.ImmersiveShell);
+ var shell = (IServiceProvider)Activator.CreateInstance(shellType);
+
+ object ppvObject;
+ shell.QueryService(CLSID.VirtualDesktopNotificationService, typeof(IVirtualDesktopNotificationService).GUID, out ppvObject);
+
+ return (IVirtualDesktopNotificationService)ppvObject;
+ }
+
+ public static IVirtualDesktopPinnedApps GetVirtualDesktopPinnedApps()
+ {
+ var shellType = Type.GetTypeFromCLSID(CLSID.ImmersiveShell);
+ var shell = (IServiceProvider)Activator.CreateInstance(shellType);
+
+ object ppvObject;
+ shell.QueryService(CLSID.VirtualDesktopPinnedApps, typeof(IVirtualDesktopPinnedApps).GUID, out ppvObject);
+
+ return (IVirtualDesktopPinnedApps)ppvObject;
+ }
+
+ public static IApplicationViewCollection GetApplicationViewCollection()
+ {
+ var shellType = Type.GetTypeFromCLSID(CLSID.ImmersiveShell);
+ var shell = (IServiceProvider)Activator.CreateInstance(shellType);
+
+ object ppvObject;
+ shell.QueryService(typeof(IApplicationViewCollection).GUID, typeof(IApplicationViewCollection).GUID, out ppvObject);
+
+ return (IApplicationViewCollection)ppvObject;
+ }
+
+ #endregion
+ }
+}
@@ -10,9 +10,9 @@ internal interface IVirtualDesktopManagerInternal10130
{
int GetCount();
- void MoveViewToDesktop(object pView, IVirtualDesktop desktop);
+ void MoveViewToDesktop(IntPtr pView, IVirtualDesktop desktop);
- bool CanViewMoveDesktops(object pView);
+ bool CanViewMoveDesktops(IntPtr pView);
IVirtualDesktop GetCurrentDesktop();
@@ -36,9 +36,9 @@ internal interface IVirtualDesktopManagerInternal10240
{
int GetCount();
- void MoveViewToDesktop(object pView, IVirtualDesktop desktop);
+ void MoveViewToDesktop(IntPtr pView, IVirtualDesktop desktop);
- bool CanViewMoveDesktops(object pView);
+ bool CanViewMoveDesktops(IntPtr pView);
IVirtualDesktop GetCurrentDesktop();
@@ -62,9 +62,9 @@ internal interface IVirtualDesktopManagerInternal14328
{
int GetCount();
- void MoveViewToDesktop(object pView, IVirtualDesktop desktop);
+ void MoveViewToDesktop(IntPtr pView, IVirtualDesktop desktop);
- bool CanViewMoveDesktops(object pView);
+ bool CanViewMoveDesktops(IntPtr pView);
IVirtualDesktop GetCurrentDesktop();
@@ -1,48 +0,0 @@
-using System;
-
-namespace WindowsDesktop.Interop
-{
- public static class VirtualDesktopInteropHelper
- {
- public static IVirtualDesktopManager GetVirtualDesktopManager()
- {
- var vdmType = Type.GetTypeFromCLSID(CLSID.VirtualDesktopManager);
- var instance = Activator.CreateInstance(vdmType);
-
- return (IVirtualDesktopManager)instance;
- }
-
- public static IVirtualDesktopNotificationService GetVirtualDesktopNotificationService()
- {
- var shellType = Type.GetTypeFromCLSID(CLSID.ImmersiveShell);
- var shell = (IServiceProvider)Activator.CreateInstance(shellType);
-
- object ppvObject;
- shell.QueryService(CLSID.VirtualDesktopNotificationService, typeof(IVirtualDesktopNotificationService).GUID, out ppvObject);
-
- return (IVirtualDesktopNotificationService)ppvObject;
- }
-
- public static IVirtualDesktopPinnedApps GetVirtualDesktopPinnedApps()
- {
- var shellType = Type.GetTypeFromCLSID(CLSID.ImmersiveShell);
- var shell = (IServiceProvider)Activator.CreateInstance(shellType);
-
- object ppvObject;
- shell.QueryService(CLSID.VirtualDesktopPinnedApps, typeof(IVirtualDesktopPinnedApps).GUID, out ppvObject);
-
- return (IVirtualDesktopPinnedApps)ppvObject;
- }
-
- public static IApplicationViewCollection GetApplicationViewCollection()
- {
- var shellType = Type.GetTypeFromCLSID(CLSID.ImmersiveShell);
- var shell = (IServiceProvider)Activator.CreateInstance(shellType);
-
- object ppvObject;
- shell.QueryService(typeof(IApplicationViewCollection).GUID, typeof(IApplicationViewCollection).GUID, out ppvObject);
-
- return (IApplicationViewCollection)ppvObject;
- }
- }
-}
@@ -57,7 +57,7 @@ public int GetCount()
}
- public void MoveViewToDesktop(object pView, IVirtualDesktop desktop)
+ public void MoveViewToDesktop(IntPtr pView, IVirtualDesktop desktop)
{
if (this._manager14328 != null)
{
@@ -80,7 +80,7 @@ public void MoveViewToDesktop(object pView, IVirtualDesktop desktop)
throw new NotSupportedException();
}
- public bool CanViewMoveDesktops(object pView)
+ public bool CanViewMoveDesktops(IntPtr pView)
{
if (this._manager14328 != null)
{
@@ -10,5 +10,5 @@
[assembly: ComVisible(false)]
[assembly: Guid("ab848ecd-76aa-41c0-b63d-86a8591b25aa")]
-[assembly: AssemblyVersion("1.0.3")]
-[assembly: AssemblyInformationalVersion("1.0.3")]
+[assembly: AssemblyVersion("2.0.0")]
+[assembly: AssemblyInformationalVersion("2.0.0")]
@@ -20,11 +20,11 @@ public partial class VirtualDesktop
public Guid Id { get; }
[EditorBrowsable(EditorBrowsableState.Never)]
- public IVirtualDesktop ComObject { get; }
+ public IVirtualDesktop ComObject => ComObjects.GetVirtualDesktop(this.Id);
private VirtualDesktop(IVirtualDesktop comObject)
{
- this.ComObject = comObject;
+ ComObjects.Register(comObject);
this.Id = comObject.GetID();
}
@@ -34,7 +34,7 @@ private VirtualDesktop(IVirtualDesktop comObject)
/// </summary>
public void Switch()
{
- ComInternal.SwitchDesktop(this.ComObject);
+ ComObjects.VirtualDesktopManagerInternal.SwitchDesktop(this.ComObject);
}
/// <summary>
@@ -52,7 +52,7 @@ public void Remove(VirtualDesktop fallbackDesktop)
{
if (fallbackDesktop == null) throw new ArgumentNullException(nameof(fallbackDesktop));
- ComInternal.RemoveDesktop(this.ComObject, fallbackDesktop.ComObject);
+ ComObjects.VirtualDesktopManagerInternal.RemoveDesktop(this.ComObject, fallbackDesktop.ComObject);
}
/// <summary>
@@ -63,13 +63,13 @@ public VirtualDesktop GetLeft()
IVirtualDesktop desktop;
try
{
- desktop = ComInternal.GetAdjacentDesktop(this.ComObject, AdjacentDesktop.LeftDirection);
+ desktop = ComObjects.VirtualDesktopManagerInternal.GetAdjacentDesktop(this.ComObject, AdjacentDesktop.LeftDirection);
}
catch (COMException ex) when (ex.Match(HResult.TYPE_E_OUTOFBOUNDS))
{
return null;
}
- var wrapper = wrappers.GetOrAdd(desktop.GetID(), _ => new VirtualDesktop(desktop));
+ var wrapper = _wrappers.GetOrAdd(desktop.GetID(), _ => new VirtualDesktop(desktop));
return wrapper;
}
@@ -82,13 +82,13 @@ public VirtualDesktop GetRight()
IVirtualDesktop desktop;
try
{
- desktop = ComInternal.GetAdjacentDesktop(this.ComObject, AdjacentDesktop.RightDirection);
+ desktop = ComObjects.VirtualDesktopManagerInternal.GetAdjacentDesktop(this.ComObject, AdjacentDesktop.RightDirection);
}
catch (COMException ex) when (ex.Match(HResult.TYPE_E_OUTOFBOUNDS))
{
return null;
}
- var wrapper = wrappers.GetOrAdd(desktop.GetID(), _ => new VirtualDesktop(desktop));
+ var wrapper = _wrappers.GetOrAdd(desktop.GetID(), _ => new VirtualDesktop(desktop));
return wrapper;
}
Oops, something went wrong.

0 comments on commit 634d886

Please sign in to comment.