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