WPF透明窗体制作

转自:http://blog.csdn.net/cmis7645/article/details/7781990

先上效果图:

WPF透明窗体制作_第1张图片

代码如下:

WPF透明窗体制作_第2张图片

上面图片中代码如下:

<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        Background="Transparent" AllowsTransparency="True"
        WindowStyle="None" MouseDown="Window_MouseDown" name="w">
    <Grid Width="{Binding Width, ElementName=w}"
          Height="{Binding Height, ElementName=w}">
        <Border CornerRadius="5" Margin="10" BorderThickness="2"
                BorderBrush="White" Opacity="0.8">
            <Border.Effect>
                <DropShadowEffect ShadowDepth="0" Color="#FF414141"
                                  BlurRadius="8"/>
            </Border.Effect>
            <Border Background="Black" Opacity="0.5" Margin="0"
                    CornerRadius="5"/>
        </Border>
        <Grid>
            <Button Content="ldajlajg" Width="200" Height="50" />
        </Grid>
    </Grid>
</Window>

其中Border用来实现透明效果,Grid用来呈现窗体内的控件,为了避免窗体内的控件变成透明的,所以Border,Grid必须处在同一级别上。

 

 

补充:还要将窗体window的background属性设为transparent,把allowTransparent属性设为true,把windowStyle设为None。

但这个透明窗体就不能移动了,要想让窗体移动,还得写window的mouseDown事件如下:

private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                DragMove();
            }
        }

你可能感兴趣的:(WPF,透明窗体)