WPF高性能无边框窗体

WPF高性能无边框窗体

结论

AllowsTransparency设置为true后,整体渲染性能降低,将会占用更多CPU资源。

解决方法

使用WindowChrome制作背景透明的窗体,这样可以避免异形窗口导致的低渲染性能。

构成要素对比

AllowsTransparency方式

  1. WindowStyle="None"
  2. AllowsTransparency="True"
  3. Background="Transparent" 或 Background="{x:Null}

WindowChrome方式

  1. WindowStyle="None"
  2. ResizeMode="NoResize" 或 ResizeMode="CanMinimize"
  3. <WindowChrome GlassFrameThickness="-1"/>
<WindowChrome.WindowChrome>
   <WindowChrome GlassFrameThickness="-1"/>
</WindowChrome.WindowChrome>

具体实现

<Window x:Class="异形窗体测试.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:异形窗体测试"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        Title="MainWindow"
        Width="800"
        Height="450"
        ResizeMode="CanMinimize"
        WindowStyle="None"
        mc:Ignorable="d">
    <WindowChrome.WindowChrome>
        <WindowChrome GlassFrameThickness="-1" />
    </WindowChrome.WindowChrome>
    <Window.Template>
        <ControlTemplate TargetType="Window">
            <Border Padding="64" Background="Transparent">
                <Border Background="White" CornerRadius="20">
                    <Border.Effect>
                        <DropShadowEffect BlurRadius="64" />
                    </Border.Effect>
                    <ContentPresenter ClipToBounds="True"/>
                </Border>
            </Border>
        </ControlTemplate>
    </Window.Template>

    <Grid>
        
        <TextBlock VerticalAlignment="Center"
                   FontFamily="24"
                   Foreground="#333333"
                   TextAlignment="Center">
            <Run Text="欢迎访问鱼塘816博客" />
            <LineBreak />
            <Run FontSize="32" Text="https://www.cnblogs.com/fishpond816/" />
        </TextBlock>
    </Grid>
</Window>

原文出处

WPF 制作高性能的透明背景异形窗口(使用 WindowChrome 而不要使用 AllowsTransparency=True)

[WPF 自定义控件]使用WindowChrome自定义Window Style

[WPF 自定义控件]使用WindowChrome的问题

WPF从最脆弱源代码了解允许透明度性能差异的原因

登峰造极的成就源于自律
原文地址:https://www.cnblogs.com/fishpond816/p/14299208.html