WPF下设置透明或者半透明窗体需要以下几个条件:
- 窗体的WindowStyle属性必须是None,非None属性的窗体无法呈现透明的效果。
- 设置窗体的Background="Transparent" AllowsTransparency="True",背景颜色必须为Transparent,否则透明效果会变成非透明效果,AllowsTransparency="True"使窗体的子控件透明属性有效。
- 然后在窗体内添加需要的透明组件,例如:
<Window x:Class="TransparentWindows.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:TransparentWindows"
mc:Ignorable="d"
Background="Transparent" AllowsTransparency="True" WindowStyle="None"
Title="MainWindow" Height="350" Width="525" WindowStartupLocation="CenterScreen">
<Grid Width="{Binding Width, ElementName=w}" Height="{Binding Height, ElementName=w}">
<Border CornerRadius="5" Margin="2" BorderThickness="2" BorderBrush="White" Opacity="0.8">
<Border.Effect>
<DropShadowEffect ShadowDepth="0" Color="#FF414141" BlurRadius="8"/>
</Border.Effect>
<Border Background="Red" Opacity="0.2" Margin="0" CornerRadius="5"/>
</Border>
</Grid>
</Window>
网友评论