WPF自定义窗口(Windows Server 2012 Style)

先上图

WPF自定义窗口(Windows Server 2012 Style)

新建CustomControl,名:HeaderedWindow

Themes\Generic.aml文件夹下加入

笔刷,转换器

     <SolidColorBrush x:Key="ActiveBackground" Color="#FF66CBEA"/>
<SolidColorBrush x:Key="DeactiveBackground" Color="#FFC2D6DC"/>
<loc:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter"/>

最大化,最小化,关闭按钮

 <Style x:Key="styleWindowButtonMinimize" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid x:Name="buttonClose">
<Border x:Name="btnEllipse" BorderThickness="1,0,1,1" BorderBrush="Black" Background="{TemplateBinding Background}"/>
<Path x:Name="iconMin" SnapsToDevicePixels="False" Height="21" Width="28" StrokeThickness="3" Stroke="Black" Fill="#FFFFFFFF" Data="M 8,13.5 L 19 13.5"/>
<ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" RecognizesAccessKey="True"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsFocused" Value="True"/>
<Trigger Property="IsDefaulted" Value="True"/>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="LightGreen" TargetName="btnEllipse"/>
</Trigger>
<Trigger Property="IsPressed" Value="True"/>
<Trigger Property="IsEnabled" Value="False"/>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style> <Style x:Key="styleWindowButtonMaximize" TargetType="{x:Type ToggleButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Grid>
<Border x:Name="btnEllipse" Background="{TemplateBinding Background}" Width="25" Height="21" BorderBrush="Black" BorderThickness="0,0,0,1"/>
<Path x:Name="iconMax" SnapsToDevicePixels="False" Stroke="Black" StrokeThickness="2" Data="F1 M 9 7 H 18 V 14 H 9 V 7 H 18"/>
<Path x:Name="iconRestore" SnapsToDevicePixels="False" Visibility="Collapsed" Stroke="Black" StrokeThickness="1" Data="F1 M 10.5 5.5 H 17.5 V 12.5 M 8.5 7.5 H 15.5 V 14.5 H 8.5 V 7.5 H 9.5 V 13.5 H 14.5 V 8.5 H 8.5"/>
<ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" RecognizesAccessKey="True"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsFocused" Value="True"/>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="LightGreen" TargetName="btnEllipse"/>
</Trigger>
<Trigger Property="IsPressed" Value="True"/>
<Trigger Property="IsEnabled" Value="False"/>
<Trigger Property="IsChecked" Value="True">
<Setter TargetName="iconMax" Property="Visibility" Value="Collapsed" />
<Setter TargetName="iconRestore" Property="Visibility" Value="Visible" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style> <Style x:Key="styleMainWindowButtonClose" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid x:Name="buttonClose" Width="47" Height="21">
<Border x:Name="btnEllipse" BorderBrush="Black" BorderThickness="1,0,1,1" Background="{TemplateBinding Background}"/>
<Path x:Name="iconX" ClipToBounds="True" Stroke="Black" StrokeThickness="1" Data="F1 M 20 7 L 26 15 M 21 7 L 27 15 M 22 7 L 28 15 M 20 15 L 26 7 M 21 15 L 27 7 M 22 15 L 28 7"/>
<ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" RecognizesAccessKey="True"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsFocused" Value="True"/>
<Trigger Property="IsDefaulted" Value="True"/>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#FFEC6C60" TargetName="btnEllipse"/>
</Trigger>
<Trigger Property="IsPressed" Value="True"/>
<Trigger Property="IsEnabled" Value="False"/>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

窗口样式

     <Style TargetType="{x:Type loc:HeaderedWindow}">
<Setter Property="WindowStyle" Value="None"/>
<Setter Property="ResizeMode" Value="NoResize"/>
<Setter Property="Background" Value="#FF66CBEA"/>
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="MinWidth" Value="90"/>
<Setter Property="MinHeight" Value="30"/>
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type loc:HeaderedWindow}">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<Grid SnapsToDevicePixels="True" Background="{TemplateBinding Background}">
<Grid.RowDefinitions>
<RowDefinition x:Name="HeaderRow" Height="30" />
<RowDefinition />
</Grid.RowDefinitions>
<!-- Window header-->
<Grid Name="PART_HeaderContainer" Background="Transparent">
<StackPanel Orientation="Horizontal" Margin="4,0" Visibility="{Binding RelativeSource={ x:Static RelativeSource.TemplatedParent}, Path=ShowDefaultHeader, Converter={ StaticResource BoolToVisibilityConverter }}">
<Image Height="24" Width="24" HorizontalAlignment="Left" VerticalAlignment="Center" Source="{TemplateBinding Icon}"/>
<TextBlock Text="{TemplateBinding Title}" Margin="4,0" VerticalAlignment="Center" FontFamily="Arial" FontSize="15" Foreground="#FFFFFFFF" TextWrapping="NoWrap"/>
</StackPanel>
<StackPanel HorizontalAlignment="Right" VerticalAlignment="Top" Orientation="Horizontal" Margin="0,0,5,0">
<Button x:Name="PART_MinimizeButton" IsTabStop="False" Style="{StaticResource styleWindowButtonMinimize}" Width="28" Height="21" ToolTip="Minimize" Visibility="{Binding RelativeSource={ x:Static RelativeSource.TemplatedParent}, Path=CanResize, Converter={ StaticResource BoolToVisibilityConverter }}"/>
<ToggleButton x:Name="PART_RestoreButton" IsTabStop="False" Style="{StaticResource styleWindowButtonMaximize}" Width="25" Height="21" ToolTip="Maximize" Visibility="{Binding RelativeSource={ x:Static RelativeSource.TemplatedParent}, Path=CanResize, Converter={ StaticResource BoolToVisibilityConverter }}"/>
<Button x:Name="PART_CloseButton" IsTabStop="False" Style="{StaticResource styleMainWindowButtonClose}" Width="47" Height="21" ToolTip="Close"/>
</StackPanel>
<ContentPresenter ContentSource="{TemplateBinding Header}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
</Grid>
<Grid Grid.RowSpan="2" x:Name="PART_ResizerContainers" Visibility="Hidden">
<Grid.Resources>
<sys:Double x:Key="StraightResizerSize">4</sys:Double>
<sys:Double x:Key="SlantResizerSize">8</sys:Double>
<Style TargetType="{x:Type Thumb}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Rectangle Fill="Transparent"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Grid.Resources>
<Thumb Width="{StaticResource StraightResizerSize}" VerticalAlignment="Stretch" HorizontalAlignment="Left" Cursor="SizeWE" x:Name="PART_LeftResizer"/>
<Thumb Height="{StaticResource StraightResizerSize}" VerticalAlignment="Top" HorizontalAlignment="Stretch" Cursor="SizeNS" x:Name="PART_TopResizer"/>
<Thumb Width="{StaticResource StraightResizerSize}" VerticalAlignment="Stretch" HorizontalAlignment="Right" Cursor="SizeWE" x:Name="PART_RightResizer"/>
<Thumb Height="{StaticResource StraightResizerSize}" VerticalAlignment="Bottom" HorizontalAlignment="Stretch" Cursor="SizeNS" x:Name="PART_BottomResizer"/>
<ResizeGrip Width="{StaticResource SlantResizerSize}" Height="{StaticResource SlantResizerSize}" HorizontalAlignment="Right" VerticalAlignment="Bottom" Visibility="{Binding RelativeSource={ x:Static RelativeSource.TemplatedParent}, Path=ShowResizeGrip, Converter={ StaticResource BoolToVisibilityConverter }}"/>
<Thumb Width="{StaticResource SlantResizerSize}" Height="{StaticResource SlantResizerSize}" HorizontalAlignment="Right" VerticalAlignment="Bottom" Cursor="SizeNWSE" x:Name="PART_BottomRightResizer"/>
<Thumb Width="{StaticResource SlantResizerSize}" Height="{StaticResource SlantResizerSize}" HorizontalAlignment="Right" VerticalAlignment="Top" Cursor="SizeNESW" x:Name="PART_TopRightResizer"/>
<Thumb Width="{StaticResource SlantResizerSize}" Height="{StaticResource SlantResizerSize}" HorizontalAlignment="Left" VerticalAlignment="Top" Cursor="SizeNWSE" x:Name="PART_TopLeftResizer"/>
<Thumb Width="{StaticResource SlantResizerSize}" Height="{StaticResource SlantResizerSize}" HorizontalAlignment="Left" VerticalAlignment="Bottom" Cursor="SizeNESW" x:Name="PART_BottomLeftResizer"/>
</Grid>
<Border x:Name="PART_ContentBorder" Grid.Row="1" Margin="7,0,7,7" ClipToBounds="True" Background="White">
<AdornerDecorator>
<ContentPresenter Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"/>
</AdornerDecorator>
</Border>
</Grid>
</Border>
<ControlTemplate.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="CanResize" Value="True"/>
<Condition Property="WindowState" Value="Normal"/>
</MultiTrigger.Conditions>
<Setter TargetName="PART_ResizerContainers" Property="Visibility" Value="Visible"/>
</MultiTrigger>
<Trigger Property="WindowState" Value="Maximized">
<Setter TargetName="PART_ContentBorder" Property="Margin" Value="0"/>
<Setter TargetName="PART_ContentBorder" Property="BorderThickness" Value="0,2,0,0"/>
<Setter TargetName="HeaderRow" Property="Height" Value="25"/>
</Trigger>
<Trigger Property="IsActive" Value="False">
<Setter Property="Background" Value="{StaticResource DeactiveBackground}"/>
<Setter TargetName="PART_MinimizeButton" Property="Background" Value="{StaticResource DeactiveBackground}"/>
<Setter TargetName="PART_RestoreButton" Property="Background" Value="{StaticResource DeactiveBackground}"/>
<Setter TargetName="PART_CloseButton" Property="Background" Value="{StaticResource DeactiveBackground}"/>
</Trigger>
<Trigger Property="IsActive" Value="True">
<Setter Property="Background" Value="{StaticResource ActiveBackground}"/>
<Setter TargetName="PART_MinimizeButton" Property="Background" Value="{StaticResource ActiveBackground}"/>
<Setter TargetName="PART_RestoreButton" Property="Background" Value="{StaticResource ActiveBackground}"/>
<Setter TargetName="PART_CloseButton" Property="Background" Value="#FFC35A50"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

HeaderedWindwo.cs

 public class HeaderedWindow : Window
{
#region 模板中的部分控件名称
private const string HeaderContainerName = "PART_HeaderContainer";
private const string MinimizeButtonName = "PART_MinimizeButton";
private const string RestoreButtonName = "PART_RestoreButton";
private const string CloseButtonName = "PART_CloseButton";
private const string TopResizerName = "PART_TopResizer";
private const string LeftResizerName = "PART_LeftResizer";
private const string RightResizerName = "PART_RightResizer";
private const string BottomResizerName = "PART_BottomResizer";
private const string BottomRightResizerName = "PART_BottomRightResizer";
private const string TopRightResizerName = "PART_TopRightResizer";
private const string TopLeftResizerName = "PART_TopLeftResizer";
private const string BottomLeftResizerName = "PART_BottomLeftResizer";
#endregion #region 依赖属性
public static readonly DependencyProperty ShowDefaultHeaderProperty =
DependencyProperty.Register("ShowDefaultHeader", typeof(bool), typeof(HeaderedWindow), new FrameworkPropertyMetadata(true)); public static readonly DependencyProperty ShowResizeGripProperty =
DependencyProperty.Register("ShowResizeGrip", typeof(bool), typeof(HeaderedWindow), new FrameworkPropertyMetadata(false)); public static readonly DependencyProperty CanResizeProperty =
DependencyProperty.Register("CanResize", typeof(bool), typeof(HeaderedWindow), new FrameworkPropertyMetadata(true)); public static readonly DependencyProperty HeaderProperty =
DependencyProperty.Register("Header", typeof(object), typeof(HeaderedWindow), new FrameworkPropertyMetadata(null, OnHeaderChanged)); public static readonly DependencyProperty HeaderTemplateProperty =
DependencyProperty.Register("HeaderTemplate", typeof(DataTemplate), typeof(HeaderedWindow), new FrameworkPropertyMetadata(null)); public static readonly DependencyProperty HeaderTempateSelectorProperty =
DependencyProperty.Register("HeaderTempateSelector", typeof(DataTemplateSelector), typeof(HeaderedWindow), new FrameworkPropertyMetadata(null)); public static readonly DependencyProperty IsFullScreenMaximizeProperty =
DependencyProperty.Register("IsFullScreenMaximize", typeof(bool), typeof(HeaderedWindow), new FrameworkPropertyMetadata(false)); private static void OnHeaderChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
HeaderedWindow win = sender as HeaderedWindow;
win.RemoveLogicalChild(e.OldValue);
win.AddLogicalChild(e.NewValue);
} public bool ShowDefaultHeader
{
get { return (bool)GetValue(ShowDefaultHeaderProperty); }
set { SetValue(ShowDefaultHeaderProperty, value); }
} public bool CanResize
{
get { return (bool)GetValue(CanResizeProperty); }
set { SetValue(CanResizeProperty, value); }
} public bool ShowResizeGrip
{
get { return (bool)GetValue(ShowResizeGripProperty); }
set { SetValue(ShowResizeGripProperty, value); }
} public object Header
{
get { return (object)GetValue(HeaderProperty); }
set { SetValue(HeaderProperty, value); }
} public DataTemplate HeaderTemplate
{
get { return (DataTemplate)GetValue(HeaderTemplateProperty); }
set { SetValue(HeaderTemplateProperty, value); }
} public DataTemplateSelector HeaderTempateSelector
{
get { return (DataTemplateSelector)GetValue(HeaderTempateSelectorProperty); }
set { SetValue(HeaderTempateSelectorProperty, value); }
} public bool IsFullScreenMaximize
{
get { return (bool)GetValue(IsFullScreenMaximizeProperty); }
set { SetValue(IsFullScreenMaximizeProperty, value); }
} #endregion static HeaderedWindow()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(HeaderedWindow), new FrameworkPropertyMetadata(typeof(HeaderedWindow)));
} #region 私有变量 private FrameworkElement headerContainer;
private Button minimizeButton;
private ToggleButton restoreButton;
private Button closeButton;
private Thumb topResizer;
private Thumb leftResizer;
private Thumb rightResizer;
private Thumb bottomResizer;
private Thumb bottomRightResizer;
private Thumb topRightResizer;
private Thumb topLeftResizer;
private Thumb bottomLeftResizer;
#endregion #region 重写函数 public override void OnApplyTemplate()
{
base.OnApplyTemplate();
headerContainer = GetTemplateChild<FrameworkElement>(HeaderContainerName);
headerContainer.MouseLeftButtonDown += HeaderContainerMouseLeftButtonDown;
closeButton = GetTemplateChild<Button>(CloseButtonName);
closeButton.Click += delegate { Close(); };
restoreButton = GetTemplateChild<ToggleButton>(RestoreButtonName);
restoreButton.Checked += delegate { ChangeWindowState(WindowState.Maximized); };
restoreButton.Unchecked += delegate { ChangeWindowState(WindowState.Normal); };
StateChanged += new EventHandler(HeaderedWindowStateChanged);
minimizeButton = GetTemplateChild<Button>(MinimizeButtonName);
minimizeButton.Click += delegate { ChangeWindowState(WindowState.Minimized); };
topResizer = GetTemplateChild<Thumb>(TopResizerName);
topResizer.DragDelta += new DragDeltaEventHandler(ResizeTop);
leftResizer = GetTemplateChild<Thumb>(LeftResizerName);
leftResizer.DragDelta += new DragDeltaEventHandler(ResizeLeft);
rightResizer = GetTemplateChild<Thumb>(RightResizerName);
rightResizer.DragDelta += new DragDeltaEventHandler(ResizeRight);
bottomResizer = GetTemplateChild<Thumb>(BottomResizerName);
bottomResizer.DragDelta += new DragDeltaEventHandler(ResizeBottom);
bottomRightResizer = GetTemplateChild<Thumb>(BottomRightResizerName);
bottomRightResizer.DragDelta += new DragDeltaEventHandler(ResizeBottomRight);
topRightResizer = GetTemplateChild<Thumb>(TopRightResizerName);
topRightResizer.DragDelta += new DragDeltaEventHandler(ResizeTopRight);
topLeftResizer = GetTemplateChild<Thumb>(TopLeftResizerName);
topLeftResizer.DragDelta += new DragDeltaEventHandler(ResizeTopLeft);
bottomLeftResizer = GetTemplateChild<Thumb>(BottomLeftResizerName);
bottomLeftResizer.DragDelta += new DragDeltaEventHandler(ResizeBottomLeft);
}
#endregion #region 辅助函数 private T GetTemplateChild<T>(string childName) where T : FrameworkElement
{
return (GetTemplateChild(childName) as T);
} /// <summary>
/// 鼠标左键单击拖动标题栏,双击窗体状态变化(最大化和还原)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void HeaderContainerMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (e.ClickCount == )
{
DragMove();
}
else
{
ChangeWindowState(WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized);
}
}
/// <summary>
/// 改变窗体状态
/// </summary>
/// <param name="state">Maximized,Normal,Minimized</param>
private void ChangeWindowState(WindowState state)
{
if (state == WindowState.Maximized)
{
if (!IsFullScreenMaximize && IsLocationOnPrimaryScreen())
{
MaxHeight = SystemParameters.WorkArea.Height;
MaxWidth = SystemParameters.WorkArea.Width;
}
else
{
MaxHeight = double.PositiveInfinity;
MaxWidth = double.PositiveInfinity;
}
}
WindowState = state;
} private void HeaderedWindowStateChanged(object sender, EventArgs e)
{
if (WindowState == WindowState.Minimized)
{
restoreButton.IsChecked = null;
}
else
{
restoreButton.IsChecked = WindowState == WindowState.Maximized;
}
}
/// <summary>
/// 判断控件是否位于主显示器中
/// </summary>
/// <returns></returns>
private bool IsLocationOnPrimaryScreen()
{
return Left < SystemParameters.PrimaryScreenWidth && Top < SystemParameters.PrimaryScreenHeight;
} #endregion #region 重置大小
private void ResizeBottomLeft(object sender, DragDeltaEventArgs e)
{
ResizeLeft(sender, e);
ResizeBottom(sender, e);
} private void ResizeTopLeft(object sender, DragDeltaEventArgs e)
{
ResizeTop(sender, e);
ResizeLeft(sender, e);
} private void ResizeTopRight(object sender, DragDeltaEventArgs e)
{
ResizeRight(sender, e);
ResizeTop(sender, e);
} private void ResizeBottomRight(object sender, DragDeltaEventArgs e)
{
ResizeBottom(sender, e);
ResizeRight(sender, e);
} private void ResizeBottom(object sender, DragDeltaEventArgs e)
{
if (ActualHeight <= MinHeight && e.VerticalChange < )
{
return;
}
if (double.IsNaN(Height))
{
Height = ActualHeight;
}
Height += e.VerticalChange;
} private void ResizeRight(object sender, DragDeltaEventArgs e)
{
if (ActualWidth <= MinWidth && e.HorizontalChange < )
{
return;
}
if (double.IsNaN(Width))
{
Width = ActualWidth;
}
Width += e.HorizontalChange;
} private void ResizeLeft(object sender, DragDeltaEventArgs e)
{
if (ActualWidth <= MinWidth && e.HorizontalChange > )
{
return;
} if (double.IsNaN(Width))
{
Width = ActualWidth;
} Width -= e.HorizontalChange;
Left += e.HorizontalChange;
} private void ResizeTop(object sender, DragDeltaEventArgs e)
{
if (ActualHeight <= MinHeight && e.VerticalChange > )
{
return;
}
if (double.IsNaN(Height))
{
Height = ActualHeight;
}
Height -= e.VerticalChange;
Top += e.VerticalChange;
}
#endregion
}

BoolToVisibilityConverter类

     public class BoolToVisibilityConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return (bool)value ? Visibility.Visible : Visibility.Hidden;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
上一篇:在Windows Server 2012中如何快速开关桌面上经典的“计算机、我的文档”等通用图标


下一篇:根据inode编号来删除文件或目录