資源文件代碼: <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> ...
資源文件代碼:
<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
效果圖:
窗體最小化效果圖: