WPF 实现窗体鼠标事件穿透

一、窗体变透明,需要加三个属性:

AllowsTransparency="True"
Background="Transparent"
WindowStyle="None"

二、利用win32接口实现窗体鼠标事件穿透

Win32 API:

private const int WS_EX_TRANSPARENT = 0x20;

private const int GWL_EXSTYLE = -20;

[DllImport("user32", EntryPoint = "SetWindowLong")]
private static extern uint SetWindowLong(IntPtr hwnd, int nIndex, uint dwNewLong);

[DllImport("user32", EntryPoint = "GetWindowLong")]
private static extern uint GetWindowLong(IntPtr hwnd, int nIndex);
View Code

在窗体的构造函数中调用:

InitializeComponent();

this.SourceInitialized += delegate
{
    IntPtr hwnd = new WindowInteropHelper(this).Handle;
    uint extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
    SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle | WS_EX_TRANSPARENT);
};
View Code

参考文章:

http://www.yuantk.com/weblog/a9ca4f90-56fc-4c8f-bc93-15d63fda4f57.html

原文地址:https://www.cnblogs.com/s0611163/p/15577454.html