欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 国际 > 最完美的WPF无边框设计!

最完美的WPF无边框设计!

2025/3/15 18:41:54 来源:https://blog.csdn.net/Yoto_Jo/article/details/146261582  浏览:    关键词:最完美的WPF无边框设计!

常规的无边框方法设计

常规的WPF无边框设计方法都是通过AllowsTransparency="True"和WindowStyle=“None”,并且使用WindowChrome样式来实现,但是这样会有问题就是,窗体最大化的时候将底部任务栏给挡住了,另外最大化的时候不能拖动窗体。参考这个大佬的设计@ 若汝棋茗 WPF制作无边框窗体、圆角窗体、支持改变大小、拖动分屏等(一)
但是感觉好麻烦啊。

比较完美的无边框设计

参照这个大佬的设计:梦机器工作室。保留了原改生的最小化、最大化、关闭、拖拽、伸缩窗体大小等操作。
在这里插入图片描述
可以看到在设计器内还是保留了默认的WindowStyle。

运行效果

在这里插入图片描述
完整的源码。

资源字典

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApp1"><Style x:Key="BorderlessButton" TargetType="Button"><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="Button"><Grid Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" Background="{TemplateBinding Background}"><Border x:Name="bg" Background="{StaticResource TransparentColor}" /><Path x:Name="content"Width="{TemplateBinding local:Icon.Width}"Height="{TemplateBinding local:Icon.Height}"Data="{TemplateBinding local:Icon.Geometry}"Fill="{TemplateBinding Foreground}"Stretch="Fill"UseLayoutRounding="True" /></Grid><ControlTemplate.Triggers><Trigger Property="IsMouseOver" Value="True"><Setter TargetName="bg" Property="Background" Value="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=(local:Mouse.OverBackColor)}" /></Trigger><Trigger Property="IsPressed" Value="True"><Setter Property="Opacity" Value="0.8" /></Trigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter></Style><!--#region 系统窗口--><Style x:Key="BorderlessWindowStyle" TargetType="{x:Type local:BorderlessWindow}"><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type local:BorderlessWindow}"><Border Background="{TemplateBinding Background}"BorderBrush="{TemplateBinding BorderBrush}"BorderThickness="{TemplateBinding BorderThickness}"SnapsToDevicePixels="True"><Grid Margin="{TemplateBinding Padding}"><Grid.RowDefinitions><RowDefinition Height="Auto" /><RowDefinition /></Grid.RowDefinitions><AdornerDecorator x:Name="content" Grid.Row="1"><ContentPresenter /></AdornerDecorator><ResizeGrip x:Name="ResizeGrip"Grid.Row="1"Margin="0,0,5,5"HorizontalAlignment="Right"VerticalAlignment="Bottom"IsTabStop="False"Visibility="Collapsed"WindowChrome.ResizeGripDirection="BottomRight" /><Grid Background="{TemplateBinding CaptionBackground}"><Grid.ColumnDefinitions><ColumnDefinition /><ColumnDefinition Width="Auto" /></Grid.ColumnDefinitions><ContentPresenter Content="{TemplateBinding TitleContent}" /><StackPanel Grid.Column="1" Orientation="Horizontal" WindowChrome.IsHitTestVisibleInChrome="True"><!--  最小化按钮  --><Button x:Name="ButtonMin"Width="{TemplateBinding SystemButtonSize}"Height="{TemplateBinding SystemButtonSize}"local:Icon.Geometry="F1M0,6L0,9 9,9 9,6 0,6z"local:Icon.Height="3"local:Mouse.OverBackColor="{TemplateBinding SystemButtonOverColor}"Background="{TemplateBinding SystemButtonColor}"Command="SystemCommands.MinimizeWindowCommand"Foreground="{TemplateBinding SystemButtonForeground}"IsTabStop="False"Style="{StaticResource BorderlessButton}" /><!--  最大化按钮  --><Button x:Name="ButtonMax"Width="{TemplateBinding SystemButtonSize}"Height="{TemplateBinding SystemButtonSize}"local:Icon.Geometry="F1M0,0L0,9 9,9 9,0 0,0 0,3 8,3 8,8 1,8 1,3z"local:Mouse.OverBackColor="{TemplateBinding SystemButtonOverColor}"Background="{TemplateBinding SystemButtonColor}"Command="SystemCommands.MaximizeWindowCommand"Foreground="{TemplateBinding SystemButtonForeground}"IsTabStop="False"Style="{StaticResource BorderlessButton}" /><!--  恢复按钮  --><Button x:Name="ButtonRestore"Width="{TemplateBinding SystemButtonSize}"Height="{TemplateBinding SystemButtonSize}"local:Icon.Geometry="F1M0,10L0,3 3,3 3,0 10,0 10,2 4,2 4,3 7,3 7,6 6,6 6,5 1,5 1,10z M1,10L7,10 7,7 10,7 10,2 9,2 9,6 6,6 6,9 1,9z"local:Mouse.OverBackColor="{TemplateBinding SystemButtonOverColor}"Background="{TemplateBinding SystemButtonColor}"Command="SystemCommands.RestoreWindowCommand"Foreground="{TemplateBinding SystemButtonForeground}"IsTabStop="False"Style="{StaticResource BorderlessButton}" /><!--  关闭窗口按钮  --><Button x:Name="ButtonClose"Width="{TemplateBinding SystemButtonSize}"Height="{TemplateBinding SystemButtonSize}"local:Icon.Geometry="M453.44 512L161.472 220.032a41.408 41.408 0 0 1 58.56-58.56L512 453.44 803.968 161.472a41.408 41.408 0 0 1 58.56 58.56L570.56 512l291.968 291.968a41.408 41.408 0 0 1-58.56 58.56L512 570.56 220.032 862.528a41.408 41.408 0 0 1-58.56-58.56L453.44 512z"local:Mouse.OverBackColor="{TemplateBinding SystemButtonCloseOverColor}"Background="{TemplateBinding SystemButtonColor}"Command="SystemCommands.CloseWindowCommand"Foreground="{TemplateBinding SystemButtonForeground}"IsTabStop="False"Style="{StaticResource BorderlessButton}" /></StackPanel></Grid></Grid></Border><ControlTemplate.Triggers><Trigger Property="FitSystemWindow" Value="True"><Setter TargetName="content" Property="Grid.Row" Value="0" /><Setter TargetName="content" Property="Grid.RowSpan" Value="2" /></Trigger><Trigger Property="WindowState" Value="Maximized"><Setter Property="Padding" Value="8" /><Setter TargetName="ButtonMax" Property="Visibility" Value="Collapsed" /><Setter TargetName="ButtonRestore" Property="Visibility" Value="Visible" /></Trigger><Trigger Property="WindowState" Value="Normal"><Setter TargetName="ButtonMax" Property="Visibility" Value="Visible" /><Setter TargetName="ButtonRestore" Property="Visibility" Value="Collapsed" /></Trigger><Trigger Property="ResizeMode" Value="NoResize"><Setter TargetName="ButtonMin" Property="Visibility" Value="Collapsed" /><Setter TargetName="ButtonMax" Property="Visibility" Value="Collapsed" /><Setter TargetName="ButtonRestore" Property="Visibility" Value="Collapsed" /></Trigger><Trigger Property="ResizeMode" Value="CanMinimize"><Setter TargetName="ButtonMax" Property="Visibility" Value=

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词