如何在WPF中创建允许鼠标事件通过的半透明窗口
我正在尝试创建一种类似于Adobe Lightroom()中的Lights out/Lights dim功能的效果,WPF中除外 我尝试的是在现有窗口的顶部创建另一个窗口,使其透明,并在其上放置半透明的路径几何体。但我希望鼠标事件能够通过这个半透明窗口(下面的窗口) 这是我所拥有内容的简化版本:如何在WPF中创建允许鼠标事件通过的半透明窗口,wpf,transparency,Wpf,Transparency,我正在尝试创建一种类似于Adobe Lightroom()中的Lights out/Lights dim功能的效果,WPF中除外 我尝试的是在现有窗口的顶部创建另一个窗口,使其透明,并在其上放置半透明的路径几何体。但我希望鼠标事件能够通过这个半透明窗口(下面的窗口) 这是我所拥有内容的简化版本: <Window x:Class="LightsOut.MaskWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/pre
<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>
点击
窗口是完全透明的,所以在路径没有覆盖的地方,鼠标事件会直接通过。到现在为止,一直都还不错。路径对象上的IshittesVisible设置为false。因此,鼠标事件将通过它传递给同一窗体上的其他控件(即,您可以单击按钮,因为它位于同一窗体上)
但是鼠标事件不会通过路径对象传递到它下面的窗口上
有什么想法吗?还是更好的方法来解决这个问题
谢谢 你所描述的听起来像是预期的行为。一种解决方案是将路径上的填充设置为{x:Null},因为这是确保对象不命中测试的唯一可靠方法。我遇到了类似的问题,并找到了解决方案:
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);
}
瞧,点击窗口!见原文:我有另一个想法
如果在鼠标光标的正下方制作一个完全透明的像素会怎么样?:] 谢谢,这正是我想要的。这太棒了,我只想再完成一件事:隐藏光标。基本上将光标设置为“无”,但使用此COOOOL代码,光标将根据窗口下方的内容进行更改。有没有办法让点击通过我们的窗口而不让光标可见?效果很好!我可以在WinForms应用程序的WPF窗口上使用您的“点击”方法。不幸的是,这样做,按钮现在看起来不可点击。半透明度在哪里?