WPF半透明窗体的实现

2016-11-23  本文已影响0人  norman1981

WPF下设置透明或者半透明窗体需要以下几个条件:

  1. 窗体的WindowStyle属性必须是None,非None属性的窗体无法呈现透明的效果。
  2. 设置窗体的Background="Transparent" AllowsTransparency="True",背景颜色必须为Transparent,否则透明效果会变成非透明效果,AllowsTransparency="True"使窗体的子控件透明属性有效。
  3. 然后在窗体内添加需要的透明组件,例如:
<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>

上一篇 下一篇

猜你喜欢

热点阅读