diff --git a/src/Wpf.Ui/Controls/TitleBar/TitleBar.cs b/src/Wpf.Ui/Controls/TitleBar/TitleBar.cs index c0cd90e90..63a32234e 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; } @@ -465,6 +471,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); @@ -652,6 +667,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 {