制作一个完全透明的矩形(窗口上的一个洞)WPF

时间:2012-02-07 18:45:08

标签: c# wpf vb.net

我有一个在Window中动态绘制的矩形。所述窗口具有背景,其不透明度设置为0.4。 我想让矩形内的区域完全透明(看看窗户后面是什么)。

有没有办法做到这一点?

以下是我的窗口代码:

    <Window x:Class="TakeAScreenzone"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="PloofTAS" Height="355" Width="539" Topmost="True"
    ResizeMode="NoResize" AllowsTransparency="True" 
    ShowInTaskbar="False" ShowActivated="True" WindowStyle="None" Background="#66FFFFFF" >
    <Grid Name="Grid1"></Grid>
</Window>

这里是我用来绘制矩形的代码(其中Grid1是我窗口的主网格):

WorkingRectangle = New Rectangle
        WorkingRectangle.Stroke = New SolidColorBrush(Colors.Red)
        WorkingRectangle.StrokeThickness = 1
        WorkingRectangle.Fill = Nothing 
        WorkingRectangle.HorizontalAlignment = Windows.HorizontalAlignment.Left
        WorkingRectangle.VerticalAlignment = Windows.VerticalAlignment.Top
        Grid1.Children.Add(WorkingRectangle)

2 个答案:

答案 0 :(得分:4)

我相信,您可以使用以下方法(这里我在窗口中心创建了一个矩形孔):

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="PloofTAS" Height="355" Width="539" Topmost="True"
ResizeMode="NoResize" AllowsTransparency="True" 
ShowInTaskbar="False" ShowActivated="True" WindowStyle="None" Background="Transparent">
    <Grid Name="Grid1">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Rectangle Fill="#66FFFFFF" Grid.Column="0" Grid.RowSpan="3"/>
        <Rectangle Fill="#66FFFFFF" Grid.Column="2" Grid.RowSpan="3"/>
        <Rectangle Fill="#66FFFFFF" Grid.Column="1" Grid.Row="0"/>
        <Rectangle Fill="#66FFFFFF" Grid.Column="1" Grid.Row="2"/>
        <Rectangle x:Name="workingRectangle" Fill="Transparent" Stroke="Red" Grid.Column="1" Grid.Row="1"/>
    </Grid>
</Window>

答案 1 :(得分:0)

将内部矩形设为Opacity Mask