From 1bc5d96b60a72054acb552918fc511df470bc580 Mon Sep 17 00:00:00 2001 From: Claris <2318678921@qq.com> Date: Sat, 4 Nov 2023 21:36:08 +0800 Subject: [PATCH 1/3] Enable drag region customization in UWP --- CoreAppUWP/Pages/MainPage.xaml | 6 +-- CoreAppUWP/Pages/MainPage.xaml.cs | 67 +++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 4 deletions(-) diff --git a/CoreAppUWP/Pages/MainPage.xaml b/CoreAppUWP/Pages/MainPage.xaml index 94eeda4..7731822 100644 --- a/CoreAppUWP/Pages/MainPage.xaml +++ b/CoreAppUWP/Pages/MainPage.xaml @@ -17,9 +17,7 @@ IsHitTestVisible="True"> - - - + @@ -57,7 +55,7 @@ > 1)) / 96); + return scaleFactorPercent / 100.0; + } + + private void DragRegion_Loaded(object sender, RoutedEventArgs e) + { + + var m_AppWindow = GetAppWindowForCurrentWindow(); + double scaleAdjustment = GetScaleAdjustment(GetWindowhWnd()); + + List dragRectsList = new(); + + Windows.Graphics.RectInt32 dragRect; + dragRect.X = (int)(LeftPaddingColumn.ActualWidth * scaleAdjustment); + dragRect.Y = 0; + dragRect.Height = (int)(AppTitleBar.ActualHeight * scaleAdjustment); + dragRect.Width = (int)(DragColumn.ActualWidth * scaleAdjustment); ; + dragRectsList.Add(dragRect); + + + + Windows.Graphics.RectInt32[] dragRects = dragRectsList.ToArray(); + + m_AppWindow.TitleBar.SetDragRectangles(dragRects); + } + private Microsoft.UI.Windowing.AppWindow GetAppWindowForCurrentWindow() + { + WindowId wndId = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(GetWindowhWnd()); + return Microsoft.UI.Windowing.AppWindow.GetFromWindowId(wndId); + } + private IntPtr GetWindowhWnd() + { + var appcoreWindow = CoreWindow.GetForCurrentThread(); + var interop = appcoreWindow.As(); + return interop.WindowHandle; + } } } From b8a80bb6599e72309699bdabb9b5afd90c8599da Mon Sep 17 00:00:00 2001 From: Claris <2318678921@qq.com> Date: Sat, 4 Nov 2023 21:53:13 +0800 Subject: [PATCH 2/3] Roll back to dotnet 7 --- CoreAppUWP/CoreAppUWP.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CoreAppUWP/CoreAppUWP.csproj b/CoreAppUWP/CoreAppUWP.csproj index e76181f..96f505d 100644 --- a/CoreAppUWP/CoreAppUWP.csproj +++ b/CoreAppUWP/CoreAppUWP.csproj @@ -12,7 +12,7 @@ win10-$(Platform).pubxml https://github.com/wherewhere/CoreAppUWP win-x86;win-x64;win-arm64 - net8.0-windows10.0.22621.0 + net7.0-windows10.0.22621.0 10.0.17763.0 partial True From 7c2f0431ed6503090bc47e9ba9ef0219b20701ad Mon Sep 17 00:00:00 2001 From: wherewhere Date: Sat, 4 Nov 2023 22:59:09 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=95=B4=E7=90=86=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CoreAppUWP/Helpers/UIHelper.cs | 7 ++++ CoreAppUWP/Helpers/WindowHelper.cs | 23 ++++++++++ CoreAppUWP/Pages/MainPage.xaml | 13 ++++-- CoreAppUWP/Pages/MainPage.xaml.cs | 67 ++---------------------------- 4 files changed, 43 insertions(+), 67 deletions(-) diff --git a/CoreAppUWP/Helpers/UIHelper.cs b/CoreAppUWP/Helpers/UIHelper.cs index 50f068d..24c8f09 100644 --- a/CoreAppUWP/Helpers/UIHelper.cs +++ b/CoreAppUWP/Helpers/UIHelper.cs @@ -3,6 +3,7 @@ using System.Threading; using System.Threading.Tasks; using Windows.Foundation.Metadata; +using Windows.Graphics.Display; namespace CoreAppUWP.Helpers { @@ -10,6 +11,12 @@ public static class UIHelper { public static bool HasStatusBar => ApiInformation.IsTypePresent("Windows.UI.ViewManagement.StatusBar"); + public static int GetActualPixel(this double pixel) + { + double currentDpi = DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel; + return Convert.ToInt32(pixel * currentDpi); + } + public static string ExceptionToMessage(this Exception ex) { StringBuilder builder = new(); diff --git a/CoreAppUWP/Helpers/WindowHelper.cs b/CoreAppUWP/Helpers/WindowHelper.cs index f1abe60..6985082 100644 --- a/CoreAppUWP/Helpers/WindowHelper.cs +++ b/CoreAppUWP/Helpers/WindowHelper.cs @@ -1,4 +1,6 @@ using CoreAppUWP.Common; +using Microsoft.UI; +using Microsoft.UI.Windowing; using Microsoft.UI.Xaml; using System; using System.Collections.Generic; @@ -6,6 +8,9 @@ using Windows.ApplicationModel.Core; using Windows.UI.Core; using Windows.UI.ViewManagement; +using Windows.Win32.Foundation; +using Windows.Win32.System.WinRT; +using WinRT; namespace CoreAppUWP.Helpers { @@ -48,6 +53,24 @@ public static void TrackWindow(this Window window) } } + public static AppWindow GetAppWindow(this CoreWindow window) + { + if (!ActiveAppWindows.TryGetValue(window, out AppWindow appWindow)) + { + HWND handle = window.As().WindowHandle; + WindowId id = Win32Interop.GetWindowIdFromWindow(handle); + appWindow = AppWindow.GetFromWindowId(id); + window.Closed += (sender, args) => + { + ActiveAppWindows.Remove(window); + window = null; + }; + ActiveAppWindows[window] = appWindow; + } + return appWindow; + } + public static Dictionary ActiveWindows { get; } = []; + public static Dictionary ActiveAppWindows { get; } = []; } } diff --git a/CoreAppUWP/Pages/MainPage.xaml b/CoreAppUWP/Pages/MainPage.xaml index 61922fa..9d109f6 100644 --- a/CoreAppUWP/Pages/MainPage.xaml +++ b/CoreAppUWP/Pages/MainPage.xaml @@ -16,10 +16,13 @@ Height="48" VerticalAlignment="Top" Canvas.ZIndex="1" - IsHitTestVisible="True"> + IsHitTestVisible="True" + SizeChanged="CustomTitleBar_SizeChanged"> - + + + @@ -54,10 +57,12 @@ TextTrimming="CharacterEllipsis" TextWrapping="NoWrap" /> - > 1)) / 96); - return scaleFactorPercent / 100.0; - } - - private void DragRegion_Loaded(object sender, RoutedEventArgs e) - { - - var m_AppWindow = GetAppWindowForCurrentWindow(); - double scaleAdjustment = GetScaleAdjustment(GetWindowhWnd()); - - List dragRectsList = new(); - - Windows.Graphics.RectInt32 dragRect; - dragRect.X = (int)(LeftPaddingColumn.ActualWidth * scaleAdjustment); - dragRect.Y = 0; - dragRect.Height = (int)(AppTitleBar.ActualHeight * scaleAdjustment); - dragRect.Width = (int)(DragColumn.ActualWidth * scaleAdjustment); ; - dragRectsList.Add(dragRect); - - - - Windows.Graphics.RectInt32[] dragRects = dragRectsList.ToArray(); - - m_AppWindow.TitleBar.SetDragRectangles(dragRects); - } - private Microsoft.UI.Windowing.AppWindow GetAppWindowForCurrentWindow() - { - WindowId wndId = Microsoft.UI.Win32Interop.GetWindowIdFromWindow(GetWindowhWnd()); - return Microsoft.UI.Windowing.AppWindow.GetFromWindowId(wndId); - } - private IntPtr GetWindowhWnd() - { - var appcoreWindow = CoreWindow.GetForCurrentThread(); - var interop = appcoreWindow.As(); - return interop.WindowHandle; + RectInt32 Rect = new((ActualWidth - DragRegion.ActualWidth).GetActualPixel(), 0, DragRegion.ActualWidth.GetActualPixel(), DragRegion.ActualHeight.GetActualPixel()); + Window.Current.CoreWindow.GetAppWindow().TitleBar.SetDragRectangles([Rect]); } } }