Closed Erapchu closed 3 years ago
`<Window x:Class="WindowChromeExample.MainWindow" 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:WindowChromeExample" mc:Ignorable="d" Title="MainWindow" Height="200" Width="500">
<!--Add the WindowChrome object--> <WindowChrome.WindowChrome> <WindowChrome CaptionHeight="34" /> </WindowChrome.WindowChrome> <Window.Resources> <ResourceDictionary> <!--Base style for title bar buttons--> <Style x:Key="CaptionButtonStyle" TargetType="Button"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <Grid x:Name="LayoutRoot" Background="Transparent" Width="44" Height="30"> <TextBlock x:Name="txt" Text="{TemplateBinding Content}" FontFamily="Segoe MDL2 Assets" FontSize="10" Foreground="Black" HorizontalAlignment="Center" VerticalAlignment="Center" RenderOptions.ClearTypeHint="Auto" TextOptions.TextRenderingMode="Aliased" TextOptions.TextFormattingMode="Display"/> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="LayoutRoot" Property="Background" Value="#E5E5E5"/> <Setter TargetName="txt" Property="Foreground" Value="#000000"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> <!--Minimize--> <Style x:Key="MinimizeButtonStyle" TargetType="Button" BasedOn="{StaticResource CaptionButtonStyle}"> <Setter Property="Content" Value=""/> </Style> <!--Maximize--> <Style x:Key="MaximizeButtonStyle" TargetType="Button" BasedOn="{StaticResource CaptionButtonStyle}"> <Setter Property="Content" Value=""/> </Style> <!--Restore--> <Style x:Key="RestoreButtonStyle" TargetType="Button" BasedOn="{StaticResource CaptionButtonStyle}"> <Setter Property="Content" Value=""/> </Style> <!--Close--> <Style x:Key="CloseButtonStyle" TargetType="Button" BasedOn="{StaticResource CaptionButtonStyle}"> <Setter Property="Content" Value=""/> </Style> </ResourceDictionary> </Window.Resources> <!--Title bar button commands--> <Window.CommandBindings> <CommandBinding Command="{x:Static SystemCommands.CloseWindowCommand}" CanExecute="CommandBinding_CanExecute" Executed="CommandBinding_Executed_Close" /> <CommandBinding Command="{x:Static SystemCommands.MaximizeWindowCommand}" CanExecute="CommandBinding_CanExecute" Executed="CommandBinding_Executed_Maximize" /> <CommandBinding Command="{x:Static SystemCommands.MinimizeWindowCommand}" CanExecute="CommandBinding_CanExecute" Executed="CommandBinding_Executed_Minimize" /> <CommandBinding Command="{x:Static SystemCommands.RestoreWindowCommand}" CanExecute="CommandBinding_CanExecute" Executed="CommandBinding_Executed_Restore" /> </Window.CommandBindings> <Border x:Name="MainWindowBorder" BorderBrush="LightCoral" BorderThickness="0" > <Grid x:Name="parentContainer" Background="LightBlue"> <Grid.RowDefinitions> <RowDefinition Height ="Auto"/> <RowDefinition Height ="*"/> </Grid.RowDefinitions> <!--Window chrome--> <Grid Grid.Row="0" Height="30" Background="#F999"> <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Center"> <!--App icon--> <Image Source="/Resources/watermelon.ico" Width="18" Margin="2" HorizontalAlignment="Left" VerticalAlignment="Center" /> <TextBlock Text="Sweet App" FontFamily="Arial" Margin="4 3 0 0" /> </StackPanel> <!--Caption buttons--> <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" > <Button Style="{StaticResource MinimizeButtonStyle}" WindowChrome.IsHitTestVisibleInChrome="True" ToolTip="Minimize" Command="{x:Static SystemCommands.MinimizeWindowCommand}"/> <Button x:Name="RestoreButton" Visibility="Collapsed" Style="{StaticResource RestoreButtonStyle}" Command="{x:Static SystemCommands.RestoreWindowCommand}" WindowChrome.IsHitTestVisibleInChrome="True" ToolTip="Restore"/> <Button x:Name="MaximizeButton" Visibility="Visible" Style="{StaticResource MaximizeButtonStyle}" Command="{x:Static SystemCommands.MaximizeWindowCommand}" WindowChrome.IsHitTestVisibleInChrome="True" ToolTip="Maximize" /> <Button Style="{StaticResource CloseButtonStyle}" WindowChrome.IsHitTestVisibleInChrome="True" ToolTip="Close" Command="{x:Static SystemCommands.CloseWindowCommand}"/> </StackPanel> </Grid> <!--App content--> <Grid Grid.Row="1" x:Name="AppArea"> <Path Data="M50,0L100,50 50,100 0,50z" Fill="White" Stretch="Fill" Stroke="Black" StrokeThickness="2" /> </Grid> </Grid> </Border>
using System; using System.Windows; using System.Windows.Input;
namespace WindowChromeExample { /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); StateChanged += MainWindowStateChangeRaised; }
// Can execute private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e) { e.CanExecute = true; } // Minimize private void CommandBinding_Executed_Minimize(object sender, ExecutedRoutedEventArgs e) { SystemCommands.MinimizeWindow(this); } // Maximize private void CommandBinding_Executed_Maximize(object sender, ExecutedRoutedEventArgs e) { SystemCommands.MaximizeWindow(this); } // Restore private void CommandBinding_Executed_Restore(object sender, ExecutedRoutedEventArgs e) { SystemCommands.RestoreWindow(this); } // Close private void CommandBinding_Executed_Close(object sender, ExecutedRoutedEventArgs e) { SystemCommands.CloseWindow(this); } // State change private void MainWindowStateChangeRaised(object sender, EventArgs e) { if (WindowState == WindowState.Maximized) { MainWindowBorder.BorderThickness = new Thickness(8); RestoreButton.Visibility = Visibility.Visible; MaximizeButton.Visibility = Visibility.Collapsed; } else { MainWindowBorder.BorderThickness = new Thickness(0); RestoreButton.Visibility = Visibility.Collapsed; MaximizeButton.Visibility = Visibility.Visible; } } }
} `
`<Window x:Class="WindowChromeExample.MainWindow" 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:WindowChromeExample" mc:Ignorable="d" Title="MainWindow" Height="200" Width="500">
namespace WindowChromeExample { ///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
StateChanged += MainWindowStateChangeRaised;
}
} `