Files
Shrlalgo.RvKits/WPFluent/Controls/ContentDialog/ClientAreaBorder.cs

142 lines
5.6 KiB
C#
Raw Normal View History

2025-05-05 17:04:06 +08:00
using System.Windows.Shell;
2025-04-24 20:56:44 +08:00
namespace WPFluent.Controls;
/// <summary>
/// If you use <see cref="WindowChrome"/> to extend the UI elements to the non-client area, you can include this
/// container in the template of <see cref="Window"/> so that the content inside automatically fills the client area.
/// Using this container can let you get rid of various margin adaptations done in Setter/Trigger of the style of <see
/// cref="Window"/> when the window state changes.
/// </summary>
/// <example>
/// <code lang="xml"> &lt;Style x:Key="MyWindowCustomStyle" BasedOn="{StaticResource {x:Type Window}}"
/// TargetType="{x:Type controls:FluentWindow}"&gt; &lt;Setter Property="Template" &gt; &lt;Setter.Value&gt;
/// &lt;ControlTemplate TargetType="{x:Type Window}"&gt; &lt;AdornerDecorator&gt; &lt;controls:ClientAreaBorder
2025-05-05 17:04:06 +08:00
/// Background="{TemplateBinding Background}" ControlBorderBrush="{TemplateBinding ControlBorderBrush}"
/// BorderThickness="{TemplateBinding BorderThickness}"&gt; &lt;ContentPresenter x:Name="ContentPresenter" /&gt;
/// &lt;/controls:ClientAreaBorder&gt; &lt;/AdornerDecorator&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt;
/// &lt;/Setter&gt; &lt;/Style&gt;</code>
/// </example>
2025-05-05 17:04:06 +08:00
public class ClientAreaBorder : System.Windows.Controls.Border/*, IThemeControl*/
{
/*private const int SM_CXFRAME = 32;
private const int SM_CYFRAME = 33;
private const int SM_CXPADDEDBORDER = 92;*/
private static Thickness? _resizeFrameBorderThickness;
private static Thickness? _windowChromeNonClientFrameThickness;
2025-05-05 17:04:06 +08:00
//private bool borderBrushApplied = false;
private System.Windows.Window? _oldWindow;
2025-05-05 17:04:06 +08:00
//public ClientAreaBorder()
//{
// ApplicationTheme = Appearance.ThemeManager.GetAppTheme();
// Appearance.ThemeManager.Changed += OnThemeChanged;
//}
//private void ApplyDefaultWindowBorder()
//{
// if (_oldWindow == null)
// {
// return;
// }
// borderBrushApplied = true;
// // SystemParameters.WindowGlassBrush
// Color borderColor =
// ApplicationTheme == ThemeType.Light
// ? Color.FromArgb(0xFF, 0x7A, 0x7A, 0x7A)
// : Color.FromArgb(0xFF, 0x3A, 0x3A, 0x3A);
// _oldWindow.SetCurrentValue(
// System.Windows.Controls.Control.BorderBrushProperty,
// new SolidColorBrush(borderColor));
// _oldWindow.SetCurrentValue(System.Windows.Controls.Control.BorderThicknessProperty, new Thickness(1));
//}
//private void OnThemeChanged(ThemeType currentApplicationTheme, Color systemAccent)
//{
// ApplicationTheme = currentApplicationTheme;
// if (!borderBrushApplied || _oldWindow == null)
// {
// return;
// }
// ApplyDefaultWindowBorder();
//}
private void OnWindowClosing(object? sender, CancelEventArgs e)
{
2025-05-05 17:04:06 +08:00
//Appearance.ThemeManager.Changed -= OnThemeChanged;
2025-04-24 20:56:44 +08:00
if (_oldWindow != null)
{
_oldWindow.Closing -= OnWindowClosing;
}
}
private void OnWindowStateChanged(object? sender, EventArgs e)
{
2025-04-24 20:56:44 +08:00
if (sender is not System.Windows.Window window)
{
return;
}
Thickness padding =
window.WindowState == WindowState.Maximized ? WindowChromeNonClientFrameThickness : default;
SetCurrentValue(PaddingProperty, padding);
}
/// <inheritdoc/>
protected override void OnVisualParentChanged(DependencyObject oldParent)
{
base.OnVisualParentChanged(oldParent);
2025-04-24 20:56:44 +08:00
if (_oldWindow is { } oldWindow)
{
oldWindow.StateChanged -= OnWindowStateChanged;
oldWindow.Closing -= OnWindowClosing;
}
var newWindow = System.Windows.Window.GetWindow(this);
2025-04-24 20:56:44 +08:00
if (newWindow is not null)
{
newWindow.StateChanged -= OnWindowStateChanged; // Unsafe
newWindow.StateChanged += OnWindowStateChanged;
newWindow.Closing += OnWindowClosing;
}
_oldWindow = newWindow;
2025-05-05 17:04:06 +08:00
//ApplyDefaultWindowBorder();
}
2025-05-05 17:04:06 +08:00
//public ThemeType ApplicationTheme { get; set; } = ThemeType.Unknow;
/// <summary>
/// Gets the system <see cref="User32.SM.CXFRAME"/> and <see cref="User32.SM.CYFRAME"/> values in WPF units.
/// </summary>
public static Thickness ResizeFrameBorderThickness => _resizeFrameBorderThickness ??= new Thickness(
SystemParameters.ResizeFrameVerticalBorderWidth,
SystemParameters.ResizeFrameHorizontalBorderHeight,
SystemParameters.ResizeFrameVerticalBorderWidth,
SystemParameters.ResizeFrameHorizontalBorderHeight);
/// <summary>
/// Gets the thickness of the window's non-client frame used for maximizing the window with a custom chrome.
/// </summary>
/// <remarks>
/// If you use a <see cref="WindowChrome"/> to extend the client area of a window to the non-client area, you need
/// to handle the edge margin issue when the window is maximized. Use this property to get the correct margin value
/// when the window is maximized, so that when the window is maximized, the client area can completely cover the
/// screen client area by no less than a single pixel at any DPI. The<see cref="User32.GetSystemMetrics"/> method
/// cannot obtain this value directly.
/// </remarks>
public Thickness WindowChromeNonClientFrameThickness => _windowChromeNonClientFrameThickness ??= new Thickness(
2025-04-24 20:56:44 +08:00
ClientAreaBorder.ResizeFrameBorderThickness.Left,
ClientAreaBorder.ResizeFrameBorderThickness.Top,
ClientAreaBorder.ResizeFrameBorderThickness.Right,
ClientAreaBorder.ResizeFrameBorderThickness.Bottom);
}