Windows 10桌面应用程序上的自定义标题栏颜色

时间:2018-07-09 19:00:30

标签: .net wpf winforms windows-10 windows-10-desktop

我有一个混合WinForms / WPF桌面应用程序运行在Windows 10上,但仍以.NET 3.5为目标。它具有白天模式,黄昏模式和夜间模式。在夜间模式下,基本上所有东西都变成黑色的红色,但标题栏仍默认为白色。

在“设置”>“个性化设置”>“颜色”>“在以下表面上显示强调色”,您可以选中“标题栏”并获取要用于标题栏的强调色,然后选择深色。但是,并非我们所有的用户都知道此设置。

是否有一种简单的方法可以以编程方式更改从应用程序创建的所有Windows的标题栏背景和前景色?我可以根据需要使用p / Invoke。

如果没有,我也将接受一种以编程方式全局更改标题栏颜色的方法(系统上的所有Windows),甚至可以提供一种自动检查该框(标题栏上的重点颜色)并设置为深色的方法强调颜色。

-

编辑:可以不必设置WindowStyle =“ None”并自定义实现标题栏吗?那就是我已经(疲倦)在做的事情。

1 个答案:

答案 0 :(得分:3)

在WPF(我假设使用WinForms)中,创建无边界窗口和自己创建标题栏更加容易。这是执行此操作的WPF代码,

首先使用C#:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    public MainWindow()
    {
        InitializeComponent();            
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void Notify(params string[] PropertyNames)
    {
        foreach (string PropertyName in PropertyNames)
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName));
    }

    public Visibility CanMaximize { get { return WindowState == WindowState.Normal ? Visibility.Visible : Visibility.Collapsed;  } }
    public Visibility CanRestore { get { return WindowState == WindowState.Maximized ? Visibility.Visible : Visibility.Collapsed; } }

    private void CloseWindow(object sender, ExecutedRoutedEventArgs e)
    {
        Application.Current.Shutdown();
    }

    private void CanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        e.CanExecute = true;            
    }

    private void MinimizeWindow(object sender, ExecutedRoutedEventArgs e)
    {
        this.WindowState = WindowState.Minimized;            
    }

    private void MaximizeWindow(object sender, ExecutedRoutedEventArgs e)
    {
        this.WindowState = WindowState.Maximized;
        Notify("CanMaximize", "CanRestore");
    }

    private void RestoreWindow(object sender, ExecutedRoutedEventArgs e)
    {
        this.WindowState = WindowState.Normal;
        Notify("CanMaximize", "CanRestore");
    }

    private void TitleBar_MouseMove(object sender, MouseEventArgs e)
    {
        if (Mouse.LeftButton == MouseButtonState.Pressed)            
        {
            if (TitleBar.IsMouseDirectlyOver)
                DragMove();
        }
    }

}

现在的XAML:

<Window x:Class="WpfApp1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    FontFamily="Segoe UI Symbol" 
    mc:Ignorable="d" x:Name="root"
    Title="MainWindow" Height="450" Width="800" WindowStyle="None">
<Window.CommandBindings>
    <CommandBinding Command="SystemCommands.CloseWindowCommand" Executed="CloseWindow" CanExecute="CanExecute" />
    <CommandBinding Command="SystemCommands.MinimizeWindowCommand" Executed="MinimizeWindow"  CanExecute="CanExecute" />
    <CommandBinding Command="SystemCommands.MaximizeWindowCommand" Executed="MaximizeWindow"  CanExecute="CanExecute" />
    <CommandBinding Command="SystemCommands.RestoreWindowCommand" Executed="RestoreWindow"  CanExecute="CanExecute" />
</Window.CommandBindings>
<DockPanel>
    <DockPanel DockPanel.Dock="Top" Background="#FF000020" x:Name="TitleBar" MouseMove="TitleBar_MouseMove">
        <Image DockPanel.Dock="Left" Source="{Binding ElementName=root, Path=Icon}" Stretch="Uniform" Height="25" Width="25" Margin="2,0"/>
        <Button Command="SystemCommands.CloseWindowCommand" DockPanel.Dock="Right" Margin="0" Padding="0">
            <Grid Width="25" Height="25" Background="{Binding ElementName=TitleBar, Path=Background}">
                <TextBlock  Text="&#xE10A;" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="White" />
            </Grid>
        </Button>
        <Button Command="SystemCommands.RestoreWindowCommand" DockPanel.Dock="Right" Visibility="{Binding ElementName=root, Path=CanRestore}">
            <Grid Width="25" Height="25" Background="{Binding ElementName=TitleBar, Path=Background}">
                <TextBlock  Text="&#xE158;" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="White" />
            </Grid>
        </Button>
        <Button Command="SystemCommands.MaximizeWindowCommand" DockPanel.Dock="Right" Visibility="{Binding ElementName=root, Path=CanMaximize}">
            <Grid Width="25" Height="25" Background="{Binding ElementName=TitleBar, Path=Background}">
                <TextBlock  Text="&#x10c3f;" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="White" />
            </Grid>
        </Button>
        <Button Command="SystemCommands.MinimizeWindowCommand" DockPanel.Dock="Right">
            <Grid Width="25" Height="25" Background="{Binding ElementName=TitleBar, Path=Background}">
                <TextBlock  Text="&#xE108;" HorizontalAlignment="Center" VerticalAlignment="Bottom" Foreground="White" />
            </Grid>
        </Button>
        <TextBlock Text="{Binding ElementName=root, Path=Title, FallbackValue='Application Title'}" Foreground="White" VerticalAlignment="Center"/>
    </DockPanel>
    <Grid>
        <TextBlock Text="Content goes here." VerticalAlignment="Center" HorizontalAlignment="Center"/>
    </Grid>
</DockPanel>