WPF发送点击控制

时间:2015-06-13 14:53:19

标签: c# wpf click-through

我想将鼠标单击发送到控件后面的应用程序。我有一个最顶层的透明窗口和一些控件。我希望能够点击一些控件点击。

我已经尝试将属性IsHitTestVisible =“False”设置为控件,但它不起作用。例如,它不会通过控件将控件发送到桌面。

我也尝试过这个问题中提出的解决方案:How to create a semi transparent window in WPF that allows mouse events to pass through

它有效,但我想透明一些控件,而不是窗口。

如何将该问题的解决方案应用于单个控件,例如椭圆?

解决方案:

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);
  }
}

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

0 个答案:

没有答案