From b1c1eb8021f45a7dfcdc152b151f8219b3baecd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BF=83=E5=80=BE?= <38943319+LHTXQ@users.noreply.github.com> Date: Sat, 6 May 2023 21:51:34 +0800 Subject: [PATCH 1/2] Show 'SystemMenu' on mouse right button up. --- Commit by LHTXQ --- src/Wpf.Ui/Controls/TitleBar.cs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Wpf.Ui/Controls/TitleBar.cs b/src/Wpf.Ui/Controls/TitleBar.cs index f95153e60..306c00768 100644 --- a/src/Wpf.Ui/Controls/TitleBar.cs +++ b/src/Wpf.Ui/Controls/TitleBar.cs @@ -27,6 +27,10 @@ public class TitleBar : System.Windows.Controls.Control, IThemeControl private const string ElementRestoreButton = "PART_RestoreButton"; + private static DpiScale? dpiScale; + + private DependencyObject? parentWindow; + private System.Windows.Window _parent; internal Interop.WinDef.POINT _doubleClickPoint; @@ -380,7 +384,7 @@ public event RoutedEventHandler HelpClicked public TitleBar() { SetValue(TemplateButtonCommandProperty, new Common.RelayCommand(o => OnTemplateButtonClick(o ?? String.Empty))); - + dpiScale ??= VisualTreeHelper.GetDpi(this); Loaded += OnLoaded; } @@ -407,6 +411,13 @@ public override void OnApplyTemplate() { base.OnApplyTemplate(); + parentWindow = VisualTreeHelper.GetParent(this); + while (parentWindow != null && parentWindow is not Window) + { + parentWindow = VisualTreeHelper.GetParent(parentWindow); + } + this.MouseRightButtonUp += TitleBar_MouseRightButtonUp; + var mainGrid = GetTemplateChild(ElementMainGrid) as System.Windows.Controls.Grid; var maximizeButton = GetTemplateChild(ElementMaximizeButton) as Wpf.Ui.Controls.Button; var restoreButton = GetTemplateChild(ElementRestoreButton) as Wpf.Ui.Controls.Button; @@ -421,6 +432,15 @@ public override void OnApplyTemplate() InitializeSnapLayout(maximizeButton, restoreButton); } + /// + /// Show 'SystemMenu' on mouse right button up. + /// + private void TitleBar_MouseRightButtonUp(object sender, MouseButtonEventArgs e) + { + var point = PointToScreen(e.GetPosition(this)); + SystemCommands.ShowSystemMenu(parentWindow as Window, new Point(point.X / dpiScale.Value.DpiScaleX, point.Y / dpiScale.Value.DpiScaleY)); + } + /// /// This virtual method is triggered when the app's theme changes. /// From fe0e1429075276808523640cba025fc4d0401cf8 Mon Sep 17 00:00:00 2001 From: pomianowski <13592821+pomianowski@users.noreply.github.com> Date: Mon, 13 Nov 2023 21:15:05 +0100 Subject: [PATCH 2/2] Rollback changes --- src/Wpf.Ui/Controls/TitleBar/TitleBar.cs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/Wpf.Ui/Controls/TitleBar/TitleBar.cs b/src/Wpf.Ui/Controls/TitleBar/TitleBar.cs index 82e301b9e..ecf090b11 100644 --- a/src/Wpf.Ui/Controls/TitleBar/TitleBar.cs +++ b/src/Wpf.Ui/Controls/TitleBar/TitleBar.cs @@ -32,6 +32,10 @@ public class TitleBar : System.Windows.Controls.Control, IThemeControl private const string ElementRestoreButton = "PART_RestoreButton"; private const string ElementCloseButton = "PART_CloseButton"; + private static DpiScale? dpiScale; + + private DependencyObject? parentWindow; + #region Static properties /// @@ -423,6 +427,8 @@ public TitleBar() { SetValue(TemplateButtonCommandProperty, new RelayCommand(OnTemplateButtonClick)); + dpiScale ??= VisualTreeHelper.GetDpi(this); + Loaded += OnLoaded; Unloaded += OnUnloaded; } @@ -469,6 +475,15 @@ public override void OnApplyTemplate() { base.OnApplyTemplate(); + parentWindow = VisualTreeHelper.GetParent(this); + + while (parentWindow != null && parentWindow is not Window) + { + parentWindow = VisualTreeHelper.GetParent(parentWindow); + } + + this.MouseRightButtonUp += TitleBar_MouseRightButtonUp; + _mainGrid = GetTemplateChild(ElementMainGrid); _icon = GetTemplateChild(ElementIcon); @@ -642,6 +657,15 @@ or User32.WM.NCLBUTTONUP } } + /// + /// Show 'SystemMenu' on mouse right button up. + /// + private void TitleBar_MouseRightButtonUp(object sender, MouseButtonEventArgs e) + { + var point = PointToScreen(e.GetPosition(this)); + SystemCommands.ShowSystemMenu(parentWindow as Window, new Point(point.X / dpiScale.Value.DpiScaleX, point.Y / dpiScale.Value.DpiScaleY)); + } + private T GetTemplateChild(string name) where T : DependencyObject {