WPF制作Windows 10关闭按钮在自定义标题栏中

发布于 2025-02-09 22:48:42 字数 667 浏览 1 评论 0 原文

我需要为我的WPF应用程序构建一个自定义标题栏,仅使用“关闭”按钮,目前看起来像这样:

“在此处输入图像说明”

这是代码:

<Grid Width="32" Height="25" Margin="1" HorizontalAlignment="Right" MouseLeftButtonUp="OnCloseWindow" Grid.Column="4">
    <Rectangle Fill="#FFE81123" Height="25" VerticalAlignment="Bottom"/>
    <TextBlock Text="╳" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="#DDFFFFFF" />
</Grid>

我想要的是默认Windows 10关闭按钮的复制品动画,当您停止徘徊时,按钮会逐渐变成悬停的红色和白色。

我该怎么做?

I need to build a custom title bar for my WPF application with only the close button, which currently looks like this:

enter image description here

This is the code:

<Grid Width="32" Height="25" Margin="1" HorizontalAlignment="Right" MouseLeftButtonUp="OnCloseWindow" Grid.Column="4">
    <Rectangle Fill="#FFE81123" Height="25" VerticalAlignment="Bottom"/>
    <TextBlock Text="╳" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="#DDFFFFFF" />
</Grid>

What I wanted is a replica of the default Windows 10 close button animation, the button gradually turning red on hover and white again when you stop hovering.

How can I accomplish this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

睫毛溺水了 2025-02-16 22:48:42

您可以通过

来添加 windowStyle =“ toolwindow”

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        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"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" WindowStyle="ToolWindow">

其中仅显示关闭按钮

来实现它。如果您想要与普通窗口相同的样式,则需要创建关闭按钮的样式。

检查此链接以获取参考

You can achieve it by adding

WindowStyle="ToolWindow"

in

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        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"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" WindowStyle="ToolWindow">

It will show the close button only.

If you want the same style as the normal window, You need to create the style of the close button.

Check this link for reference

https://www.codemag.com/article/1903031/Create-a-Title-Bar-for-User-Controls

海之角 2025-02-16 22:48:42

这是我使用的...

样式:

<Style TargetType="{x:Type Button}" x:Key="WindowControlRed">
        <Setter Property="IsTabStop" Value="False"/>
        <Setter Property="Opacity" Value="1"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}" >
                    <Border Background="{TemplateBinding Background}" BorderBrush="Transparent" BorderThickness="0">
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="IndianRed"/>
            </Trigger>
            <Trigger Property="IsPressed" Value="True">
                <Setter Property="Background" Value="Red"/>
            </Trigger>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Opacity" Value="0.25"/>
            </Trigger>
        </Style.Triggers>
    </Style>

XAML中的按钮:

<Button Content="" FontFamily="Segoe MDL2 Assets" Height="40" FontSize="12" Width="40" Style="{StaticResource WindowControlRed}" Click="Close_Click"/>

背后的代码:

private void Close_Click(object sender, RoutedEventArgs e)
{
    Close();
}

This is what I use...

Style:

<Style TargetType="{x:Type Button}" x:Key="WindowControlRed">
        <Setter Property="IsTabStop" Value="False"/>
        <Setter Property="Opacity" Value="1"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}" >
                    <Border Background="{TemplateBinding Background}" BorderBrush="Transparent" BorderThickness="0">
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="IndianRed"/>
            </Trigger>
            <Trigger Property="IsPressed" Value="True">
                <Setter Property="Background" Value="Red"/>
            </Trigger>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Opacity" Value="0.25"/>
            </Trigger>
        </Style.Triggers>
    </Style>

Button in XAML:

<Button Content="" FontFamily="Segoe MDL2 Assets" Height="40" FontSize="12" Width="40" Style="{StaticResource WindowControlRed}" Click="Close_Click"/>

Code behind:

private void Close_Click(object sender, RoutedEventArgs e)
{
    Close();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文