如何在 WPF 窗口中“打孔”

我现在开始使用 WPF,我不知道它是否可能,但它应该是。

我试图在我的表单中创建一个完全透明的区域,删除一个矩形内的所有内容(这些内容将是 AxWindowsMediaPlayer,如下所示),并且能够看到我的窗口后面的内容。

它可以在 WinForms 中使用 TransparencyKey 和面板,但 WinForms 并不能满足我对这个项目的需求。

例子:

http://img3.mukewang.com/634a5eb70001d7ce13670770.jpg

另一个例子:

https://i.stack.imgur.com/I15Jg.gif


慕虎7371278
浏览 148回答 4
4回答

侃侃无极

这应该为您解决问题:<Window&nbsp; &nbsp; &nbsp; &nbsp; [...]&nbsp; &nbsp; &nbsp; &nbsp; Title="MainWindow" MinHeight="200" MinWidth="400" WindowStyle="None" AllowsTransparency="True">&nbsp; &nbsp; <Window.OpacityMask>&nbsp; &nbsp; &nbsp; &nbsp; <ImageBrush&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; ViewportUnits="RelativeToBoundingBox"&nbsp; &nbsp; &nbsp; &nbsp; TileMode="None"&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; ImageSource="/Images/rect.png"&nbsp; &nbsp; &nbsp; &nbsp; />&nbsp; &nbsp; </Window.OpacityMask>&nbsp;<!-- many many controls--></Window>WindowStyle不需要AllowTransparency必须是真的并且图像源只是一个中间有一个矩形透明部分的图像。你也可以动态地画这个!结果:

动漫人物

屏蔽可能是最接近 Windows 窗体的TransparencyKey.

森林海

&nbsp;<Window x:Class="WpfApp5.MainWindow"&nbsp; &nbsp; xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"&nbsp; &nbsp; xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&nbsp; &nbsp; xmlns:d="http://schemas.microsoft.com/expression/blend/2008"&nbsp; &nbsp; xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"&nbsp; &nbsp; xmlns:local="clr-namespace:WpfApp5"&nbsp; &nbsp; mc:Ignorable="d"&nbsp; &nbsp; Title="MainWindow" Height="450" Width="800"&nbsp; &nbsp; WindowStyle="None"&nbsp; &nbsp; Opacity="0.1"&nbsp; &nbsp; AllowsTransparency="True">&nbsp; &nbsp;&nbsp;</Window>使 WindowStyle="None" 和 AllowsTransparency="True" 获得透明窗口。改变不透明度以获得透明度

慕尼黑5688855

得到了这样的东西。<Window x:Class="WpfApp3.MainWindow"&nbsp; &nbsp; xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"&nbsp; &nbsp; xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&nbsp; &nbsp; xmlns:d="http://schemas.microsoft.com/expression/blend/2008"&nbsp; &nbsp; xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"&nbsp; &nbsp; mc:Ignorable="d"&nbsp; &nbsp; Title="MainWindow" Height="450" Width="800" WindowStyle="None" AllowsTransparency="True" Background="Transparent"><Grid>&nbsp; &nbsp; <Grid.RowDefinitions>&nbsp; &nbsp; &nbsp; &nbsp; <RowDefinition Height="*" />&nbsp; &nbsp; &nbsp; &nbsp; <RowDefinition Height="*" />&nbsp; &nbsp; &nbsp; &nbsp; <RowDefinition Height="*" />&nbsp; &nbsp; </Grid.RowDefinitions>&nbsp; &nbsp; <Grid.ColumnDefinitions>&nbsp; &nbsp; &nbsp; &nbsp; <ColumnDefinition Width="*" />&nbsp; &nbsp; &nbsp; &nbsp; <ColumnDefinition Width="*" />&nbsp; &nbsp; &nbsp; &nbsp; <ColumnDefinition Width="*" />&nbsp; &nbsp; </Grid.ColumnDefinitions>&nbsp; &nbsp; <Rectangle Fill="#66FFFFFF" Grid.Column="0" Grid.RowSpan="3"/>&nbsp; &nbsp; <Rectangle Fill="#66FFFFFF" Grid.Column="2" Grid.RowSpan="3"/>&nbsp; &nbsp; <Rectangle Fill="#66FFFFFF" Grid.Column="1" Grid.Row="0"/>&nbsp; &nbsp; <Rectangle Fill="#66FFFFFF" Grid.Column="1" Grid.Row="2"/>&nbsp; &nbsp; <Rectangle x:Name="workingRectangle" Fill="Transparent" Stroke="Red" Grid.Column="1" Grid.Row="1"/></Grid></Window>
打开App,查看更多内容
随时随地看视频慕课网APP