WPF自定义Window窗体样式

简介: 原文:WPF自定义Window窗体样式资源文件代码: ...
原文: WPF自定义Window窗体样式

资源文件代码:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <!-- 窗体按钮模板 -->
    <ControlTemplate x:Key="tmplWindowBtn" TargetType="{x:Type Button}">
        <Border x:Name="bd" Width="28" Height="18" Background="Transparent"  >
            <Grid>
                <Image x:Name="img" Stretch="Fill" Source="{TemplateBinding Tag}"  />
                <Border x:Name="bdMask" Opacity="0.3" Visibility="Collapsed" Background="#001122" Margin="1 0 1 1" CornerRadius="0 0 3 3"></Border>
            </Grid>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="IsMouseOver" Value="true">
                <Setter TargetName="bdMask" Property="Visibility" Value="Visible"/>
            </Trigger>
            <Trigger Property="IsPressed" Value="true">
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
    <!-- 窗体模板 -->
    <ControlTemplate x:Key="tmplWindowEx" TargetType="{x:Type Window}">
        <Border>
            <Border CornerRadius="5" Background="#0998B8" Margin="{Binding BorderMargin}">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="26"></RowDefinition>
                        <RowDefinition></RowDefinition>
                    </Grid.RowDefinitions>
                    <TextBlock Text="{TemplateBinding Title}" Margin="10 0 0 0" FontFamily="微软雅黑,黑体" FontSize="12" Foreground="#fff" VerticalAlignment="Center"></TextBlock>
                    <StackPanel Orientation="Horizontal" Background="#0998B8" HorizontalAlignment="{Binding BtnPanelHorizontalAlignment}" Width="100" Margin="88 0 0 0">
                        <StackPanel Orientation="Horizontal" Margin="10 0 5 0" VerticalAlignment="Top" HorizontalAlignment="Right">
                            <Button x:Name="btnMinimize" Template="{StaticResource tmplWindowBtn}" WindowChrome.IsHitTestVisibleInChrome="True" Command="{Binding DataContext.WindowBtnCommand, RelativeSource={RelativeSource AncestorType=Window}}" CommandParameter="1" Visibility="{Binding BtnMinimizeVisibility}" >
                                <Button.Tag>
                                    <BitmapImage UriSource="/SunCreate.Common.Controls;Component/WindowEx/Images/btnWindowMin.png"/>
                                </Button.Tag>
                            </Button>
                            <Button x:Name="btnMaximize" Template="{StaticResource tmplWindowBtn}" WindowChrome.IsHitTestVisibleInChrome="True" Command="{Binding DataContext.WindowBtnCommand, RelativeSource={RelativeSource AncestorType=Window}}" CommandParameter="2" Visibility="{Binding BtnMaximizeVisibility}" >
                                <Button.Tag>
                                    <BitmapImage UriSource="/SunCreate.Common.Controls;Component/WindowEx/Images/btnWindowMax.png"/>
                                </Button.Tag>
                            </Button>
                            <Button x:Name="btnClose" Template="{StaticResource tmplWindowBtn}" WindowChrome.IsHitTestVisibleInChrome="True" Command="{Binding DataContext.WindowBtnCommand, RelativeSource={RelativeSource AncestorType=Window}}" CommandParameter="3">
                                <Button.Tag>
                                    <BitmapImage UriSource="/SunCreate.Common.Controls;Component/WindowEx/Images/btnWindowClose.png"/>
                                </Button.Tag>
                            </Button>
                        </StackPanel>
                    </StackPanel>
                    <Border Background="#d6e7f1" CornerRadius="3 0 3 3" Grid.Row="1" Margin="3" >
                        <ContentPresenter ></ContentPresenter>
                    </Border>
                </Grid>
            </Border>
        </Border>
    </ControlTemplate>
    <!-- 窗体样式 -->
    <Style x:Key="stlWindowEx" TargetType="{x:Type Window}">
        <Setter Property="Template" Value="{StaticResource tmplWindowEx}"/>
        <Setter Property="AllowsTransparency" Value="True"></Setter>
        <Setter Property="Background" Value="Transparent"></Setter>
        <Setter Property="BorderThickness" Value="0" />
        <Setter Property="BorderBrush" Value="Transparent" />
        <Setter Property="ResizeMode" Value="NoResize" />
        <Setter Property="WindowStyle" Value="None" />
        <Setter Property="WindowChrome.WindowChrome">
            <Setter.Value>
                <WindowChrome CornerRadius="5 5 0 0"
                              CaptionHeight="35"
                              GlassFrameThickness="0"
                              UseAeroCaptionButtons="False"
                              NonClientFrameEdges="None">
                </WindowChrome>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>
    
View Code

自定义窗体封装WindowEx类代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Resources;

namespace SunCreate.Common.Controls
{
    /// <summary>
    /// 窗体封装
    /// </summary>
    public class WindowEx : Window, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private ICommand _WindowBtnCommand;
        /// <summary>
        /// 窗体按钮命令
        /// </summary>
        public ICommand WindowBtnCommand
        {
            get
            {
                return _WindowBtnCommand;
            }
            set
            {
                _WindowBtnCommand = value;
                OnPropertyChanged("WindowBtnCommand");
            }
        }

        private Thickness _BorderMargin = new Thickness(0, 0, 0, 0);
        public Thickness BorderMargin
        {
            get
            {
                return _BorderMargin;
            }
            set
            {
                _BorderMargin = value;
                OnPropertyChanged("BorderMargin");
            }
        }

        private HorizontalAlignment _BtnPanelHorizontalAlignment = HorizontalAlignment.Right;
        /// <summary>
        /// 窗体按钮的Panel位置
        /// </summary>
        public HorizontalAlignment BtnPanelHorizontalAlignment
        {
            get
            {
                return _BtnPanelHorizontalAlignment;
            }
            set
            {
                _BtnPanelHorizontalAlignment = value;
                OnPropertyChanged("BtnPanelHorizontalAlignment");
            }
        }

        private Visibility _BtnMinimizeVisibility = Visibility.Visible;
        /// <summary>
        /// 窗体最小化按钮的显示状态
        /// </summary>
        public Visibility BtnMinimizeVisibility
        {
            get
            {
                return _BtnMinimizeVisibility;
            }
            set
            {
                _BtnMinimizeVisibility = value;
                OnPropertyChanged("BtnMinimizeVisibility");
            }
        }

        private Visibility _BtnMaximizeVisibility = Visibility.Visible;
        /// <summary>
        /// 窗体最大化按钮的显示状态
        /// </summary>
        public Visibility BtnMaximizeVisibility
        {
            get
            {
                return _BtnMaximizeVisibility;
            }
            set
            {
                _BtnMaximizeVisibility = value;
                OnPropertyChanged("BtnMaximizeVisibility");
            }
        }

        /// <summary>
        /// 窗体 构造函数
        /// </summary>
        public WindowEx()
        {
            this.DataContext = this;
            this.ShowInTaskbar = false;

            #region 窗体样式设置
            Uri uri = new Uri("/SunCreate.Common.Controls;Component/WindowEx/WindowExResource.xaml", UriKind.Relative);
            ResourceDictionary rd = new ResourceDictionary();
            rd.Source = uri;
            this.Style = rd["stlWindowEx"] as Style;
            #endregion

            #region 窗体按钮事件
            WindowBtnCommand windowBtnCommand = new WindowBtnCommand();
            windowBtnCommand.DoAction = (parameter) =>
            {
                if (parameter == 1) //最小化
                {
                    this.BorderMargin = new Thickness(1, 0, 0, 0);
                    BtnPanelHorizontalAlignment = HorizontalAlignment.Left;
                    BtnMinimizeVisibility = Visibility.Collapsed;
                    this.WindowState = WindowState.Minimized;
                }
                if (parameter == 2) //窗口还原、最大化
                {
                    if (this.WindowState == WindowState.Normal)
                    {
                        double taskBarHeight = SystemParameters.PrimaryScreenHeight - SystemParameters.WorkArea.Height;
                        double taskBarWidth = SystemParameters.PrimaryScreenWidth - SystemParameters.WorkArea.Width;
                        if (taskBarWidth > 0)
                        {
                            this.BorderMargin = new Thickness(0, 0, taskBarWidth, 0);
                        }
                        if (taskBarHeight > 0)
                        {
                            this.BorderMargin = new Thickness(0, 0, 0, taskBarHeight);
                        }
                        BtnPanelHorizontalAlignment = HorizontalAlignment.Right;
                        BtnMinimizeVisibility = Visibility.Visible;
                        this.WindowState = WindowState.Maximized;
                    }
                    else if (this.WindowState == WindowState.Maximized)
                    {
                        this.BorderMargin = new Thickness(0, 0, 0, 0);
                        BtnPanelHorizontalAlignment = HorizontalAlignment.Right;
                        BtnMinimizeVisibility = Visibility.Visible;
                        this.WindowState = WindowState.Normal;
                    }
                    else if (this.WindowState == WindowState.Minimized)
                    {
                        this.BorderMargin = new Thickness(0, 0, 0, 0);
                        BtnPanelHorizontalAlignment = HorizontalAlignment.Right;
                        BtnMinimizeVisibility = Visibility.Visible;
                        this.WindowState = WindowState.Normal;
                    }
                }
                if (parameter == 3) //关闭窗口
                {
                    this.Close();
                }
            };
            this.WindowBtnCommand = windowBtnCommand;
            this.StateChanged += (s, e) =>
            {
                if (this.WindowState == WindowState.Maximized)
                {
                    BtnPanelHorizontalAlignment = HorizontalAlignment.Right;
                    BtnMinimizeVisibility = Visibility.Visible;
                    double taskBarHeight = SystemParameters.PrimaryScreenHeight - SystemParameters.WorkArea.Height;
                    double taskBarWidth = SystemParameters.PrimaryScreenWidth - SystemParameters.WorkArea.Width;
                    if (taskBarWidth > 0)
                    {
                        this.BorderMargin = new Thickness(0, 0, taskBarWidth, 0);
                    }
                    if (taskBarHeight > 0)
                    {
                        this.BorderMargin = new Thickness(0, 0, 0, taskBarHeight);
                    }
                }
                if (this.WindowState == WindowState.Normal)
                {
                    this.BorderMargin = new Thickness(0, 0, 0, 0);
                    BtnPanelHorizontalAlignment = HorizontalAlignment.Right;
                    BtnMinimizeVisibility = Visibility.Visible;
                }
            };
            #endregion

        }

        protected void OnPropertyChanged(string name)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }
    }
}
View Code

窗体最小化、最大化、关闭按钮的命令WindowBtnCommand:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace SunCreate.Common.Controls
{
    public class WindowBtnCommand : ICommand
    {
        public Action<int> DoAction { get; set; }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public void Execute(object parameter)
        {
            if (DoAction != null)
            {
                DoAction(Convert.ToInt32(parameter));
            }
        }
    }
}
View Code

使用WindowEx类的示例代码:

<ui:WindowEx x:Class="SunCreate.Common.Controls.Demo.MyWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ui="clr-namespace:SunCreate.Common.Controls;assembly=SunCreate.Common.Controls"
        Title="视频播放视频播放ABCDEFG" Height="300" Width="500" WindowStartupLocation="CenterScreen"
        BtnMinimizeVisibility="Visible" BtnMaximizeVisibility="Visible" >
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/SunCreate.Common.Controls;Component/Themes/ScrollViewer.xaml"/>
                <ResourceDictionary Source="/SunCreate.Common.Controls;Component/Themes/ControlsResource.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <Border Margin="10">
                <Button Height="30" Width="80" Content="测试" Style="{StaticResource stlTxtBtn}" HorizontalAlignment="Left" Click="Button_Click" />
            </Border>
            <Border Margin="10">
                <TextBlock Text="测试内容ABC"></TextBlock>
            </Border>
        </StackPanel>
    </Grid>
</ui:WindowEx>
View Code

效果图:

窗体最小化效果图:

 

目录
相关文章
WPF疑难问题之Treeview中HierarchicalDataTemplate多级样式
WPF疑难问题之Treeview中HierarchicalDataTemplate多级样式
333 0
|
3月前
|
开发框架 缓存 前端开发
循序渐进介绍基于CommunityToolkit.Mvvm 和HandyControl的WPF应用端开发(11) -- 下拉列表的数据绑定以及自定义系统字典列表控件
循序渐进介绍基于CommunityToolkit.Mvvm 和HandyControl的WPF应用端开发(11) -- 下拉列表的数据绑定以及自定义系统字典列表控件
|
3月前
|
开发者 C# 存储
WPF开发者必读:资源字典应用秘籍,轻松实现样式与模板共享,让你的WPF应用更上一层楼!
【8月更文挑战第31天】在WPF开发中,资源字典是一种强大的工具,用于共享样式、模板、图像等资源,提高了应用的可维护性和可扩展性。本文介绍了资源字典的基础知识、创建方法及最佳实践,并通过示例展示了如何在项目中有效利用资源字典,实现资源的重用和动态绑定。
65 0
|
3月前
|
开发者 C# 存储
WPF开发者必读:样式与模板的艺术,轻松定制UI外观,让你的应用程序更上一层楼!
【8月更文挑战第31天】在WPF应用开发中,样式与模板是实现美观界面与一致性的关键工具。样式定义了控件如字体、颜色等属性,而模板则允许自定义控件布局与子控件,两者均可存储于`.xaml`文件中。本文介绍了样式与模板的基础知识,通过示例展示了如何创建并应用它们来改变按钮的外观,从而提升用户体验。
62 0
|
3月前
|
开发框架 前端开发 JavaScript
循序渐进介绍基于CommunityToolkit.Mvvm 和HandyControl的WPF应用端开发(3)--自定义用户控件
循序渐进介绍基于CommunityToolkit.Mvvm 和HandyControl的WPF应用端开发(3)--自定义用户控件
|
3月前
|
C#
WPF 自定义可拖动标题栏
WPF 自定义可拖动标题栏
42 0
|
3月前
|
存储 前端开发 C#
WPF/C#:更改界面的样式
WPF/C#:更改界面的样式
41 0
|
3月前
|
开发框架 前端开发 C#
使用WPF开发自定义用户控件,以及实现相关自定义事件的处理
使用WPF开发自定义用户控件,以及实现相关自定义事件的处理
WPF-布局样式练习-Day02-聊天气泡
WPF-布局样式练习-Day02-聊天气泡
235 1
|
6月前
|
C#
浅谈WPF之样式与资源
WPF通过样式,不仅可以方便的设置控件元素的展示方式,给用户呈现多样化的体验,还简化配置,避免重复设置元素的属性,以达到节约成本,提高工作效率的目的,样式也是资源的一种表现形式。本文以一个简单的小例子,简述如何设置WPF的样式以及资源的应用,仅供学习分享使用,如有不足之处,还请指正。
115 0