我正在尝试创建类似于 Adobe Lightroom 中的 Lights out /lights dim 功能(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 对象传递到其下方的窗口。
有什么想法吗?或者更好的方法来解决这个问题?
我也遇到过类似的问题并找到了解决方案:
public static class WindowsServices
{
const int WS_EX_TRANSPARENT = 0x00000020;
const int GWL_EXSTYLE = (-20);
[DllImport("user32.dll")]
static extern int GetWindowLong(IntPtr hwnd, int index);
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hwnd, int index, int newStyle);
public static void SetWindowExTransparent(IntPtr hwnd)
{
var extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle | WS_EX_TRANSPARENT);
}
}
对于您的窗户套装:
WindowStyle = None
Topmost = true
AllowsTransparency = true
在窗口后面的代码中添加:
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
var hwnd = new WindowInteropHelper(this).Handle;
WindowsServices.SetWindowExTransparent(hwnd);
}
瞧 - 点击窗口!请参阅原始答案:http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/a3cb7db6-5014-430f-a5c2-c9746b077d4f
您所描述的听起来像是预期的行为。一种解决方案是将路径上的填充设置为 {x:Null},因为这是使对象不命中测试的唯一可靠方法。
我有另一个想法。
如果您在鼠标光标正下方制作一个完全透明的像素会怎么样? :]