如何在WPF中使鼠标事件对窗口不可见?

Lar*_*187 5 c# wpf events mouseevent

我创建了这个类,它完美地使我的WPF应用程序对鼠标事件透明.

using System.Runtime.InteropServices;

class Win32

{
    public const int WS_EX_TRANSPARENT = 0x00000020;
    public const int GWL_EXSTYLE = (-20);

    [DllImport("user32.dll")]
    public static extern int GetWindowLong(IntPtr hwnd, int index);

    [DllImport("user32.dll")]
    public static extern int SetWindowLong(IntPtr hwnd, int index, int newStyle);

    public static void makeTransparent(IntPtr hwnd)
    {
        // Change the extended window style to include WS_EX_TRANSPARENT
        int extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
        Win32.SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle | WS_EX_TRANSPARENT);    
    }

    public static void makeNormal(IntPtr hwnd)
    {
      //how back to normal what is the code ?

    }

}
Run Code Online (Sandbox Code Playgroud)

我运行它来使我的应用程序忽略鼠标事件,但在执行代码后,我希望应用程序恢复正常并再次处理鼠标事件.怎么办?

IntPtr hwnd = new WindowInteropHelper(this).Handle;
Win32.makeTransparent(hwnd);
Run Code Online (Sandbox Code Playgroud)

使应用程序恢复正常的代码是什么?

Cod*_*ray 7

现有类中的以下代码获取现有窗口样式(GetWindowLong),并将WS_EX_TRANSPARENT样式标志添加到现有窗口样式:

// Change the extended window style to include WS_EX_TRANSPARENT
int extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
Win32.SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle | WS_EX_TRANSPARENT);
Run Code Online (Sandbox Code Playgroud)

如果要将其更改回正常行为,则需要删除WS_EX_TRANSPARENT从窗口样式添加的标志.您可以通过执行按位AND NOT操作来执行此操作(与您添加标志时执行的OR操作相反).根据deltreme的回答,完全没有必要记住以前检索到的扩展样式,因为你要做的只是清除标志.WS_EX_TRANSPARENT

代码看起来像这样:

public static void makeNormal(IntPtr hwnd)
{
    //Remove the WS_EX_TRANSPARENT flag from the extended window style
    int extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
    Win32.SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle & ~WS_EX_TRANSPARENT);
}
Run Code Online (Sandbox Code Playgroud)