如何在WPF中创建一个允许鼠标事件通过的半透明窗口
我正在尝试创建类似于 Adobe Lightroom WPF 中除外。
我尝试的是在现有窗口之上创建另一个窗口,使其透明并在其上放置半透明的 Path 几何图形。但我希望鼠标事件能够穿过这个半透明窗口(到下面的窗口)。
这是我所拥有的简化版本:
<Window x:Class="LightsOut.MaskWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
AllowsTransparency="True"
WindowStyle="None"
ShowInTaskbar="False"
Topmost="True"
Background="Transparent">
<Grid>
<Button HorizontalAlignment="Left" Height="20" Width="60">click</Button>
<Path IsHitTestVisible="False" Stroke="Black" Fill="Black" Opacity="0.3">
<Path.Data>
<RectangleGeometry Rect="0,0,1000,1000 "/>
</Path.Data>
</Path>
</Grid>
窗口是完全透明的,因此在 Path
未覆盖的地方,鼠标事件会直接通过。到目前为止,一切都很好。路径对象上的 IsHitTestvisible
设置为 False
。因此,鼠标事件将通过它传递到同一窗体上的其他控件(即您可以单击 Button
,因为它位于同一窗体上)。
但鼠标事件不会通过 Path 对象传递到其下方的窗口。
有什么想法吗?或者更好的方法来解决这个问题?
I am trying to create an effect similar to the Lights out /lights dim feature in Adobe Lightroom except in WPF.
What I tried was to create another window over-top of my existing window, make it transparent and put a semi-transparent Path
geometry on it. But I want mouse events to be able to pass through this semi-transparent window (on to the windows below).
This is a simplified version of what I have:
<Window x:Class="LightsOut.MaskWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
AllowsTransparency="True"
WindowStyle="None"
ShowInTaskbar="False"
Topmost="True"
Background="Transparent">
<Grid>
<Button HorizontalAlignment="Left" Height="20" Width="60">click</Button>
<Path IsHitTestVisible="False" Stroke="Black" Fill="Black" Opacity="0.3">
<Path.Data>
<RectangleGeometry Rect="0,0,1000,1000 "/>
</Path.Data>
</Path>
</Grid>
The window is fully transparent, so on places where the Path
doesn't cover, mouse events pass right through. So far so good. The IsHitTestvisible
is set to False
on the path object. So, mouse events will pass through it to other controls on the same form (i.e. you can click on the Button
, because it is on the same form).
But mouse events won't pass through the Path object onto windows that are below it.
Any ideas? Or better ways to solve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我遇到了类似的问题并找到了解决方案:
对于您的窗口设置:
在窗口的代码后面添加:
瞧 - 点击窗口!请参阅原始答案: http: //social.msdn.microsoft.com/Forums/en-US/wpf/thread/a3cb7db6-5014-430f-a5c2-c9746b077d4f
I've had similar problem and found a solution:
for your window set:
in code behind for the window add:
and voila - click-through window! See original answer in: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/a3cb7db6-5014-430f-a5c2-c9746b077d4f
您所描述的听起来像是预期的行为。一种解决方案是将路径上的填充设置为 {x:Null},因为这是使对象不进行命中测试的唯一可靠方法。
What you described sounds like the expected behavior. One solution is to set the Fill to {x:Null} on the Path as that is the only sure way to make an object not hit test.
我有另一个想法。
如果您在鼠标光标正下方制作一个完全透明的像素,会怎么样? :]
I haveanother idea.
What if you made exactly ONE PIXEL, right under the mouse cursor, completely transparent? :]