1. 程式人生 > >WPR-007:WPF中窗體的透明設定

WPR-007:WPF中窗體的透明設定

一般使用WindowStyle=,Background=,AllowsTransparency,Opacity來進行設定。
1、設定Opacity控制整個視窗的透明,包括上面的控制元件

Window x:Class="TestTransparentWindow.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="441" Width="870"
        WindowStyle="None"  AllowsTransparency="True" Opacity="0.5">
    <Grid Width="500" Height="300" Background="Black">
        
    </Grid>
</Window>

2、窗體空白部分全部透明,控制元件部分顯示顏色
這種可是使用自定義的控制元件形狀,來使窗體呈現自定義的形狀

<Window x:Class="TestTransparentWindow.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="441" Width="870"
        WindowStyle="None"  AllowsTransparency="True" Background="#00FFFFFF">
    <Grid Width="500" Height="300" Background="Black">
       
    </Grid>
</Window>

3、空白部分半透明,控制元件部分不透明
依然使用Background設定顏色

<Window x:Class="TestTransparentWindow.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="441" Width="870"
        WindowStyle="None"  AllowsTransparency="True" Background="#AA000000">
    <Grid Width="500" Height="300" Background="Black">
      
    </Grid>
</Window>