You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Avalonia窗口控制按钮与标题栏定制需求及实现求助

Avalonia窗口定制解决方案

以下是满足你三个需求的完整修改代码及说明:

修改后的MainWindow.axaml代码

<Window 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="800" d:DesignHeight="450"
        x:Class="AvaloniaAppTest.MainWindow"
        Height="600"
        Width="900"
        Icon="/assets/icon.ico"
        Title="AppTest"
        <!-- 禁用窗口大小调整,隐藏最大化按钮 -->
        ResizeMode="CanMinimize"
        <!-- 开启自定义标题栏必备属性 -->
        ExtendClientAreaToDecorationsHint="True"
        ExtendClientAreaChromeHints="NoChrome"
        ExtendClientAreaTitleBarHeightHint="-1"
        <!-- 窗口黑色边框 -->
        BorderBrush="Black"
        BorderThickness="2">

    <Window.Background>
        <ImageBrush Source="/assets/background.png"/>
    </Window.Background>

    <!-- 整体布局:标题栏 + 内容区域 -->
    <DockPanel>
        <!-- 自定义标题栏 -->
        <Grid DockPanel.Dock="Top" Height="32" DragMove="True">
            <!-- 标题栏自定义背景图 -->
            <Grid.Background>
                <ImageBrush Source="/assets/titlebar-bg.png" Stretch="Fill"/>
            </Grid.Background>

            <!-- 窗口控制按钮 -->
            <StackPanel HorizontalAlignment="Right" Orientation="Horizontal">
                <!-- 最小化按钮 -->
                <Button Width="32" Height="32" Margin="0" Padding="0"
                        Command="{Binding MinimizeCommand}"
                        Style="{StaticResource CustomWindowButtonStyle}">
                    <Path Data="M0,8 L16,8" Stroke="White" StrokeThickness="2"/>
                </Button>
                <!-- 关闭按钮 -->
                <Button Width="32" Height="32" Margin="0" Padding="0"
                        Command="{Binding CloseCommand}"
                        Style="{StaticResource CustomWindowButtonStyle}">
                    <Path Data="M0,0 L16,16 M16,0 L0,16" Stroke="White" StrokeThickness="2"/>
                </Button>
            </StackPanel>
        </Grid>

        <!-- 原有内容区域 -->
        <Grid>
            <Grid.Background>
                <ImageBrush Stretch="UniformToFill" Source="/assets/background.png"/>
            </Grid.Background>              
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            
            <!-- Left Side -->
            <StackPanel Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center">
                <TextBlock Text="Left Click" FontWeight="Bold" FontSize="20" Foreground="White"/>
                <Button x:Name="leftStart" Content="Start" Margin="5"  Background="Yellow"/>
                <Button x:Name="leftStop" Content="Stop" Margin="5"  Background="Yellow"/>
                <Button x:Name="leftHotkey" Content="Hotkey" Margin="5"  Background="Yellow"/>
            </StackPanel>
            
            <!-- Right Side -->
            <StackPanel Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center">
                <TextBlock Text="Right Click" FontWeight="Bold" FontSize="20" Foreground="White"/>
                <Button Content="Start" Margin="5"  Background="Yellow"/>
                <Button Content="Stop" Margin="5"  Background="Yellow"/>
                <Button Content="Hotkey" Margin="5"  Background="Yellow"/>
            </StackPanel>
        </Grid>
    </DockPanel>
</Window>

配套的按钮样式(添加到App.axaml或Window的Resources中)

<Application.Styles>
    <FluentTheme Mode="Dark"/>
    <Style x:Key="CustomWindowButtonStyle" TargetType="Button">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="Template">
            <ControlTemplate TargetType="Button">
                <Border Background="{TemplateBinding Background}"
                        CornerRadius="0">
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsPointerOver" Value="True">
                        <Setter Property="Background" Value="#33FFFFFF"/>
                    </Trigger>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter Property="Background" Value="#66FFFFFF"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter>
    </Style>
</Application.Styles>

MainWindow.axaml.cs中添加命令实现

using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using ReactiveUI;

namespace AvaloniaAppTest;

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        // 绑定命令
        this.WhenAnyValue(x => x.MinimizeCommand).Subscribe(_ => WindowState = WindowState.Minimized);
        this.WhenAnyValue(x => x.CloseCommand).Subscribe(_ => Close());
    }

    public ReactiveCommand<object?, object?> MinimizeCommand { get; } = ReactiveCommand.Create(() => { });
    public ReactiveCommand<object?, object?> CloseCommand { get; } = ReactiveCommand.Create(() => { });
}

各需求实现说明

  • 移除窗口大小调整及最大化按钮:设置ResizeMode="CanMinimize"后,窗口无法调整大小,系统默认的最大化按钮会被隐藏,仅保留最小化和关闭按钮。
  • 修改最小化/关闭按钮样式:通过自定义CustomWindowButtonStyle样式去除按钮默认边框和背景,添加hover/pressed状态效果;用Path绘制极简图标,并通过ReactiveCommand实现按钮的最小化、关闭功能。
  • 标题栏自定义背景图+窗口黑边框:
    1. 窗口层面设置BorderBrush="Black"和BorderThickness="2"直接添加黑色边框;
    2. 开启ExtendClientAreaToDecorationsHint="True"和ExtendClientAreaChromeHints="NoChrome"隐藏系统标题栏,自定义标题栏Grid并设置ImageBrush作为背景;
    3. 给标题栏Grid添加DragMove="True"事件,实现窗口拖动功能。

内容的提问来源于stack exchange,提问作者JuninOP

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 22:15:26