当前位置:网站首页>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.
边栏推荐
- 二分答案裸题(加一点鸽巢原理)
- 【HMS core】【FAQ】Account Kit、MDM能力、push Kit典型问题合集6
- (2022杭电多校四)1001-Link with Bracket Sequence II(区间动态规划)
- 毕业1年从事软件测试拿下11.5k,没有给98后丢脸吧...
- MySQL——基础知识
- 【HMS core】【Analytics Kit】【FAQ】如何解决华为分析付费分析中付款金额显示为0的问题?
- 微信小程序云开发 | 城市信息管理
- LeetCode Exercise - Two Questions About Finding Sum of Array Elements
- ESP8266-Arduino编程实例-DS18B20温度传感器驱动
- Codeblocks + Widgets 创建窗口代码分析
猜你喜欢

CCNA-网络汇总 超网(CIDR) 路由最长掩码匹配

Application of time series database in the field of ship risk management

ESP8266-Arduino编程实例-DS18B20温度传感器驱动

SwiftUI iOS 精品开源项目之 完整烘焙食品菜谱App基于SQLite(教程含源码)

Immersive experience iFLYTEK 2022 Consumer Expo "Official Designated Product"

《痞子衡嵌入式半月刊》 第 59 期

Codeblocks + Widgets create window code analysis

ESP8266-Arduino编程实例-BMP180气压温度传感器驱动

One year after graduation, I was engaged in software testing and won 11.5k. I didn't lose face to the post-98 generation...

自然语言处理nltk
随机推荐
Quickly build an e-commerce platform based on Amazon cloud technology serverless service - performance
常见链表题及其 Go 实现
CIMC Shilian Dafeitong is the global industrial artificial intelligence AI leader, the world's top AI core technology, high generalization, high robustness, sparse sample continuous learning, industri
【Pointing to Offer】Pointing to Offer 18. Delete the node of the linked list
Fixed asset visualization intelligent management system
卫星电话是直接与卫星通信还是通过地面站?
Network Basics (3) 01-Basic Concepts of Networks - Protocols, Host Addresses, Paths and Parameters of URL Addresses & 127.0.0.1 Local Loopback Address & View URL IP Address and Access Ping Space + URL
毕业1年从事软件测试拿下11.5k,没有给98后丢脸吧...
After 23 years of operation, the former "China's largest e-commerce website" has turned yellow...
【每日一道LeetCode】——191. 位1的个数
二分答案裸题(加一点鸽巢原理)
NC | 西湖大学陶亮组-TMPRSS2“助攻”病毒感染并介导索氏梭菌出血毒素的宿主入侵...
One year after graduation, I was engaged in software testing and won 11.5k. I didn't lose face to the post-98 generation...
DevEco Studio3.0下载失败,提示An unknown error occurred
ByteArrayInputStream class source code analysis
Go system collection
SwiftUI iOS 精品开源项目之 完整烘焙食品菜谱App基于SQLite(教程含源码)
DTSE Tech Talk丨第2期:1小时深度解读SaaS应用系统设计
core sound driver详解
自然语言处理nltk