簡介
Microsoft官網(wǎng)關(guān)于 WindowChome 的介紹
截取Microsoft文章的一段話:
??若要在保留其標(biāo)準(zhǔn)功能時(shí)自定義窗口,可以使用該WindowChrome
類。 該WindowChrome
類將窗口框架的功能與視覺對象分開,并允許你控制應(yīng)用程序窗口的客戶端和非客戶端區(qū)域之間的邊界。 通過WindowChrome
該類,可以通過擴(kuò)展工作區(qū)來覆蓋非工作區(qū),將 WPF 內(nèi)容置于窗口框架中。 同時(shí),它通過兩個(gè)不可見區(qū)域保留系統(tǒng)行為: 調(diào)整邊框 和 標(biāo)題 區(qū)域的大小。
效果圖
自定義最小化、最大化、關(guān)閉按鈕
- 最小化按鈕
<Style
x:Key="SystemCloseButtonStyle"
BasedOn="{StaticResource SystemButtonStyleBase}"
TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid Background="{TemplateBinding Background}">
<Viewbox Width="12" Height="12">
<Path Data="M550.848 502.496l308.64-308.896a31.968 31.968 0 1 0-45.248-45.248l-308.608 308.896-308.64-308.928a31.968 31.968 0 1 0-45.248 45.248l308.64 308.896-308.64 308.896a31.968 31.968 0 1 0 45.248 45.248l308.64-308.896 308.608 308.896a31.968 31.968 0 1 0 45.248-45.248l-308.64-308.864z" Fill="{TemplateBinding BorderBrush}" />
</Viewbox>
<ContentPresenter
HorizontalAlignment="Center"
VerticalAlignment="Center"
Content="{TemplateBinding Content}" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{StaticResource CloseColor}" />
<Setter Property="BorderBrush" Value="{StaticResource DominantColor}" />
</Trigger>
<Trigger Property="IsFocused" Value="True">
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
- 最大化按鈕
<Style
x:Key="SystemMaxButtonStyle"
BasedOn="{StaticResource SystemButtonStyleBase}"
TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid Background="{TemplateBinding Background}">
<Viewbox Width="12" Height="12">
<Path Data="M959.72 0H294.216a63.96 63.96 0 0 0-63.96 63.96v127.92H64.28A63.96 63.96 0 0 0 0.32 255.84V959.4a63.96 63.96 0 0 0 63.96 63.96h703.56a63.96 63.96 0 0 0 63.96-63.96V792.465h127.92a63.96 63.96 0 0 0 63.96-63.96V63.96A63.96 63.96 0 0 0 959.72 0zM767.84 728.505V959.4H64.28V255.84h703.56z m189.322 0H831.8V255.84a63.96 63.96 0 0 0-63.96-63.96H294.216V63.96H959.72z" Fill="{TemplateBinding BorderBrush}" />
</Viewbox>
<ContentPresenter
HorizontalAlignment="Center"
VerticalAlignment="Center"
Content="{TemplateBinding Content}" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{StaticResource SuspensionColor}" />
</Trigger>
<Trigger Property="IsFocused" Value="True">
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
- 關(guān)閉按鈕
<Style
x:Key="SystemMinButtonStyle"
BasedOn="{StaticResource SystemButtonStyleBase}"
TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid Background="{TemplateBinding Background}">
<Viewbox Width="12" Height="10">
<Path Data="M928.2 548h-832c-17.7 0-32-14.3-32-32s14.3-32 32-32h832c17.7 0 32 14.3 32 32s-14.3 32-32 32z" Fill="{TemplateBinding BorderBrush}" />
</Viewbox>
<ContentPresenter
HorizontalAlignment="Center"
VerticalAlignment="Center"
Content="{TemplateBinding Content}" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{StaticResource SuspensionColor}" />
</Trigger>
<Trigger Property="IsFocused" Value="True">
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
/// <summary>
/// 窗口移動
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Move_Click(object sender, System.Windows.Input.MouseButtonEventArgs e) => this.DragMove();
/// <summary>
/// 最小化
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BtnMin_Click(object sender, RoutedEventArgs e) => WindowState = WindowState.Minimized;
/// <summary>
/// 最大化/還原
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BtnMax_Click(object sender, RoutedEventArgs e) => WindowState = WindowState is WindowState.Normal ? WindowState.Maximized : WindowState.Normal;
/// <summary>
/// 關(guān)閉
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BtnClose_Click(object sender, RoutedEventArgs e) => Application.Current.Shutdown();
布局實(shí)現(xiàn)
首先我們需要在
MainWindow
也就是我們的主窗口中的Window.Resources
中實(shí)現(xiàn)WindowChrome
的基本樣式:WindowChrome.ResizeBorderThickness
設(shè)置不可見邊框?qū)挾?br>WindowChrome.CaptionHeight>
設(shè)置屬于標(biāo)題欄的范圍——高度WindowChrome.UseAeroCaptionButtons
是否啟用默認(rèn)系統(tǒng)按鈕功能——三大金剛鍵WindowChrome.NonClientFrameEdges
設(shè)置客戶區(qū)域,使用bottom
可以實(shí)現(xiàn)加載時(shí)空白窗口而不顯示默認(rèn)窗口,提升用戶體驗(yàn)
<Window
x:Class="SignalRClient.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:local="clr-namespace:SignalRClient"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title=""
Width="880"
Height="620"
MinWidth="700"
MinHeight="500"
Style="{StaticResource mainWindow}"
WindowChrome.WindowChrome="{DynamicResource WindowChromeKey}"
WindowStartupLocation="CenterScreen"
mc:Ignorable="d">
<Window.Resources>
<WindowChrome x:Key="WindowChromeKey">
<WindowChrome.ResizeBorderThickness>
<Thickness>5</Thickness>
</WindowChrome.ResizeBorderThickness>
<WindowChrome.CaptionHeight>60</WindowChrome.CaptionHeight>
<WindowChrome.UseAeroCaptionButtons>false</WindowChrome.UseAeroCaptionButtons>
<WindowChrome.NonClientFrameEdges>bottom</WindowChrome.NonClientFrameEdges>
</WindowChrome>
</Window.Resources>
</Window>
重寫窗口,實(shí)現(xiàn)最大化窗口下,標(biāo)題欄及客戶區(qū)域偏移問題的修正。
通過代碼獲取當(dāng)前窗口的工作區(qū)域,及任務(wù)欄以外的其他區(qū)域System.Windows.SystemParameters.WorkArea.Width
獲取工作區(qū)域的寬System.Windows.SystemParameters.WorkArea.Height
獲取工作區(qū)域的高
為什么要使用ValueConverter
主要是因?yàn)?WorkArea
返回的類型無法直接 binding xaml文章來源:http://www.zghlxwxcb.cn/news/detail-457965.html
<ValueConverters:WorkAreaWidth x:Key="workAreaWidth" />
<ValueConverters:WorkAreaHeight x:Key="workAreaHeight" />
<Style x:Key="mainWindow" TargetType="{x:Type Window}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Window">
<ContentControl x:Name="window" Content="{TemplateBinding Content}" />
<ControlTemplate.Triggers>
<Trigger Property="WindowState" Value="Maximized">
<Setter TargetName="window" Property="MaxHeight" Value="{Binding Converter={StaticResource workAreaHeight}}" />
<Setter TargetName="window" Property="MaxWidth" Value="{Binding Converter={StaticResource workAreaWidth}}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
using System;
using System.Globalization;
using System.Windows.Data;
namespace SignalRClient.ValueConverters
{
internal class WorkAreaWidth : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return System.Windows.SystemParameters.WorkArea.Width;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
using System;
using System.Globalization;
using System.Windows.Data;
namespace SignalRClient.ValueConverters
{
internal class WorkAreaHeight : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return System.Windows.SystemParameters.WorkArea.Height;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
結(jié)語
一開始,確實(shí)很難搞,Microsoft 的文檔,里面也并沒有,詳細(xì)介紹窗口內(nèi)容溢出的問題,但是只要仔細(xì)研究過 WPF 的同學(xué)都知道,很多東西是可以通過
Trigger
來實(shí)現(xiàn)的。Get 到這一點(diǎn)很多問題就迎刃而解了。歡迎大家留言通過探討 ??????文章來源地址http://www.zghlxwxcb.cn/news/detail-457965.html
到了這里,關(guān)于【W(wǎng)PF】WindowChrome 自定義窗口完美實(shí)現(xiàn)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!