添加按钮到窗口边框

本文关键字:边框 窗口 按钮 添加 | 更新日期: 2023-09-27 18:10:22

我有一个属性为WindowStyle="SingleBorderWindow"的WPF窗口。现在我想知道如何在窗口边界的左上角设置一个按钮。

添加按钮到窗口边框

您可能需要使用这里描述的window chrome库

我认为这是这样你可以重新创建最小化/最大化/关闭按钮。

您有两个选择,使用GridDockPanel。我在下面包含了一个Grid的样本。

<Grid>
     <Grid.RowDefinitions>
         <RowDefinition Height="Auto" />
         <RowDefinition Height="*" />
     <Grid.RowDefinitions>
     <StackPanel Orientation="Horizontal" Margin="2,0" HorizontalAlignment="Right" VerticalAlignment="Top">
     </StackPanel>
</Grid>

具有RowDefinition Height="Auto"StackPanel Orientation="Horizontal"右对齐的网格。


然而

如果你想做一个真正的无边框窗口,你将有更多的工作要做,而不是简单地将WindowStyle设置为None

我遇到的一个主要障碍是,当WindowStyleNone时,它将不尊重任务栏(即重叠它),您将需要挂钩到消息泵以设置正确的窗口约束。

如果你想知道如何做到这一点,请在评论中告诉我,我会很高兴地发布示例代码。

您需要通过设置透明度,背景和样式来创建自定义窗口,如下所示:

<Window x:Class="WpfApplication2.Window2" 
            Name="Window2xx" 
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
        Title="window1"
         xmlns:m="clr-namespace:WpfApplication2"
       AllowsTransparency="True" WindowStyle="None" 
       WindowStartupLocation="CenterOwner" d:DesignWidth="410" StateChanged="Window_StateChanged" 
        SizeToContent="WidthAndHeight" ShowInTaskbar="False" Background="Transparent">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Style.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
    <Grid>
        <m:MasterWindow Width="400">
            <m:MasterWindow.WindowTitle>
                <ContentPresenter Content="window1"  MouseLeftButtonDown="Window_MouseLeftButtonDown"></ContentPresenter>
            </m:MasterWindow.WindowTitle>
            <m:MasterWindow.Content>
                <Grid>
                </Grid>
            </m:MasterWindow.Content>
        </m:MasterWindow>
    </Grid>
</Window> 

window2背后的代码:

namespace WpfApplication2
{
    public partial class Window2 : Window
    {
        public Window2()
        {
            InitializeComponent();
        }
        private void Window_StateChanged(object sender, EventArgs e)
        {
            if (((Window)sender).WindowState == WindowState.Maximized)
                ((Window)sender).WindowState = WindowState.Normal;
        }
        private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                DragMove();
            }
            catch ()
            {}
        }
}
    }

主窗口如下:

namespace WpfApplication2
{
    public class MasterWindow : ContentControl
        {
            public static RoutedCommand CloseWindowCommand;
            static MasterWindow()
            {
                DefaultStyleKeyProperty.OverrideMetadata(typeof(MasterWindow), new FrameworkPropertyMetadata(typeof(MasterWindow)));
                CloseWindowCommand = new RoutedCommand("CloseWindow", typeof(MasterWindow));
                CommandManager.RegisterClassCommandBinding(typeof(MasterWindow), new CommandBinding(CloseWindowCommand, CloseWindowEvent));
            }

            public static readonly DependencyProperty WindowTitleProperty = DependencyProperty.Register("WindowTitle", typeof(object), typeof(MasterWindow), new UIPropertyMetadata());
            public object WindowTitle
            {
                get { return (object)GetValue(WindowTitleProperty); }
                set { SetValue(WindowTitleProperty, value); }
            }
            private static void CloseWindowEvent(object sender, ExecutedRoutedEventArgs e)
            {
                MasterWindow control = sender as MasterWindow;
                if (control != null)
                {
                    Window objWindow = Window.GetWindow(((FrameworkElement)sender));
                    if (objWindow != null)
                    {
                        if (objWindow.Name.ToLower() != "unlockscreenwindow")
                        {
                            Window.GetWindow(((FrameworkElement)sender)).Close();
                        }
                    }
                }
            }
        }  
}

主窗口样式:

 <ResourceDictionary  
    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"  
    mc:Ignorable="d" 
    xmlns:local="clr-namespace:WpfApplication2" 
    x:Class="MasterWindow">
    <Style x:Key="WindowTitle" TargetType="ContentPresenter">
        <Setter Property="Control.FontFamily" Value="Segoe UI"></Setter>
        <Setter Property="Control.FontSize" Value="14"></Setter>
        <Setter Property="Control.FontWeight" Value="SemiBold"></Setter>
        <Setter Property="Control.Foreground" Value="White"></Setter>
        <Setter Property="Control.VerticalAlignment" Value="Top"></Setter>
        <Setter Property="Control.HorizontalAlignment" Value="Left"></Setter>
        <Setter Property="Control.VerticalContentAlignment" Value="Top"></Setter>
    </Style>
    <Style x:Key="closebutton" BasedOn="{x:Null}" TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid>
                        <Rectangle 
                                    Opacity="1" 
                                    RadiusX="2" 
                                    RadiusY="2" 
                                    Stroke="#ffffff" 
                                    StrokeThickness="1">
                            <Rectangle.Fill>
                                <LinearGradientBrush 
                                            StartPoint="0.6190476190476191,-0.5" 
                                            EndPoint="1.1128888811383928,1.426776123046875">
                                    <LinearGradientBrush.GradientStops>
                                        <GradientStopCollection>
                                            <GradientStop 
                                                        Color="#2E4C87" 
                                                        Offset="0" />
                                            <GradientStop 
                                                        Color="#FFffffff" 
                                                        Offset="1" />
                                        </GradientStopCollection>
                                    </LinearGradientBrush.GradientStops>
                                </LinearGradientBrush>
                            </Rectangle.Fill>
                        </Rectangle>
                        <Path 
                                    Margin="5,5,5,5" 
                                    Stretch="Fill" 
                                    Opacity="1" 
                                    Data="M 808.8311767578125,278.7662353515625 C808.8311767578125,278.7662353515625 820,268 820,268 " 
                                    Stroke="#ffffff" 
                                    StrokeThickness="2" />
                        <Path 
                                    Margin="5,5,5,5" 
                                    Stretch="Fill" 
                                    Opacity="1" 
                                    Data="M 809.4155883789062,268.3636474609375 C809.4155883789062,268.3636474609375 820,279 820,279 " 
                                    Stroke="#ffffff" 
                                    StrokeThickness="2" />
                        <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" RecognizesAccessKey="True"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsFocused" Value="True"/>
                        <Trigger Property="IsDefaulted" Value="True"/>
                        <Trigger Property="IsMouseOver" Value="True"/>
                        <Trigger Property="IsPressed" Value="True"/>
                        <Trigger Property="IsEnabled" Value="False"/>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style TargetType="{x:Type local:MasterWindow}">
        <Setter Property="IsTabStop" Value="False" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:MasterWindow}">
                    <StackPanel>
                        <Border x:Name="border" Background="WhiteSmoke" BorderBrush="#2E4C87" BorderThickness="7" CornerRadius="8,8,8,8" >
                            <Border.BitmapEffect>
                                <DropShadowBitmapEffect Color="Black" Direction="320" Opacity="0.75" ShadowDepth="8"></DropShadowBitmapEffect>
                            </Border.BitmapEffect>
                            <Border.RenderTransform>
                                <TransformGroup>
                                    <ScaleTransform ScaleX="0.95" ScaleY="0.95"/>
                                    <SkewTransform AngleX="0" AngleY="0"/>
                                    <RotateTransform Angle="0"/>
                                    <TranslateTransform X="0" Y="0"/>
                                </TransformGroup>
                            </Border.RenderTransform>
                            <Border.Triggers>
                                <EventTrigger RoutedEvent="Border.Loaded">
                                    <BeginStoryboard>
                                        <Storyboard>
                                            <DoubleAnimation Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)" From="0" To="0.96" Duration="0:0:0.6"/>
                                            <DoubleAnimation Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" From="0" To="0.96" Duration="0:0:0.6"/>
                                        </Storyboard>
                                    </BeginStoryboard>
                                </EventTrigger>
                            </Border.Triggers>
                            <Grid HorizontalAlignment="Stretch" >
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="32"></RowDefinition>
                                    <RowDefinition Height="*" />
                                </Grid.RowDefinitions>
                                <Border BorderThickness="5,5,3,0"  Background="#2E4C87" BorderBrush="#2E4C87" CornerRadius="5,4,0,0" VerticalAlignment="Top" Height="32" Margin="-6,-6,-4,0" >
                                    <Grid Background="Transparent" HorizontalAlignment="Stretch" Height="32" VerticalAlignment="Center"  >
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="30"/>
                                        </Grid.ColumnDefinitions>
                                        <ContentPresenter Content="{TemplateBinding WindowTitle}" Margin="0,4,0,0" HorizontalAlignment="Stretch"  Style="{StaticResource WindowTitle}" />
                                        <Button Name="btnClose" Style="{StaticResource closebutton}" Cursor="Hand" Command="{x:Static local:MasterWindow.CloseWindowCommand}" VerticalAlignment="Top" Height="18" HorizontalAlignment="Right" Width="18" Margin="0,5,8,0" Grid.Column="1" />
                                    </Grid>
                                </Border>
                                <Border BorderBrush="Transparent" BorderThickness="7,0,7,7" VerticalAlignment="Top" HorizontalAlignment="Left" CornerRadius="0,0,10,10" Grid.Row="1"></Border>
                                <ContentPresenter Grid.Row="1" Content="{TemplateBinding Content}" VerticalAlignment="Top" Margin="0,-6,0,0"/>
                            </Grid>
                        </Border>
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>