1. 程式人生 > 其它 >WPF 使用WindowChrome自定義窗體樣式

WPF 使用WindowChrome自定義窗體樣式

技術標籤:WPF

WPF 使用WindowChrome自定義窗體樣式

示例程式碼:

<Window x:Class="WpfApp4.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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp4" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800" FontWeight="ExtraLight" ResizeMode=
"CanResize" WindowStartupLocation="CenterScreen" WindowStyle="None" AllowsTransparency="True" Background="{x:Null}"> <Window.Resources> <Style x:Key="btn_nap" TargetType="{x:Type Button}"> <
Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <Border Background="{TemplateBinding Background}"> <ContentPresenter VerticalAlignment="{TemplateBinding VerticalContentAlignment}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"></ContentPresenter> </Border> </ControlTemplate> </Setter.Value> </Setter> <Setter Property="FontSize" Value="18"/> <Setter Property="BorderThickness" Value="0"/> <Setter Property="Background" Value="Transparent"/> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Opacity" Value="0.7"/> </Trigger> <!--<Trigger Property="IsMouseOver" Value="False"> <Setter Property="Background" Value="#EEF0F5"/> </Trigger>--> </Style.Triggers> </Style> </Window.Resources> <WindowChrome.WindowChrome> <WindowChrome CaptionHeight="35" x:Name="windowChrome" CornerRadius="0" GlassFrameThickness="0"/> </WindowChrome.WindowChrome> <Grid> <Grid.RowDefinitions> <RowDefinition Height="35"/> <RowDefinition/> </Grid.RowDefinitions> <Grid Background="#3C6AB1"> <TextBlock x:Name="lblTitle" Text="測試" Foreground="White" FontSize="14" Margin="10 0 0 0" VerticalAlignment="Center"/> <StackPanel Orientation="Horizontal" HorizontalAlignment="Right"> <Button WindowChrome.IsHitTestVisibleInChrome="True" Name="button_MiniSize" Content="─" Style="{StaticResource btn_nap}" HorizontalAlignment="Right" Foreground="White" Margin="0 0 5 0" Height="30" Width="30"/> <Button WindowChrome.IsHitTestVisibleInChrome="True" Name="button_MaxSize" Content="☐" Style="{StaticResource btn_nap}" HorizontalAlignment="Right" Foreground="White" Margin="0 0 5 0" Height="30" Width="30"/> <Button WindowChrome.IsHitTestVisibleInChrome="True" x:Name="btn_Close" Content="✕" Style="{StaticResource btn_nap}" HorizontalAlignment="Right" Foreground="White" Margin="0 0 5 0" Height="30" Width="30"/> </StackPanel> </Grid> <Grid Grid.Row="1" Background="LightBlue"> </Grid> </Grid> </Window>

效果:
在這裡插入圖片描述
介紹:
WindowChrome
若要在保留其標準功能的同時自定義視窗,可以使用 WindowChrome 類。 WindowChrome類將視窗框架的功能與視覺物件隔開,並使你能夠控制應用程式視窗的客戶端和非工作區之間的邊界。 WindowChrome通過使用類,您可以通過擴充套件工作區以覆蓋非工作區來將 WPF 內容置於視窗框架中。 同時,它將通過兩個不可見區域保留系統行為; 調整邊框 和 標題 區的大小。
使用類建立自定義視窗分為兩個主要部分 WindowChrome 。 首先,通過設定物件上公開的屬性來自定義視窗的非客戶端部分 WindowChrome 。 然後,為視窗提供一個模板,該模板定義擴充套件到非工作區的應用程式部分。 物件上公開的屬性 WindowChrome 為 ResizeBorderThickness 、、 CaptionHeight CornerRadius 和 GlassFrameThickness 。
ResizeBorderThickness屬性指定應用程式視窗外的不可見邊框,使用者可以單擊並拖動它來調整視窗的大小。
CaptionHeight屬性在視窗頂部指定一個不可見的區域,該區域啟用通常與標題欄關聯的系統行為。 這些行為包括:單擊並拖動以移動視窗,雙擊以最大化視窗,並右鍵單擊以顯示 “系統” 選單。
調整邊框和標題區的大小不包含任何可視元素;它們僅定義響應輸入和啟用標準系統提供的視窗行為的區域。
CornerRadius屬性指定視窗的角的舍入量。 如果為視窗啟用了玻璃框架,則此屬性不起作用。
GlassFrameThickness屬性指定視窗周圍的玻璃幀的寬度。 預設情況下,它使用屬性指定的系統值 WindowNonClientFrameThickness 來模擬標準視窗的外觀。 使用玻璃幀時,“最小化”、“最大化” 和 “關閉” 的標題按鈕是可見的,並且是互動式的。 應用程式負責顯示應用程式圖示和標題文字。 可以設定 GlassFrameThickness 屬性,使玻璃框架更寬或更小。

介紹參考:https://docs.microsoft.com/zh-cn/dotnet/api/system.windows.shell.windowchrome?view=netframework-4.7.2