How can I make a window invisible to mouse events in WPF?

試著忘記壹切 提交于 2019-12-05 10:56:48
Cody Gray

The following code in your existing class gets the existing window styles (GetWindowLong), and adds the WS_EX_TRANSPARENT style flag to those existing window styles:

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

When you want to change it back to the normal behavior, you need to remove the WS_EX_TRANSPARENT flag that you added from the window styles. You do this by performing a bitwise AND NOT operation (in contrast to the OR operation you performed to add the flag). There's absolutely no need to remember the previously retrieved extended style, as suggested by deltreme's answer, since all you want to do is clear the WS_EX_TRANSPARENT flag.

The code would look something like this:

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

This code fetches the current window style:

int extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE); 

This code sets the WS_EX_TRANSPARENT flag on the extendedStyle:

Win32.SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle | WS_EX_TRANSPARENT);

All you need to do is remember what extendedStyle you got from GetWindowLong(), and call SetWindowLong() again with that original value.

Have you tried using this instead? (this equals window)

this.IsHitTestVisible = false;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!