[WPF 学习] 9.自定义一个Window的样子

基本思路是把原来的WindowStyle设置为None,然后自己弄一个标题栏

一、xmal

<Window x:Class="YKCore.WindowSetup" Name="WinMain"        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:YKCore"        mc:Ignorable="d"        WindowState="Maximized"        WindowStyle="None" SizeChanged="WinMain_SizeChanged"        Height="450" Width="800" BorderThickness="0" Background="{DynamicResource BackgroundColor}"    xmlns:theme="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero2">           <!-- 最大化按钮形状 -->        <PathGeometry x:Key="pathMaximize">            <PathGeometry.Figures>                M1,1  L1 ,11 L11,11 L11,1 z M0,0 L12,0 L12,12 L0,12 z            </PathGeometry.Figures>        </PathGeometry>        <!-- 还原按钮形状 -->        <PathGeometry x:Key="pathRestore">            <PathGeometry.Figures>                M1,3 L1,11 L9,11 L9,3 z M3,1 L3,2 L10,2 L10,9 L11,9 L11,1 z M2 ,0 L12,0 L12,10 L10,10 L10,12 L0,12 L0,2 L2 ,2 z            </PathGeometry.Figures>        </PathGeometry>    </Window.Resources>    <Grid>        <Grid.RowDefinitions>            <RowDefinition Height="50"></RowDefinition>            <RowDefinition Height="*"></RowDefinition>        </Grid.RowDefinitions>        <Border BorderBrush="{DynamicResource ForeColor}" BorderThickness="2,2,2,0" Padding="10,0">            <Grid><!--自己画个图标,也就是一个圆角矩形里面写了个字母M-->                <Rectangle Width="30" Height="28" Stroke="Yellow" StrokeThickness="1" HorizontalAlignment="Left" RadiusX="5" RadiusY="5" />                <TextBlock Text="M" Foreground="Yellow" Margin="10,0,0,0">                    <TextBlock.RenderTransform>                        <SkewTransform AngleX="-15"/>                    </TextBlock.RenderTransform>                </TextBlock>                <StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Right">                    <Button x:Name="BtnMin"  Width="28" Height="28"   Click="BtnMin_Click" >                        <Path  Data="M0,5 L12,5 L12,6 L0,6 z" Fill="{DynamicResource ForeColor}" Width="12" Height="12"/>                    </Button>                    <Button x:Name="BtnMax"  Width="28" Height="28"  Click="BtnMax_Click">                        <Path x:Name="PMax" Fill="{DynamicResource ForeColor}" Width="12" Height="12"/>                    </Button>                    <Button x:Name="BtnClose"  Width="28" Height="28"  Click="BtnClose_Click" >                        <Button.Content>                            <Path Data="M1,0 L6,5 L11,0 L12,1 L7,6 L12,11 L11,12 L6,7 L1,12 L0,11 L5,6 L0,1 z" Fill="{DynamicResource ForeColor}" Width="12" Height="12"/>                        </Button.Content>                    </Button>                </StackPanel>            </Grid>        </Border>        <Border Grid.Row="1" BorderBrush="{DynamicResource ForeColor}" BorderThickness="2" Padding="10"><!--原来的Window的Content-->        </Border>    </Grid></Window>

二、后台代码(几个事件)

        private void BtnClose_Click(object sender, RoutedEventArgs e)        {            Application.Current.Shutdown();        }        private void BtnMax_Click(object sender, RoutedEventArgs e)        {            this.WindowState = this.WindowState != WindowState.Maximized ? WindowState.Maximized : WindowState.Normal;        }        private void BtnMin_Click(object sender, RoutedEventArgs e)        {            this.WindowState = WindowState.Minimized;        }        private void WinMain_SizeChanged(object sender, SizeChangedEventArgs e)        {            PMax.Data = this.WindowState == WindowState.Maximized ? Resources["pathRestore"] as Geometry : Resources["pathMaximize"] as Geometry;        }
(0)

相关推荐