wpf 窗口添加阴影并实现自定义窗口

当WindowStyle=None时整个 窗口的样式都没有了包括最大化最新化都没有了。直接上代码吧。。。。

xaml :

<Window x:Class="WindowsNone.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:WindowsNone"
        Style="{StaticResource for_noresize_window}"
        WindowStyle="None"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="50"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <!--头部-->
        <StackPanel Height="50" 
                    Grid.Row="0"
                   MouseMove="TitleBar_MouseMove" 
                   MouseDown="TitleBar_MouseDown" 
                   Background="#336681" 
                   VerticalAlignment="Center">
            <Grid >
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"></ColumnDefinition>
                    <ColumnDefinition Width="Auto"></ColumnDefinition>
                    <ColumnDefinition Width="40"></ColumnDefinition>
                    <ColumnDefinition Width="40"></ColumnDefinition>
                    <ColumnDefinition Width="40"></ColumnDefinition>
                    <ColumnDefinition Width="40"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <Button Grid.Column="3" 
                        x:Name="btn_min" 
                        Height="40"
                        Width="40"
                        Content="min"
                        VerticalAlignment="Center"
                      
                         Click="btn_min_Click" />
                <Button Grid.Column="4" x:Name="btn_max" 
                        VerticalAlignment="Center"
                        Content="Max"
                        Width="40"
                        Height="40"
                        Click="btn_max_Click"/>
                <Button Grid.Column="4" x:Name="btn_exitMax" 
                        VerticalAlignment="Center" Visibility="Collapsed"
                      Content="Normal"
                        Height="40"
                       Width="40"
                        Click="btn_exit_Click"/>
                <Button Grid.Column="5" x:Name="btn_close" 
                        VerticalAlignment="Center"
                        Content="Close"
                        Height="40"
                        Width="40"
                        Click="btn_close_Click"/>
            </Grid>
        </StackPanel>
    </Grid>
</Window>

窗口阴影样式:

  <Style x:Key="for_noresize_window" TargetType="{x:Type Window}">
            <Setter Property="AllowsTransparency" Value="true"/>
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="WindowStyle" Value="None"/>
            <Setter Property="ResizeMode" Value="NoResize"></Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Window}">
                        <Grid Margin="5">
                            <Rectangle Fill="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"  >
                                <Rectangle.Effect>
                                    <DropShadowEffect BlurRadius="10" ShadowDepth="0" Opacity="0.3"/>
                                </Rectangle.Effect>
                            </Rectangle>
                            <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            Padding="{TemplateBinding Margin}"
                            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
                                <ContentPresenter />
                            </Border>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

后台:

/// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        #region 标题栏事件

        /// <summary>
        /// 窗口移动事件
        /// </summary>
        private void TitleBar_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                this.DragMove();
            }
        }

        int i = 0;
        /// <summary>
        /// 标题栏双击事件
        /// </summary>
        private void TitleBar_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount == 2)
            {
                if (this.ActualWidth >= SystemParameters.WorkArea.Width)
                {
                    btn_exit_Click(null, null);
                }
                else
                {
                    btn_max_Click(null, null);
                }
            }
        }

        /// <summary>
        /// 窗口最小化
        /// </summary>
        private void btn_min_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized; //设置窗口最小化
        }

        /// <summary>
        /// 窗口最大化与还原
        /// </summary>
        private void btn_max_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Maximized;
            btn_max.Visibility = Visibility.Collapsed;
            btn_exitMax.Visibility = Visibility.Visible;
        }

        /// <summary>
        /// 窗口关闭
        /// </summary>
        private void btn_close_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
            Application.Current.Shutdown();
        }

        #endregion 标题栏事件

        private void btn_exit_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Normal;
            btn_max.Visibility = Visibility.Visible;
            btn_exitMax.Visibility = Visibility.Collapsed;

        }
    }


阅读更多
个人分类: wpf
下一篇python在线安装第三方包
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭