如何使WPF中的鼠标事件不可见? [英] How can I make a window invisible to mouse events in WPF?

查看:176
本文介绍了如何使WPF中的鼠标事件不可见?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I created this class, and it works perfectly for make my WPF application transparent to mouse events.

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 ?

    }

}

我运行这个使我的应用程序忽略鼠标事件,但执行代码后,我希望应用程序恢复正常并再次处理鼠标事件。如何做到这一点?

I run this to make my application ignore mouse events, but after execute the code, I want the application to return to normal and handle mouse events again. How can do that?

IntPtr hwnd = new WindowInteropHelper(this).Handle;
Win32.makeTransparent(hwnd);

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

What is the code to make the application back to normal?

推荐答案

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

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

当您想将其更改回正常行为时,您需要删除您从窗口样式添加的 WS_EX_TRANSPARENT 标志。通过执行按位AND NOT操作(与OR操作相反)你执行了添加标志)。根据先前检索的扩展样式-invisible-to-mouse-events-in-wpf / 4647377#4647377> deltreme的答案,因为你想做的只是清楚 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.

代码看起来像这样:

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

这篇关于如何使WPF中的鼠标事件不可见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆