Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/Wpf.Ui/Controls/TitleBar/TitleBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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

/// <summary>
Expand Down Expand Up @@ -423,6 +427,8 @@ public TitleBar()
{
SetValue(TemplateButtonCommandProperty, new RelayCommand<TitleBarButtonType>(OnTemplateButtonClick));

dpiScale ??= VisualTreeHelper.GetDpi(this);

Loaded += OnLoaded;
Unloaded += OnUnloaded;
}
Expand Down Expand Up @@ -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<System.Windows.Controls.Grid>(ElementMainGrid);
_icon = GetTemplateChild<System.Windows.Controls.ContentPresenter>(ElementIcon);

Expand Down Expand Up @@ -652,6 +667,15 @@ or User32.WM.NCLBUTTONUP
}
}

/// <summary>
/// Show 'SystemMenu' on mouse right button up.
/// </summary>
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<T>(string name)
where T : DependencyObject
{
Expand Down