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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM