|
| 1 | +using System; |
| 2 | +using System.Runtime.InteropServices; |
| 3 | +using Windows.ApplicationModel.Core; |
| 4 | +using Windows.UI.Core; |
| 5 | +using Windows.UI.ViewManagement; |
| 6 | +using Windows.UI.WindowManagement; |
| 7 | +using static FullTrustUWP.Core.InteropHelper; |
| 8 | + |
| 9 | +namespace FullTrustUWP.Core.Activation |
| 10 | +{ |
| 11 | + public static class CoreWindowActivator |
| 12 | + { |
| 13 | + public enum WindowType : long |
| 14 | + { |
| 15 | + IMMERSIVE_DOCK = 1, |
| 16 | + IMMERSIVE_HOSTED, |
| 17 | + IMMERSIVE_BODY_ACTIVE = 4, |
| 18 | + IMMERSIVE_DOCK_ACTIVE, |
| 19 | + NOT_IMMERSIVE |
| 20 | + } |
| 21 | + |
| 22 | + private delegate int PrivateCreateCoreWindow_Sig(WindowType windowType, [MarshalAs(UnmanagedType.BStr)] string windowTitle, int x, int y, uint width, uint height, uint dwAttributes, IntPtr hOwnerWindow, Guid riid, out ICoreWindowInterop windowRef); |
| 23 | + private static PrivateCreateCoreWindow_Sig PrivateCreateCoreWindow_Ref; |
| 24 | + |
| 25 | + public static CoreWindow CreateCoreWindow(WindowType windowType, string windowTitle, IntPtr hOwnerWindow, int x = 0, int y = 0, uint width = 10, uint height = 10, uint dwAttributes = 0) |
| 26 | + { |
| 27 | + if (PrivateCreateCoreWindow_Ref == null) |
| 28 | + PrivateCreateCoreWindow_Ref = DynamicLoad<PrivateCreateCoreWindow_Sig>("windows.ui.dll", 0x5dc); |
| 29 | + |
| 30 | + Marshal.ThrowExceptionForHR(PrivateCreateCoreWindow_Ref(windowType, windowTitle, x, y, width, height, dwAttributes, hOwnerWindow, typeof(ICoreWindowInterop).GUID, out ICoreWindowInterop windowRef)); |
| 31 | + return windowRef as object as CoreWindow; |
| 32 | + } |
| 33 | + |
| 34 | + private delegate int CreateCoreApplicationViewTitleBar_Sig(CoreWindow titleBarClientAdapter, IntPtr hWnd, out CoreApplicationViewTitleBar titleBar); |
| 35 | + private static CreateCoreApplicationViewTitleBar_Sig CreateCoreApplicationViewTitleBar_Ref; |
| 36 | + |
| 37 | + public static CoreApplicationViewTitleBar CreateCoreApplicationViewTitleBar(CoreWindow coreWindow, IntPtr hWnd) |
| 38 | + { |
| 39 | + if (CreateCoreApplicationViewTitleBar_Ref == null) |
| 40 | + CreateCoreApplicationViewTitleBar_Ref = DynamicLoad<CreateCoreApplicationViewTitleBar_Sig>("twinapi.appcore.dll", 501); |
| 41 | + |
| 42 | + Marshal.ThrowExceptionForHR(CreateCoreApplicationViewTitleBar_Ref(coreWindow, hWnd, out var titleBar)); |
| 43 | + return titleBar; |
| 44 | + } |
| 45 | + |
| 46 | + [DllImport("twinapi.appcore.dll", SetLastError = true)] |
| 47 | + private static extern int CreateCoreApplicationViewTitleBar(CoreWindow titleBarClientAdapter, IntPtr hWnd, out CoreApplicationViewTitleBar titleBar); |
| 48 | + |
| 49 | + [DllImport("twinapi.appcore", SetLastError = true)] |
| 50 | + private static extern int CreateApplicationViewTitleBar(AppWindow titleBarClientAdapter, IntPtr hWnd, out ApplicationViewTitleBar titleBar); |
| 51 | + } |
| 52 | + |
| 53 | + [ComImport, Guid(Const.IID_ICoreWindowInterop)] |
| 54 | + [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] |
| 55 | + public interface ICoreWindowInterop |
| 56 | + { |
| 57 | + IntPtr WindowHandle { get; } |
| 58 | + bool MessageHandled { get; } |
| 59 | + } |
| 60 | + |
| 61 | + [InterfaceType(ComInterfaceType.InterfaceIsIInspectable)] |
| 62 | + [Guid("CD292360-2763-4085-8A9F-74B224A29175")] |
| 63 | + public interface ICoreWindowFactory |
| 64 | + { |
| 65 | + void CreateCoreWindow([MarshalAs(UnmanagedType.HString)] string windowTitle, out CoreWindow window); |
| 66 | + bool WindowReuseAllowed { get; } |
| 67 | + } |
| 68 | + |
| 69 | + public sealed class CoreWindowFactory : ICoreWindowFactory |
| 70 | + { |
| 71 | + public bool WindowReuseAllowed => true; |
| 72 | + |
| 73 | + public void CreateCoreWindow([MarshalAs(UnmanagedType.HString)] string windowTitle, out CoreWindow window) |
| 74 | + { |
| 75 | + window = CoreWindowActivator.CreateCoreWindow( |
| 76 | + CoreWindowActivator.WindowType.IMMERSIVE_BODY_ACTIVE, |
| 77 | + windowTitle, |
| 78 | + IntPtr.Zero, |
| 79 | + width: 1024, |
| 80 | + height: 768 |
| 81 | + ); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments