我可以帮你翻译成中文:在WPF中,我可以设置窗口边框的颜色吗?

17

请问如何设置窗口的样式或颜色?以下是我的 WPF 图像 enter image description here

我想将我的窗口设置成下面这个应用程序的样子:

enter image description here

谢谢。

2个回答

15

据我所记(时间已超过4年),您可以创建一个样式并将其用于 WPF 窗口。

下面是一个示例窗口样式:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
                      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <Style x:Key="MainWindow" 
           TargetType="{x:Type Window}"> 
        <Setter Property="Foreground" 
                Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}" /> 
        <Setter Property="Template"> 
            <Setter.Value> 
                <ControlTemplate TargetType="{x:Type Window}"> 
                    <Grid> 
                        <Border x:Name="MainBorder" 
                                BorderBrush="{DynamicResource MainWindowBorderBrush}" 
                                BorderThickness="1" 
                                CornerRadius="2" 
                                Background="{DynamicResource MainWindowBackgroundBrush}"> 
                            <DockPanel LastChildFill="True"> 
                                <Rectangle x:Name="PART_LEFT_BORDER" 
                                           Width="2" 
                                           Cursor="SizeWE"> 
                                    <Rectangle.Fill> 
                                        <SolidColorBrush Color="Transparent" /> 
                                    </Rectangle.Fill> 
                                </Rectangle> 
                                <Rectangle x:Name="PART_RIGHT_BORDER" 
                                           Cursor="SizeWE" 
                                           Width="2" 
                                           DockPanel.Dock="Right"> 
                                    <Rectangle.Fill> 
                                        <SolidColorBrush Color="Transparent" /> 
                                    </Rectangle.Fill> 
                                </Rectangle> 
                                <Rectangle x:Name="PART_TOP_BORDER" 
                                           Cursor="SizeNS" 
                                           DockPanel.Dock="Top" 
                                           Height="2"> 
                                    <Rectangle.Fill> 
                                        <SolidColorBrush Color="Transparent" /> 
                                    </Rectangle.Fill> 
                                </Rectangle> 
                                <Rectangle x:Name="PART_BOTTOM_BORDER" 
                                           Cursor="SizeNS" 
                                           Height="2" 
                                           DockPanel.Dock="Bottom"> 
                                    <Rectangle.Fill> 
                                        <SolidColorBrush Color="Transparent" /> 
                                    </Rectangle.Fill> 
                                </Rectangle> 
                                <Border x:Name="PART_TITLEBAR" 
                                        Margin="2,0,2,2" 
                                        Height="40" 
                                        DockPanel.Dock="Top" 
                                        CornerRadius="2" 
                                        Background="Transparent"> 
                                    <DockPanel LastChildFill="False"> 
                                        <TextBlock Margin="8,0,0,4" 
                                                   VerticalAlignment="Center" 
                                                   FontStretch="UltraExpanded" 
                                                   Foreground="Black" 
                                                   TextTrimming="CharacterEllipsis" 
                                                   TextWrapping="NoWrap" 
                                                   Text="{TemplateBinding Title}" 
                                                   FontSize="16" /> 
                                        <Button x:Name="PART_CLOSE" 
                                                DockPanel.Dock="Right" 
                                                Style="{DynamicResource FlatButton}" 
                                                VerticalAlignment="Center" 
                                                Margin="0,0,4,0"> 
                                            <Image Source="/MixModes.Synergy.Resources;
                        component/Resources/Close.png" 
                                                   Stretch="None" 
                                                   Margin="4" /> 
                                        </Button> 
                                        <Button x:Name="PART_MAXIMIZE_RESTORE" 
                                                DockPanel.Dock="Right" 
                                                HorizontalAlignment="Center" 
                                                VerticalAlignment="Center" 
                                                Style="{DynamicResource FlatButton}"> 
                                            <Image x:Name="MaximizeRestoreImage" 
                                                   Source="/MixModes.Synergy.Resources;
                        component/Resources/Restore.png" 
                                                   Stretch="None" 
                                                   Margin="4" /> 
                                        </Button> 
                                        <Button x:Name="PART_MINIMIZE" 
                                                HorizontalAlignment="Center" 
                                                Style="{DynamicResource FlatButton}" 
                                                VerticalAlignment="Center" 
                                                DockPanel.Dock="Right"> 
                                            <Image Margin="4" 
                                                   Source="/MixModes.Synergy.
                        Resources;component/Resources/
                        Minimize.png" 
                                                   Stretch="None" /> 
                                        </Button> 
                                    </DockPanel> 
                                </Border>

                                <!-- Title bar separator--> 
                                <Border Height="1" 
                                        DockPanel.Dock="Top" 
                                        Background="{DynamicResource 
                    MainWindowTitleBarSeparator}" />

                                <!-- Actual Window Content --> 
                                <AdornerDecorator DockPanel.Dock="Bottom"> 
                                    <ContentPresenter /> 
                                </AdornerDecorator> 
                            </DockPanel> 
                        </Border> 
                    </Grid> 
                    <ControlTemplate.Triggers>                        
                        <DataTrigger Binding="{Binding RelativeSource=
                {RelativeSource Self}, Path=Maximized}" 
                                     Value="False"> 
                            <Setter TargetName="MaximizeRestoreImage" 
                                    Property="Source" 
                                    Value="/MixModes.Synergy.Resources;
                component/Resources/Maximize.png" /> 
                        </DataTrigger> 
                    </ControlTemplate.Triggers> 
                </ControlTemplate> 
            </Setter.Value> 
        </Setter> 
    </Style> 
</ResourceDictionary>

结果将会是这样:

http://www.codeproject.com/KB/WPF/CustomWPFWindow/8.png

了解更多信息请访问使用轻松的方式在WPF中创建自定义窗口


8
您可以使用下面的XAML代码在WPF中实现圆角窗口,但这种方法的问题是您必须为最小化、最大化和关闭实现自己的按钮。
幸运的是,在code plex上有一个库可以为您完成所有这些工作。
<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    AllowsTransparency="True"
    WindowStyle="None"
    Padding="0"
    WindowStartupLocation="CenterScreen" Height="200" Width="200" >
    <Border BorderBrush="Blue" BorderThickness="2" CornerRadius="6" >
    </Border>
</Window>

1
这个库似乎不太好用。我建议使用WPF自定义Chrome库。这样,您可以在使用“完全自定义Chrome”窗口时保留GPU加速。 - Erti-Chris Eelmaa

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接