diff --git a/src/WinUIEx/HwndExtensions.cs b/src/WinUIEx/HwndExtensions.cs
index e2b5c709..c5e2f995 100644
--- a/src/WinUIEx/HwndExtensions.cs
+++ b/src/WinUIEx/HwndExtensions.cs
@@ -1,14 +1,12 @@
using System;
-using System.Runtime.CompilerServices;
+using System.ComponentModel;
using System.Runtime.InteropServices;
+using Microsoft.UI;
+using Windows.ApplicationModel.DataTransfer;
using Windows.Win32;
using Windows.Win32.Foundation;
-using Windows.Win32.UI.WindowsAndMessaging;
using Windows.Win32.Graphics.Gdi;
-using WinRT;
-using Microsoft.UI;
-using Microsoft.UI.Xaml.Controls.Primitives;
-using System.ComponentModel;
+using Windows.Win32.UI.WindowsAndMessaging;
namespace WinUIEx
{
@@ -372,6 +370,29 @@ public static void SetLayeredWindowAttributes(IntPtr hWnd, byte chromaKeyRed, by
if (!PInvoke.SetLayeredWindowAttributes(handle, color, alpha, LAYERED_WINDOW_ATTRIBUTES_FLAGS.LWA_COLORKEY | LAYERED_WINDOW_ATTRIBUTES_FLAGS.LWA_ALPHA))
Marshal.ThrowExceptionForHR(Marshal.GetLastWin32Error());
}*/
+
+ private static readonly Guid dtmIid = new(0xa5caee9b, 0x8708, 0x49d1, 0x8d, 0x36, 0x67, 0xd2, 0x5a, 0x8d, 0xa0, 0x0c);
+
+ ///
+ /// Gets the DataTransferManager for the specified window handle
+ ///
+ /// Window handle
+ /// The DataTransferManager associated with the specified window handle.
+ public static DataTransferManager GetDataTransferManagerForWindow(IntPtr hwnd)
+ {
+ IDataTransferManagerInterop interop = DataTransferManager.As();
+ return WinRT.MarshalInterface.FromAbi(interop.GetForWindow(hwnd, dtmIid));
+ }
+
+ ///
+ /// Displays the Windows Share UI for the specified window handle.
+ ///
+ /// Window handle
+ public static void ShowShareUIForWindow(IntPtr hwnd)
+ {
+ IDataTransferManagerInterop interop = DataTransferManager.As();
+ interop.ShowShareUIForWindow(hwnd);
+ }
}
///
@@ -441,7 +462,7 @@ public enum WindowStyle : int
Visible = 0x10000000,
/// The window has a vertical scroll bar.
VScroll = 0x00200000,
- }
+ }
///
/// Flags used for ToggleExtendedWindowStyle method
@@ -512,4 +533,25 @@ public enum ExtendedWindowStyle : int
/// The window has a border with a raised edge.
WindowEdge = 0x00000100,
}
+
+ [System.Runtime.InteropServices.ComImport]
+ [System.Runtime.InteropServices.Guid("3A3DCD6C-3EAB-43DC-BCDE-45671CE800C8")]
+ [System.Runtime.InteropServices.InterfaceType(
+ System.Runtime.InteropServices.ComInterfaceType.InterfaceIsIUnknown)]
+ interface IDataTransferManagerInterop
+ {
+ ///
+ /// Initializes the Share UI for the specified window.
+ ///
+ /// The window for which to initialize the Share UI.
+ /// The interface ID of the IDataTransferManager interface.
+ /// The IDataTransferManager interface for the specified window.
+ IntPtr GetForWindow([System.Runtime.InteropServices.In] IntPtr appWindow, [System.Runtime.InteropServices.In] ref Guid riid);
+
+ ///
+ /// Displays the Share UI for the specified window.
+ ///
+ /// The window for which to display the Share UI.
+ void ShowShareUIForWindow(IntPtr appWindow);
+ }
}
diff --git a/src/WinUIEx/WindowExtensions.cs b/src/WinUIEx/WindowExtensions.cs
index dc57a5c4..eb2306f8 100644
--- a/src/WinUIEx/WindowExtensions.cs
+++ b/src/WinUIEx/WindowExtensions.cs
@@ -1,7 +1,9 @@
using Microsoft.UI;
using Microsoft.UI.Windowing;
using System;
+using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
+using Windows.ApplicationModel.DataTransfer;
using Windows.Win32;
namespace WinUIEx
@@ -121,7 +123,7 @@ private static void UpdateOverlappedPresenter(this Microsoft.UI.Xaml.Window wind
var appwindow = window.AppWindow;
if (appwindow.Presenter is OverlappedPresenter overlapped)
action(overlapped);
- else
+ else
throw new NotSupportedException($"Not supported with a {appwindow.Presenter.Kind} presenter");
}
private static T GetOverlappedPresenterValue(this Microsoft.UI.Xaml.Window window, Func action)
@@ -361,7 +363,7 @@ public static void SetWindowChromaKey(this Microsoft.UI.Xaml.Window window, Wind
/// window
/// The color that specifies the transparency color key to be used when composing the layered window. All pixels painted by the window in this color will be transparent.
/// Alpha value used to describe the opacity of the layered window. When is 0, the window is completely transparent. When is 255, the window is opaque.
- public static void SetLayeredWindowAttributes(this Microsoft.UI.Xaml.Window window, Windows.UI.Color chromaKey, byte alpha)
+ public static void SetLayeredWindowAttributes(this Microsoft.UI.Xaml.Window window, Windows.UI.Color chromaKey, byte alpha)
=> HwndExtensions.SetLayeredWindowAttributes(GetWindowHandle(window), chromaKey.R, chromaKey.G, chromaKey.B, alpha);*/
///
@@ -385,5 +387,18 @@ public static void SetRegion(this Microsoft.UI.Xaml.Window window, Region? regio
PInvoke.DeleteObject(rgn);
}
}
+
+ ///
+ /// Gets the for the specified window.
+ ///
+ /// The window for which to get the .
+ /// The associated with the specified window.
+ public static DataTransferManager GetDataTransferManager(this Microsoft.UI.Xaml.Window window) => HwndExtensions.GetDataTransferManagerForWindow(window.GetWindowHandle());
+
+ ///
+ /// Displays the Windows Share UI for the specified window.
+ ///
+ /// The window for which to display the Share UI.
+ public static void ShowShareUI(this Microsoft.UI.Xaml.Window window) => HwndExtensions.ShowShareUIForWindow(window.GetWindowHandle());
}
}
diff --git a/src/WinUIExSample/Pages/Dialogs.xaml b/src/WinUIExSample/Pages/Dialogs.xaml
index 710b546d..65557c9d 100644
--- a/src/WinUIExSample/Pages/Dialogs.xaml
+++ b/src/WinUIExSample/Pages/Dialogs.xaml
@@ -20,6 +20,7 @@
+
diff --git a/src/WinUIExSample/Pages/Dialogs.xaml.cs b/src/WinUIExSample/Pages/Dialogs.xaml.cs
index 379e4829..a76f0813 100644
--- a/src/WinUIExSample/Pages/Dialogs.xaml.cs
+++ b/src/WinUIExSample/Pages/Dialogs.xaml.cs
@@ -10,6 +10,7 @@
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
+using Windows.ApplicationModel.DataTransfer;
using Windows.Foundation;
using Windows.Foundation.Collections;
using WinUIEx;
@@ -30,6 +31,7 @@ public Dialogs()
}
public WindowEx MainWindow => ((App)Application.Current).MainWindow!;
+ private TypedEventHandler? shareDataProvider;
private async void ShowDialog_Click(object sender, RoutedEventArgs e)
{
@@ -65,5 +67,23 @@ private async void ShowDialog3_Click(object sender, RoutedEventArgs e)
resultText.Text = "You chose: " + result.Label;
}
+ private async void ShowShareSheet_Click(object sender, RoutedEventArgs e)
+ {
+ var dtm = MainWindow.GetDataTransferManager();
+ dtm.DataRequested -= shareDataProvider;
+
+ shareDataProvider = (sender, args) =>
+ {
+ sender.DataRequested -= shareDataProvider;
+ resultText.Text = "";
+ args.Request.Data.Properties.Title = "WinUIEx Sample Share";
+ args.Request.Data.SetUri(new Uri("https://github.com/dotMorten/WinUIEx"));
+ args.Request.Data.SetText("Hello from WinUIEx!");
+ };
+
+ dtm.DataRequested += shareDataProvider;
+ MainWindow.ShowShareUI();
+ }
+
}
}