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

如何在WPF中修改应用程序标题栏(非客户区)颜色?

嘿,我太懂你的这种两难了——明知道改标题栏颜色有点“不走常规路线”,又不想用第三方控件花钱,更不想丢了原生窗口那些贴心的功能(比如最小化最大化的过渡效果)。刚好有两个可靠又易实现的WPF原生方案,给你唠唠:

方案一:用WindowChrome自定义标题栏(灵活可控)

WPF自带的WindowChrome类就是干这个的——既能让你自定义标题栏样式,又能完整保留原生窗口的拖动、大小调整、系统按钮功能(包括过渡效果)。

具体步骤:

  1. 在窗口的XAML里配置WindowChrome,关闭默认的玻璃效果,设置标题栏高度和边框厚度;
  2. 自己写一个标题栏的布局,设置背景色,再把系统的最小化/最大化/关闭按钮加回去;
  3. 处理标题栏的拖动事件。

代码示例:

<Window x:Class="WpfCustomTitleBar.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Custom Title Bar Demo" Height="450" Width="800">
    <!-- 配置WindowChrome -->
    <WindowChrome.WindowChrome>
        <WindowChrome 
            CaptionHeight="32"       <!-- 和自定义标题栏高度一致 -->
            ResizeBorderThickness="4"<!-- 保留窗口调整大小的边框 -->
            GlassFrameThickness="0"  <!-- 关闭默认玻璃效果 -->
            CornerRadius="0"/>
    </WindowChrome.WindowChrome>

    <Grid>
        <!-- 自定义标题栏 -->
        <Grid Height="32" VerticalAlignment="Top" Background="#2C3E50" MouseDown="TitleBar_MouseDown">
            <!-- 窗口标题 -->
            <TextBlock Text="我的自定义标题栏" Margin="10,0,0,0" VerticalAlignment="Center" Foreground="White"/>
            <!-- 系统按钮组 -->
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,0,10,0">
                <Button x:Name="MinimizeBtn" Width="25" Height="25" 
                        Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}"
                        Command="{x:Static SystemCommands.MinimizeWindowCommand}">
                    <Path Data="M0,6 L8,6" Stroke="White" StrokeThickness="2"/>
                </Button>
                <Button x:Name="MaximizeBtn" Width="25" Height="25" 
                        Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}"
                        Command="{x:Static SystemCommands.MaximizeWindowCommand}">
                    <Path Data="M0,0 L8,0 L8,8 L0,8 Z" Stroke="White" StrokeThickness="2"/>
                </Button>
                <Button x:Name="CloseBtn" Width="25" Height="25" 
                        Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}"
                        Command="{x:Static SystemCommands.CloseWindowCommand}">
                    <Path Data="M0,0 L8,8 M8,0 L0,8" Stroke="White" StrokeThickness="2"/>
                </Button>
            </StackPanel>
        </Grid>

        <!-- 主内容区域 -->
        <Grid Margin="0,32,0,0">
            <!-- 你的业务内容放在这里 -->
            <TextBlock Text="主内容区域" HorizontalAlignment="Center" VerticalAlignment="Center"/>
        </Grid>
    </Grid>
</Window>

然后在后台代码里处理拖动:

private void TitleBar_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    if (e.LeftButton == System.Windows.Input.MouseButtonState.Pressed)
    {
        DragMove();
    }
}

优势:

  • 完全自定义标题栏的布局、颜色、字体,想怎么改就怎么改;
  • 原生窗口的所有功能(最小化/最大化过渡、拖动、调整大小)都保留;
  • 纯WPF原生实现,不需要任何第三方依赖。
方案二:调用Win32 API直接改标题栏颜色(简单高效)

如果你不想折腾自定义布局,只想单纯改标题栏颜色,那直接调用Windows的DwmSetWindowAttribute API就行——这个方法是直接告诉系统“把这个窗口的标题栏改成我要的颜色”,完全不影响原生窗口的任何功能。

代码示例:

首先在后台代码里导入Win32 API:

using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using System.Drawing;

public partial class MainWindow : Window
{
    // 导入DwmSetWindowAttribute API
    [DllImport("dwmapi.dll")]
    private static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);

    // 标题栏颜色的属性ID(Windows 10 1809+支持)
    private const int DWMWA_CAPTION_COLOR = 35;

    public MainWindow()
    {
        InitializeComponent();
        Loaded += MainWindow_Loaded;
    }

    private void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        // 获取窗口句柄
        var hwnd = new WindowInteropHelper(this).Handle;
        // 设置标题栏颜色(这里用ARGB值,示例是深灰色#2C3E50)
        int color = Color.FromArgb(255, 44, 62, 80).ToArgb();
        // 调用API设置颜色
        DwmSetWindowAttribute(hwnd, DWMWA_CAPTION_COLOR, ref color, sizeof(int));
    }
}

注意事项:

  • 这个API只支持Windows 10 1809及以上版本,如果你需要兼容更早的系统,这个方法就不太适用;
  • 颜色值是ARGB格式,记得用System.Drawing.Color.FromArgb()来生成正确的数值;
  • 完全保留原生标题栏的所有功能,包括过渡效果、系统按钮,不需要自己额外处理。

总结

两个方案都满足你的需求:

  • 想要高度自定义选方案一;
  • 只想快速改颜色选方案二;
  • 都不需要第三方控件,也不会丢失原生窗口的任何功能。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 07:40:59