Skip to content

Commit

Permalink
在 Win32 窗口中显示一个正方形
Browse files Browse the repository at this point in the history
  • Loading branch information
wherewhere committed Nov 5, 2023
1 parent 8483c43 commit afae124
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
38 changes: 38 additions & 0 deletions CoreAppUWP/Helpers/WindowHelper.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
using CoreAppUWP.Common;
using Microsoft.UI;
using Microsoft.UI.Composition;
using Microsoft.UI.Content;
using Microsoft.UI.Dispatching;
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Windows.ApplicationModel.Core;
using Windows.UI.Core;
Expand Down Expand Up @@ -37,6 +41,40 @@ public static async Task<bool> CreateWindowAsync(Action<Window> launched)
return await ApplicationViewSwitcher.TryShowAsStandaloneAsync(newViewId);
}



public static Task<AppWindow> CreateAppWindowAsync(Action<DesktopChildSiteBridge, Compositor> launched)
{
TaskCompletionSource<AppWindow> taskCompletionSource = new();

Thread thread = new(async () =>
{
try
{
DispatcherQueueController controller = DispatcherQueueController.CreateOnCurrentThread();
DispatcherQueue dispatcherQueue = controller.DispatcherQueue;
AppWindow window = AppWindow.Create();
window.AssociateWithDispatcherQueue(dispatcherQueue);
taskCompletionSource.SetResult(window);
window.Destroying += (sender, args) => dispatcherQueue.EnqueueEventLoopExit();
Compositor compositor = new();
DesktopChildSiteBridge bridge = DesktopChildSiteBridge.Create(compositor, window.Id);
bridge.Show();
bridge.ResizePolicy = ContentSizePolicy.ResizeContentToParentWindow;
launched(bridge, compositor);
dispatcherQueue.RunEventLoop();
await controller.ShutdownQueueAsync();
}
catch (Exception e)
{
taskCompletionSource.SetException(e);
}
});
thread.Start();

return taskCompletionSource.Task;
}

public static void TrackWindow(this Window window)
{
if (!ActiveWindows.ContainsKey(window.Dispatcher))
Expand Down
16 changes: 13 additions & 3 deletions CoreAppUWP/Pages/SettingsPages/SettingsPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
using CoreAppUWP.Helpers;
using CoreAppUWP.ViewModels.SettingsPages;
using Microsoft.UI;
using Microsoft.UI.Composition;
using Microsoft.UI.Content;
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media.Animation;
using Microsoft.UI.Xaml.Navigation;
using System;
using System.Numerics;
using System.Threading.Tasks;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Core;
Expand Down Expand Up @@ -96,8 +99,16 @@ private async void Button_Click(object sender, RoutedEventArgs e)
});
break;
case "NewAppWindow":
AppWindow window = AppWindow.Create();
window.Closing += (sender, args) => sender.Destroy();
AppWindow window = await WindowHelper.CreateAppWindowAsync((bridge, compositor) =>
{
ContainerVisual root = compositor.CreateContainerVisual();
ContentIsland contentIsland = ContentIsland.Create(root);
SpriteVisual child = compositor.CreateSpriteVisual();
child.Size = new Vector2(100f, 100f);
child.Brush = compositor.CreateColorBrush(Color.FromArgb(0xFF, 0x00, 0x80, 0xFF));
root.Children.InsertAtTop(child);
bridge.Connect(contentIsland);
}).ConfigureAwait(false);
if (AppWindowTitleBar.IsCustomizationSupported())
{
AppWindowTitleBar TitleBar = window.TitleBar;
Expand All @@ -110,7 +121,6 @@ private async void Button_Click(object sender, RoutedEventArgs e)
TitleBar.BackgroundColor = TitleBar.InactiveBackgroundColor = BackgroundColor;
TitleBar.ButtonBackgroundColor = TitleBar.ButtonInactiveBackgroundColor = Colors.Transparent;
}
window.AssociateWithDispatcherQueue(DispatcherQueue);
window.Title = Package.Current.DisplayName;
window.SetIcon("favicon.ico");
window.Show();
Expand Down

0 comments on commit afae124

Please sign in to comment.