WPF利用WindowChrome實現(xiàn)自定義窗口
簡介
Microsoft官網(wǎng)關(guān)于 WindowChome 的介紹
截取Microsoft文章的一段話:
若要在保留其標準功能時自定義窗口,可以使用該 WindowChrome 類。 該 WindowChrome 類將窗口框架的功能與視覺對象分開,并允許你控制應(yīng)用程序窗口的客戶端和非客戶端區(qū)域之間的邊界。 通過 WindowChrome 該類,可以通過擴展工作區(qū)來覆蓋非工作區(qū),將 WPF 內(nèi)容置于窗口框架中。 同時,它通過兩個不可見區(qū)域保留系統(tǒng)行為: 調(dià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();
布局實現(xiàn)
首先我們需要在 MainWindow 也就是我們的主窗口中的 Window.Resources 中實現(xiàn) WindowChrome 的基本樣式:
WindowChrome.ResizeBorderThickness 設(shè)置不可見邊框?qū)挾?/p>
WindowChrome.CaptionHeight> 設(shè)置屬于標題欄的范圍——高度
WindowChrome.UseAeroCaptionButtons 是否啟用默認系統(tǒng)按鈕功能——三大金剛鍵
WindowChrome.NonClientFrameEdges 設(shè)置客戶區(qū)域,使用 bottom 可以實現(xià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>
重寫窗口,實現(xiàn)最大化窗口下,標題欄及客戶區(qū)域偏移問題的修正。
通過代碼獲取當(dāng)前窗口的工作區(qū)域,及任務(wù)欄以外的其他區(qū)域
System.Windows.SystemParameters.WorkArea.Width 獲取工作區(qū)域的寬
System.Windows.SystemParameters.WorkArea.Height 獲取工作區(qū)域的高
為什么要使用 ValueConverter 主要是因為 WorkArea 返回的類型無法直接 binding xaml
<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é)語
一開始,確實很難搞,Microsoft 的文檔,里面也并沒有,詳細介紹窗口內(nèi)容溢出的問題,但是只要仔細研究過 WPF 的同學(xué)都知道,很多東西是可以通過 Trigger 來實現(xiàn)的。Get 到這一點很多問題就迎刃而解了。
到此這篇關(guān)于WPF利用WindowChrome實現(xiàn)自定義窗口的文章就介紹到這了,更多相關(guān)WPF WindowChrome自定義窗口內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#中WebBrowser.DocumentCompleted事件多次調(diào)用問題解決方法
這篇文章主要介紹了C#中WebBrowser.DocumentCompleted事件多次調(diào)用問題解決方法,本文講解了3種情況和各自情況的解決方法,需要的朋友可以參考下2015-01-01
如何:對Windows 窗體控件進行線程安全調(diào)用
使用多線程提高 Windows 窗體應(yīng)用程序的性能時,必須注意以線程安全方式調(diào)用控件。2007-03-03
c# wpf使用GMap.NET類庫,實現(xiàn)地圖軌跡回放
這篇文章主要介紹了c# wpf使用GMap.NET類庫,實現(xiàn)地圖軌跡回放的方法,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-03-03
C#中Equals和GetHashCode使用及區(qū)別
這篇文章主要介紹了C#中Equals和GetHashCode使用及區(qū)別,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
在WPF中合并兩個ObservableCollection集合
這篇文章介紹了在WPF中合并兩個ObservableCollection集合的方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06

