当前位置:网站首页>C# wpf borderless window add shadow effect
C# wpf borderless window add shadow effect
2022-07-30 18:35:00 【CodeOfCC】
前言
When making borderless windows,The system's own shadow will disappear,At this time, I need to add shadows to the window to prevent the window from blending into the background.The way to add shadows is simple,直接用effect就可以了,But there is still a subtle detail that needs to be dealt with,Window maximization after shadowing can be problematic.
一、如何实现?
1、去除边框
(1)方法一
使用WindowStyleWindow borders can be removed,AllowsTransparency+BackgroundMake a transparent window to leave a transparent margin for the shadow.
注:This method affects the rendering performance of the window more.
<Window WindowStyle="None" AllowsTransparency="True" Background="Transparent" >
(2)方法二
使用WindowChromeBorderless windows can also be implemented,.net4.5This component can then be used.WindowChromeUsually does not affect rendering performance.
<Window WindowStyle="None" Background="Transparent" ResizeMode="NoResize">
<WindowChrome.WindowChrome>
<WindowChrome GlassFrameThickness="-1" CaptionHeight="0" />
</WindowChrome.WindowChrome <Grid>
</Grid>
</Window>
2、添加阴影
使用DropShadowEffect 加Margin属性即可.After adding shadow effects,需要设置marginLeave a margin for the shadow,Otherwise, no shadow can be seen.Usually it ends at this point,Continue down if the window needs to be maximized.
<Window >
<Grid Margin="10" Background="White">
<Grid.Effect>
<DropShadowEffect ShadowDepth="0" BlurRadius="10" Opacity="0.8" Color="#AAAAAA"/>
</Grid.Effect>
</Grid>
</Window>
3、添加触发器
1、 Why add triggers?
根据上述2Steps after adding shadows,If you maximize the window you will find it,Margin依然生效,Fullscreen windows have a transparent margin,In order to solve this problem, we need to add triggers.
2、 具体实现
在style中使用触发器,bind window state,The margins are set to when maximized0,Otherwise set to the required margin for the shadow.在这里需要注意的是此时Grid不可以设置MarginAttributes can only be set in triggers,Because of assignment priority,在Grid中设置MarginPost-trigger assignments are invalidated.
<Grid Background="#1e1e1e">
<Grid.Style>
<Style TargetType="Grid"> <!--Leave a margin for the shadow--> <Style.Triggers> <DataTrigger Binding="{Binding WindowState, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}" Value="Normal"> <Setter Property="Margin" Value="10" /> </DataTrigger> <DataTrigger Binding="{Binding WindowState, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}" Value="Minimized"> <Setter Property="Margin" Value="10" /> </DataTrigger> <DataTrigger Binding="{Binding WindowState, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}" Value="Maximized"> <Setter Property="Margin" Value="0" /> </DataTrigger> </Style.Triggers> </Style>
</Grid.Style>
</Grid>
二、示例代码
MainWindow.xaml
<Window x:Class="WpfApp8.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:WpfApp8" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800" WindowStyle="None" Background="Transparent" ResizeMode="NoResize" >
<WindowChrome.WindowChrome>
<WindowChrome GlassFrameThickness="-1" CaptionHeight="0" />
</WindowChrome.WindowChrome>
<Grid Background="white">
<Grid.Effect>
<DropShadowEffect ShadowDepth="0" BlurRadius="10" Opacity="0.8" Color="#AAAAAA"/>
</Grid.Effect>
<Grid.Style>
<Style TargetType="Grid"> <!--Leave a margin for the shadow--> <Style.Triggers> <DataTrigger Binding="{Binding WindowState, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}" Value="Normal"> <Setter Property="Margin" Value="10" /> </DataTrigger> <DataTrigger Binding="{Binding WindowState, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}" Value="Minimized"> <Setter Property="Margin" Value="10" /> </DataTrigger> <DataTrigger Binding="{Binding WindowState, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}" Value="Maximized"> <Setter Property="Margin" Value="0" /> </DataTrigger> </Style.Triggers> </Style>
</Grid.Style>
<!--标题栏-->
<Grid VerticalAlignment="Top" >
<StackPanel Margin="0,0,10,0" HorizontalAlignment="Right" Orientation="Horizontal">
<!--最小化按钮-->
<Button Width="50" Height="50" Focusable="False" VerticalAlignment="Center" Cursor="Hand" Click="Button_Click_1" >
<Button.Template>
<ControlTemplate TargetType="Button">
<Grid x:Name="grd" Background="Transparent">
<Rectangle Width="20" Height="3" Fill="#1e1e1e" ></Rectangle>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="grd" Property="Background" Value="#666666"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>
<!--最大化按钮-->
<Button Width="50" Height="50" Focusable="False" VerticalAlignment="Center" Cursor="Hand" Visibility="{DynamicResource MaximizeButtonVisibility}" Click="Button_Click">
<Button.Template>
<ControlTemplate>
<Grid x:Name="grd" Background="Transparent">
<Rectangle Width="20" Height="20" Stroke="#1e1e1e" StrokeThickness="3"></Rectangle>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="grd" Property="Background" Value="#666666"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>
<!--关闭按钮-->
<Button Width="50" Height="50" Focusable="False" VerticalAlignment="Center" Cursor="Hand" Click="Button_Click_2">
<Button.Template>
<ControlTemplate>
<Grid x:Name="grd" Background="Transparent">
<Line Width="20" Height="20" X1="0" Y1="0" X2="20" Y2="20" StrokeThickness="3" Stroke="#1e1e1e" ></Line>
<Line Width="20" Height="20" X1="20" Y1="0" X2="0" Y2="20" StrokeThickness="3" Stroke="#1e1e1e" ></Line>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="grd" Property="Background" Value="#666666"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>
</StackPanel>
</Grid>
</Grid>
</Window>
MainWindow.xaml.cs
using System.Windows;
namespace WpfApp8
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
WindowState = WindowState== WindowState.Maximized? WindowState .Normal: WindowState.Maximized;
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
WindowState = WindowState.Minimized;
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
Close();
}
}
}
三、效果预览

总结
以上就是今天要讲的内容,The method of adding shadows to windows is relatively simple,Just need to pay attention to the case where the window is maximized.But in fact, window shadows have a relatively large impact on performance,Especially if you have rendered video,消耗更多的cpu.So shadows are only suitable for scenes with low performance requirements.
边栏推荐
- 中集世联达飞瞳全球工业人工智能AI领军者,全球顶尖AI核心技术高泛化性高鲁棒性稀疏样本持续学习,工业级高性能成熟AI产品规模应用
- 高精度加法
- node封装一个控制台进度条插件
- 中集世联达工业级成熟航运港口人工智能AI产品规模化应用,打造新一代高效能智慧港口和创新数字港口,全球港航人工智能能领军者中集飞瞳
- The large-scale application of artificial intelligence AI products in industrial-grade mature shipping ports of CIMC World Lianda will create a new generation of high-efficiency smart ports and innova
- 【AGC】构建服务1-云函数示例
- 基于b/s架构搭建一个支持多路摄像头的实时处理系统 ---- 使用yolo v5 系列模型
- Scrapy框架介绍
- The sixteenth issue of eight-part article Balabala said (MQ)
- scrapy基本使用
猜你喜欢
![[Summary] 1396- 60+ VSCode plugins to create a useful editor](/img/e4/65e55d0e4948c011585b72733d4d19.jpg)
[Summary] 1396- 60+ VSCode plugins to create a useful editor

银行适用:此文能够突破你的运维流程管理问题

Codeblocks + Widgets create window code analysis

CCNA-NAT协议(理论与实验练习)

图解LeetCode——11. 盛最多水的容器(难度:中等)

Pytorch foundation -- tensorboard use (1)

natural language processing nltk

After 23 years of operation, the former "China's largest e-commerce website" has turned yellow...

智慧中控屏

【剑指 Offe】剑指 Offer 18. 删除链表的节点
随机推荐
使用postman调接口报Content type ‘text/plain;charset=UTF-8‘ not supported
Redis for infrastructure
荐书 | 推荐好评如潮的3本数据库书籍
NC | 西湖大学陶亮组-TMPRSS2“助攻”病毒感染并介导索氏梭菌出血毒素的宿主入侵...
【HMS core】【FAQ】HMS Toolkit典型问题合集1
载誉而归,重磅发布!润和软件亮相2022开放原子全球开源峰会
3D机器视觉厂商的场景争夺战役
Chapter 14 Type Information
SwiftUI iOS Boutique Open Source Project Complete Baked Food Recipe App based on SQLite (tutorial including source code)
【PHPWord】Quick Start of PHPWord in PHPOffice Suite
Scrapy框架介绍
cocos creater 热更重启导致崩溃
(2022杭电多校四)1001-Link with Bracket Sequence II(区间动态规划)
OSPF详解(3)
Immersive experience iFLYTEK 2022 Consumer Expo "Official Designated Product"
运营 23 年,昔日“国内第一大电商网站”黄了...
Deepen school-enterprise cooperation and build an "overpass" for the growth of technical and skilled talents
Chapter 4 Controlling the Execution Flow
微信小程序云开发 | 城市信息管理
LeetCode Exercise - Two Questions About Finding Sum of Array Elements