【问题标题】:Made a rectangle completely transparent (a hole in the window) WPF使一个矩形完全透明(窗口中的一个洞)WPF
【发布时间】:2012-02-07 18:45:08
【问题描述】:

我有一个在窗口中动态绘制的矩形。所述窗口具有不透明度设置为 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)

【问题讨论】:

  • 窗口不透明度为40%,还是矩形?
  • 窗口的背景颜色。我正在尝试使用与 windows 内的快照工具相同的渲染。

标签: c# wpf vb.net


【解决方案1】:

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

<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>

【讨论】:

    【解决方案2】:

    将内部矩形设为Opacity Mask

    【讨论】:

    • 你能更精确一点吗?我用我的代码编辑了我的第一篇文章。我阅读了这篇文章,但我没有找到我必须做些什么才能让它发挥作用。
    猜你喜欢
    • 2010-10-20
    • 2010-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    • 2011-12-16
    相关资源
    最近更新 更多