網站首頁 編程語言 正文
前言
制作無邊框窗口時,系統(tǒng)自帶陰影會消失,這時就需要我自己給窗口添加陰影以防止窗口融入背景。添加陰影的方法很簡單,直接用effect就可以了,但這里還是有個不容易注意到的細節(jié)需要處理,加陰影后窗口最大化可能會有問題。
一、如何實現(xiàn)?
1、去除邊框
(1)方法一
使用WindowStyle可以去除窗口邊框,AllowsTransparency+Background制造透明窗口為陰影留出透明邊距。
注:此方法較影響窗口渲染性能。
<Window WindowStyle="None" AllowsTransparency="True" Background="Transparent" >
(2)方法二
使用WindowChrome也可以實現(xiàn)無邊框窗口,.net4.5之后可以使用此組件。WindowChrome通常不會影響渲染性能。
<Window WindowStyle="None" Background="Transparent" ResizeMode="NoResize"> <WindowChrome.WindowChrome> <WindowChrome GlassFrameThickness="-1" CaptionHeight="0" /> </WindowChrome.WindowChrome <Grid> </Grid> </Window>
2、添加陰影
使用DropShadowEffect 加Margin屬性即可。添加陰影特效后,需要設置margin給陰影留出邊距,否則是看不到陰影的。通常到這一步就結束了,如果窗口需要最大化則繼續(xù)往下。
<Window > <Grid Margin="10" Background="White"> <Grid.Effect> <DropShadowEffect ShadowDepth="0" BlurRadius="10" Opacity="0.8" Color="#AAAAAA"/> </Grid.Effect> </Grid> </Window>
3、添加觸發(fā)器
1、 為何添加觸發(fā)器?
根據上述2個步驟添加完陰影后,如果將窗口最大化就會發(fā)現(xiàn),Margin依然生效,全屏窗口有一個透明外邊距,為了解決這問題所以需要添加觸發(fā)器。
2、 具體實現(xiàn)
在style中使用觸發(fā)器,綁定窗口狀態(tài),當最大化時邊距設為0,其他情況設為陰影需要的邊距。在這里需要注意的是此時Grid不可以設置Margin屬性了只能在觸發(fā)器中設置,因為賦值優(yōu)先級的原因,在Grid中設置Margin后觸發(fā)器的賦值會失效。
<Grid Background="#1e1e1e"> <Grid.Style> <Style TargetType="Grid"> <!--給陰影留出邊距--> <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"> ? ? ? ? ? ? ? ? <!--給陰影留出邊距--> ? ? ? ? ? ? ? ? <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();
}
}
}
三、效果預覽
總結
原文鏈接:https://blog.csdn.net/u013113678/article/details/126045002
相關推薦
- 2022-07-08 一文詳解C++中運算符的使用_C 語言
- 2022-11-14 Python?查看數據類型與格式_python
- 2022-07-07 Python推導式使用詳情_python
- 2022-03-04 Tue Dec 01 00:00:00 GMT+08:00 1998 轉成自定義字符串
- 2023-07-25 使用線程池異步執(zhí)行定時任務
- 2022-03-20 .NET+Sqlite支持加密的操作方法_實用技巧
- 2022-10-23 React日期時間顯示組件的封裝方法_React
- 2022-10-06 一文掌握python中的時間包_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支