如何在WPF中创建一个允许鼠标事件通过的半透明窗口

RMK*_*RMK 33 wpf transparency

我试图在Adobe Lightroom(http://www.youtube.com/watch?v=87hNd3vaENE)中创建类似于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>
Run Code Online (Sandbox Code Playgroud)

窗口是完全透明的,因此在路径未覆盖的位置,鼠标事件直接通过.到现在为止还挺好.路径对象上的IsHitTestvisible设置为false.因此鼠标事件将通过它传递到同一表单上的其他控件(即,您可以单击Button,因为它位于同一表单上).

但是鼠标事件不会通过Path对象传递到它下面的窗口.

有任何想法吗?还是更好的方法来解决这个问题?

谢谢.

Ole*_*sov 58

我有类似的问题,并找到了解决方案:

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);
  }
}
Run Code Online (Sandbox Code Playgroud)

为您的窗口设置:

WindowStyle = None
Topmost = true
AllowsTransparency = true
Run Code Online (Sandbox Code Playgroud)

在窗口后面的代码中添加:

protected override void OnSourceInitialized(EventArgs e)
{
  base.OnSourceInitialized(e);
  var hwnd = new WindowInteropHelper(this).Handle;
  WindowsServices.SetWindowExTransparent(hwnd);
}
Run Code Online (Sandbox Code Playgroud)

瞧 - 点击窗口!请参阅原始答案:http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/a3cb7db6-5014-430f-a5c2-c9746b077d4f

  • 谢谢,这正是我想要的. (2认同)
  • 不幸的是,这样做后,该按钮现在看起来无法点击。 (2认同)