一、在窗口设置取消自带窗口

xmlns:local="clr-namespace:CodeGenerator"
ExtendClientAreaToDecorationsHint="True"
ExtendClientAreaChromeHints="NoChrome"
ExtendClientAreaTitleBarHeightHint="-1"

二、在窗口设置类并增加自定义标题

上述设置第一条已经引入自定义标题类,以下代码可以参考用来设置自定义标题。

<Grid Margin="5 5 5 5">
		<Grid.RowDefinitions>
			<RowDefinition Height="1*"/>
			<RowDefinition Height="10*"/>
		</Grid.RowDefinitions>
		<Border Grid.Row="0">
			<local:WindowsTitleBar x:Name="title_bar" />
		</Border>
		<Border Grid.Row="1" Background="LightGray">
		</Border>
</Grid>
title_bar.OnPointerMouseHander += TitleBarOnPointerMouseHander;
title_bar.Title = "数据库配置";

private void TitleBarOnPointerMouseHander(object? sender, PointerPressedEventArgs e)
{
    BeginMoveDrag(e);
}

三、创建自定义标题类,以下可供参考

<UserControl xmlns="https://github.com/avaloniaui"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d" d:DesignWidth="300" d:DesignHeight="22"
        x:Class="CodeGenerator.WindowsTitleBar">
	<DockPanel x:Name="sys_bar">
		<StackPanel Orientation="Horizontal"
					DockPanel.Dock="Left"
					Spacing="0">
			<TextBlock
				FontSize="18"
				Foreground="Black"
				VerticalAlignment="Center"
				IsHitTestVisible="False"
				x:Name="sys_title"
				Margin="5 0 0 0"/>
		</StackPanel>
		<StackPanel Height="22"
					HorizontalAlignment="Right"
					Orientation="Horizontal"
					Spacing="0"
					VerticalAlignment="Top">
			<Button Width="46"
					VerticalAlignment="Stretch"
					BorderThickness="0"
					x:Name="btn_close">
				<Button.Resources>
					<CornerRadius x:Key="ControlCornerRadius">0</CornerRadius>
				</Button.Resources>
				<Button.Styles>
					<Style Selector="Button:pointerover /template/ ContentPresenter#PART_ContentPresenter">
						<Setter Property="Background" Value="Red"/>
					</Style>
					<Style Selector="Button:not(:pointerover) /template/ ContentPresenter#PART_ContentPresenter">
						<Setter Property="Background" Value="Transparent"/>
					</Style>
					<Style Selector="Button:pointerover > Path">
						<Setter Property="Fill" Value="White"/>
					</Style>
					<Style Selector="Button:not(:pointerover) > Path">
						<Setter Property="Fill" Value="{DynamicResource SystemControlForegroundBaseHighBrush}"/>
					</Style>
				</Button.Styles>
				<Path Margin="10,1,10,0"
					  Stretch="Uniform"
					  Data="M1169 1024l879 -879l-145 -145l-879 879l-879 -879l-145 145l879 879l-879 879l145 145l879 -879l879 879l145 -145z"></Path>
			</Button>
		</StackPanel>
	</DockPanel>
</UserControl>
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Threading;
using Avalonia.VisualTree;
using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;

namespace CodeGenerator;

public partial class WindowsTitleBar : UserControl
{
    private bool _isShining = false;
    private Window? _parentWindow;
    public static readonly StyledProperty<string> TitleProperty =
        AvaloniaProperty.Register<WindowsTitleBar, string>(nameof(Title));

    /// <summary>
    /// 窗口移动
    /// </summary>
    public event EventHandler<PointerPressedEventArgs>? OnPointerMouseHander;

    public string Title
    {
        get { return GetValue(TitleProperty); }
        set
        {
            SetValue(TitleProperty, value);
            sys_title.Text = value;
        }
    }

    public WindowsTitleBar()
    {
        InitializeComponent();
        Loaded += WindowLoaded;
        PointerPressed += WindowsTitleBarPointerPressed;
        btn_close.Click += CloseWindow;
    }

    private void WindowLoaded(object sender, RoutedEventArgs e)
    {
        var root = this.GetVisualRoot();
        if (root != null && root is Window parentWindow && RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
        {
            _parentWindow = parentWindow;
            var handle = _parentWindow.TryGetPlatformHandle();
            if (handle != null)
            {
                // handle.Handle;
            }
        }
    }

    private void WindowsTitleBarPointerPressed(object? sender, PointerPressedEventArgs e)
    {
        if (e.Pointer.Type == PointerType.Mouse)
        {
            OnPointerMouseHander?.Invoke(sender, e);
        }
    }

    private void CloseWindow(object sender, RoutedEventArgs e)
    {
        if (VisualRoot != null && VisualRoot is Window hostWindow)
        {
            hostWindow.Close();
        }
    }

    private void StartShine()
    {
        if (!_isShining)
        {
            _isShining = true;
            _ = Task.Factory.StartNew(() =>
            {
                try
                {
                    for (int i = 0; i < 4; i++)
                    {
                        Dispatcher.UIThread.InvokeAsync(() =>
                        {
                            sys_bar.Opacity = i % 2 == 0 ? 0.5 : 1;
                        });
                        Thread.Sleep(200);
                    }
                }
                catch (Exception)
                {
                }
                finally
                {
                    _isShining = false;
                }
            });
        }
    }
}