【问题标题】:Mouse Event handling鼠标事件处理
【发布时间】:2011-05-05 07:22:22
【问题描述】:

我有一个 WPF 窗口。我希望当我的鼠标光标位于窗口的控制区域之外并且我单击它时我希望我的窗口消失 有没有任何机制可以通过 WPF 实现它??

【问题讨论】:

    标签: wpf wpf-controls binding


    【解决方案1】:

    看看Mouse.Capture 方法。即使鼠标不在您的控制范围内,这也可以让您获得鼠标事件。

    请务必在完成后通过使用 null 调用 Capture 来释放鼠标。

    要释放鼠标捕获,调用 Capture 并将 null 作为要捕获的元素传递。

    在构造函数中放置:

    public MyControl()
    {
        //Other stuff like initialize component
        Mouse.Capture(this);
        MouseLeftButtonDown += OnMouseLeftButtonDown;
    }
    

    然后实现那个方法:

    private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        if(!this.IsMouseOver)
        {
            Close(); //your closing implementation here
            Mouse.Capture(null);
        } 
    }
    

    【讨论】:

    • 我是 wpf 的新手,您能详细说明一下吗??在 Capture mouse 中,Msdn 的说明对我来说还不够
    • 给你..希望澄清事情:)
    • 嗨,Arcturus,我想我的问题陈述不清楚我的 Wpfcontrol 是一个窗口,我的窗口/客户端区域之外是桌面上下文。我尝试放置您共享的代码 sn-p 但是当我在客户区/wpf 窗口上下文外单击鼠标时,我看不到任何调用 OnMouseLeftButtonDown 方法
    • 嗨.. 如果您想在窗口外接收事件,您可能需要查看 Windows Hooks:msdn.microsoft.com/en-us/magazine/cc188966.aspx。顺便说一句,这要复杂得多;)
    猜你喜欢
    • 1970-01-01
    • 2017-05-03
    • 2018-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多