当前位置: 首页 > news >正文

最完美的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=

相关文章:

  • Java 大视界 -- Java 大数据分布式计算中的资源调度与优化策略(131)
  • Java 代理模式:从静态代理到动态代理
  • DeepSeek:为教培小程序赋能,引领行业变革新潮流
  • 基于STM32的火灾报警设备(阿里云平台)
  • Oracle Database 11g、12c、18c、19c、21c、22c 与 23AI 各版本差异、优缺点详解
  • 【Node.js入门笔记6---fs流(Streams)与管道(Pipe)】
  • 使用 Doris 和 Hudi
  • JVM 如何保证 Java 程序的安全性?
  • 共享内存的通信
  • css模拟雷达扫描动画
  • 新办公室哪款空气净化器除甲醛效果好?高效除甲醛,提升效率
  • 【算法设计与分析】算法概论
  • 使用Redis如何实现分布式锁?(超卖)
  • JVM的各种细节
  • android 新闻客户端和springboot后台开发(一)
  • 如何处理PHP中的日期和时间问题
  • Node.js REPL 深入解析
  • OTSU算法(大津算法)
  • DeepSeek技术解析:MoE架构实现与代码实战
  • Python高级算法与数据结构优化实战
  • 见微知沪|科学既要勇攀高峰,又要放低身段
  • 朱雀二号改进型遥二运载火箭发射成功
  • 海外考古大家访谈|冈村秀典:礼制的形成与早期中国
  • 2025年“新时代网络文明公益广告”征集展示活动在沪启动
  • 俄乌官员即将在土耳其会谈,外交部:支持俄乌开启直接对话
  • 美国将与阿联酋合作建立海外最大的人工智能数据中心