標籤:
第一、視窗的AllowsTransparency設定為True
第二、視窗的Background設定為Transparent
第三、視窗的WindowStyle設定為None
第四、視窗內的Grid用Clip或者Border設定為需要的形狀
程式碼範例(利用Clip實現圓角視窗)
1 <Window x:Class="Test" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:CaiPiaoUI" 7 mc:Ignorable="d" 8 Title="Test" Height="500" Width="500" WindowStyle="None" AllowsTransparency="True" 9 Background="Transparent" WindowStartupLocation="CenterScreen">10 <Grid Background="Blue">11 <Grid.Clip>12 <RectangleGeometry RadiusX="50" RadiusY="50" Rect="0,0,500,500"></RectangleGeometry>13 </Grid.Clip>14 </Grid>15 </Window>
程式碼範例(利用Border實現圓角視窗)
1 <Window x:Class="Test" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:CaiPiaoUI" 7 mc:Ignorable="d" 8 Title="Test" Height="500" Width="500" WindowStyle="None" AllowsTransparency="True" 9 Background="Transparent" WindowStartupLocation="CenterScreen">10 <Grid>11 <Border BorderThickness="20" CornerRadius="50" Background="Blue"></Border>12 </Grid>13 </Window>
這裡特別注意一點,就是視窗背景色設定的位置:
Clip實現方式中,視窗背景色設定在Grid上;
Border實現方式中,視窗背景色設定在Border上。
WPF-非矩形視窗的建立