在窗口背景中创建一个透明孔 - WPF [英] Create a transparent hole inside a window's background - WPF
问题描述
我有一个包含这些值的窗口:
I have a window with these values:
WindowState="Maximized"
AllowsTransparency="True"
Opacity="0.5"
WindowStyle="None"
此窗口位于其他窗口的顶部(作为弹出窗口),其内容位于特定位置.
This window is coming on top of other window (as a pop-up) with content on it on a specific location.
我有一个新要求.这个窗口必须从下面的窗口中显示一个矩形区域.换句话说,我必须在这个窗口中设置一个完全透明的洞"(没有不透明度值).直到这一刻,我才想出一种方法来制作这个透明的洞.
I have a new requirement. This window have to show a rectangle area from the window below. In other words, i have to set a "hole" in this window which will be totally transparent (without the opacity value). Until this moment i couldn't figure out a way to make this transparent hole.
希望得到一个想法...
Hope to get an idea...
推荐答案
尽量避免 AllowsTransparency=true,它有很多bug,而且很慢.
try to avoid AllowsTransparency=true, it is very buggy and slow.
您可以通过调用 SetWindowRgn 来创建任意形状的窗口:
you can PInvoke SetWindowRgn to create a a window of any shape:
- 使用 CreateRectRgn 两次,一次用于窗口边界矩形,一次用于孔.
- 使用CombineRgn 和RGN_AND 作为第4 个参数来获得一个有洞的区域
- 调用 SetWindowRgn 将区域应用到窗口
- 不要忘记删除除传递给 SetWindowRgn 的区域之外的所有区域
这篇关于在窗口背景中创建一个透明孔 - WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!