none
WPF:无边框窗体无法触发OnMouseLeftButtonUp事件! RRS feed

  • 问题

  • 有一个自定义无边框窗体,在OnMouseLeftButtonDown事件中通过DragMove()方法使窗口可以移动,在OnMouseLeftButtonUp事件处理其他业务逻辑,如下代码。

    问题:当鼠标释放后却没有触发OnMouseLeftButtonUp事件!如何解决?

           protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
            {
                DragMove();
                base.OnMouseLeftButtonDown(e);
            }
            protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
            {
                //其他业务逻辑
                base.OnMouseLeftButtonUp(e);
            }

    2018年1月13日 9:48

答案

  • 你好,

    >>为了给窗体添加阴影,特意设置了窗体BorderThickness="10"和DropShadowEffect效果,为什么鼠标释放后没有触发OnMouseLeftButtonUp事件?bug?

    根据你的描述和相关,我重现了你的问题, 这是一个很奇怪的问题, 我猜测是DragMove 的鼠标事件在有BorderThickness 的情况下和OnMouseLeftButtonUp 的鼠标事件有冲突。 我建议你可以使用其他事件来实现你的逻辑。 比如OnLocationChanged 加一个Flag 来判断一下。 请参考下面的代码:

    public partial class Window13 : Window
        {
    
            private bool ChangeFlag = true;
    
            public Window13()
            {
                InitializeComponent();
            }
    
            protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
            {
                this.DragMove();
                this.ChangeFlag = false;
                base.OnMouseLeftButtonDown(e);
            }
    
            protected override void OnLocationChanged(EventArgs e)
            {
                if (!ChangeFlag)
                {
                    Test1.Text = new Random().Next(1, 100).ToString();
                    ChangeFlag = true;
                    base.OnLocationChanged(e);
                }
            }
    
    }

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年1月22日 7:03

全部回复

  • 你好,

    根据你的描述和相关的代码, 我做了一个简单的例子(代码如下),OnMouseLeftButtonUp 事件是可以触发的。 你可以提供一下重现的步骤吗?

    using System.Windows;
    using System.Windows.Input;
    
    namespace WpfApp1
    {
        /// <summary>
        /// Interaction logic for Window9.xaml
        /// </summary>
        public partial class Window9 : Window
        {
            public Window9()
            {
                InitializeComponent();
            }
    
            protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
            {
                DragMove();
                base.OnMouseLeftButtonDown(e);
            }
    
            protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
            {
                MessageBox.Show("UP");
                base.OnMouseLeftButtonUp(e);
            }
        }
    }
    

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年1月15日 6:06
  • 感谢回复!

    我给窗体加了边框和阴影,xaml代码如下:

    <Window x:Class="WpfApp2.Window13"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WpfApp2"
            mc:Ignorable="d"
            Title="Window13" BorderThickness="10" MinHeight="200" Height="320" MinWidth="200" Width="320" AllowsTransparency="True" WindowStyle="None" Background="#FF747373">
        <Window.Effect>
            <DropShadowEffect x:Name="WinDropShadow" ShadowDepth="0" BlurRadius="11" Direction="100" Color="#FF211613" Opacity="0.5"/>
        </Window.Effect>
        <Grid>
            
        </Grid>
    </Window>
    

    后台代码:

    public partial class Window13 : Window
        {
            public Window13()
            {
                InitializeComponent();
            }
            protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
            {
                DragMove();
                base.OnMouseLeftButtonDown(e);
            }
    
            protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
            {
                MessageBox.Show("test");
                base.OnMouseLeftButtonUp(e);
            }
        }

    此时运行程序无法触发OnMouseLeftButtonUp,但是把窗体的BorderThickness="10"删除后再运行程序却能触发OnMouseLeftButtonUp。这是什么原因?

    2018年1月19日 6:29
  • 你好,

    根据你的描述和相关的代码, 我做了一个简单的例子(代码如下),OnMouseLeftButtonUp 事件是可以触发的。 你可以提供一下重现的步骤吗?

    using System.Windows;
    using System.Windows.Input;
    
    namespace WpfApp1
    {
        /// <summary>
        /// Interaction logic for Window9.xaml
        /// </summary>
        public partial class Window9 : Window
        {
            public Window9()
            {
                InitializeComponent();
            }
    
            protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
            {
                DragMove();
                base.OnMouseLeftButtonDown(e);
            }
    
            protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
            {
                MessageBox.Show("UP");
                base.OnMouseLeftButtonUp(e);
            }
        }
    }

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    为了给窗体添加阴影,特意设置了窗体BorderThickness="10"和DropShadowEffect效果,为什么鼠标释放后没有触发OnMouseLeftButtonUp事件?bug?
    2018年1月19日 9:58
  • 你好,

    >>为了给窗体添加阴影,特意设置了窗体BorderThickness="10"和DropShadowEffect效果,为什么鼠标释放后没有触发OnMouseLeftButtonUp事件?bug?

    根据你的描述和相关,我重现了你的问题, 这是一个很奇怪的问题, 我猜测是DragMove 的鼠标事件在有BorderThickness 的情况下和OnMouseLeftButtonUp 的鼠标事件有冲突。 我建议你可以使用其他事件来实现你的逻辑。 比如OnLocationChanged 加一个Flag 来判断一下。 请参考下面的代码:

    public partial class Window13 : Window
        {
    
            private bool ChangeFlag = true;
    
            public Window13()
            {
                InitializeComponent();
            }
    
            protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
            {
                this.DragMove();
                this.ChangeFlag = false;
                base.OnMouseLeftButtonDown(e);
            }
    
            protected override void OnLocationChanged(EventArgs e)
            {
                if (!ChangeFlag)
                {
                    Test1.Text = new Random().Next(1, 100).ToString();
                    ChangeFlag = true;
                    base.OnLocationChanged(e);
                }
            }
    
    }

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年1月22日 7:03
  • 你好,

    >>为了给窗体添加阴影,特意设置了窗体BorderThickness="10"和DropShadowEffect效果,为什么鼠标释放后没有触发OnMouseLeftButtonUp事件?bug?

    根据你的描述和相关,我重现了你的问题, 这是一个很奇怪的问题, 我猜测是DragMove 的鼠标事件在有BorderThickness 的情况下和OnMouseLeftButtonUp 的鼠标事件有冲突。 我建议你可以使用其他事件来实现你的逻辑。 比如OnLocationChanged 加一个Flag 来判断一下。 请参考下面的代码:

    public partial class Window13 : Window
        {
    
            private bool ChangeFlag = true;
    
            public Window13()
            {
                InitializeComponent();
            }
    
            protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
            {
                this.DragMove();
                this.ChangeFlag = false;
                base.OnMouseLeftButtonDown(e);
            }
    
            protected override void OnLocationChanged(EventArgs e)
            {
                if (!ChangeFlag)
                {
                    Test1.Text = new Random().Next(1, 100).ToString();
                    ChangeFlag = true;
                    base.OnLocationChanged(e);
                }
            }
    
    }

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    谢谢回复

    也只能这么做了。

    2018年1月24日 9:48