原文:
WPF自定义Window窗体样式
View Code
View Code
View Code
View Code
资源文件代码:
<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>
自定义窗体封装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)); } } } }
窗体最小化、最大化、关闭按钮的命令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)); } } } }
使用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>
效果图:
窗体最小化效果图: