WPF CaptureMouse()之后不发送MouseMove事件; [英] WPF Not sending MouseMove events after CaptureMouse();

查看:1322
本文介绍了WPF CaptureMouse()之后不发送MouseMove事件;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有一个带有圆角矩形的WPF画布,我可以使用鼠标拖动。但是,一旦我尝试捕获鼠标在画布上,我不再得到移动事件。

I'm trying to have a WPF canvas with rounded rectangles on that I can drag round using the mouse. However once I try and capture the mouse on the canvas I don't get the move events any more.

这是一个mycanvas用户控件,矩形是 foo用户控件。这些(减去序言)的XAML是:

This is a "mycanvas" user control and the rectangles are "foo" user controls. The XAML for these (minus the preamble) are:

mycanvas.xaml:

mycanvas.xaml:

<Canvas MouseDown="CanvasMouseDown" MouseMove="CanvasMouseMove" MouseUp="CanvasMouseUp" Background="White">

    <my:Foo HorizontalAlignment="Left" Canvas.Left="97" Canvas.Top="30" x:Name="m_foo" VerticalAlignment="Top" Height="87" Width="128" />
</Canvas>

foo.xaml:

<Border BorderThickness="2" BorderBrush="Black" CornerRadius="15" Background="Plum">
    <Grid>
        <Label Content="Foo" Height="28" HorizontalAlignment="Left" Margin="6,6,0,0" Name="label1" VerticalAlignment="Top" />
    </Grid>
</Border>

然后处理程序是:
mycanvas.xaml.cs:

And then the handlers are: mycanvas.xaml.cs:

private void CanvasMouseDown(object sender, MouseButtonEventArgs e)
{
    if (e.Source is Foo)
    {
        m_moving = e.Source as Foo;
        CaptureMouse();
        e.Handled = true;
    }
}

private void CanvasMouseMove(object sender, MouseEventArgs e)
{
    if (m_moving != null)
    {
        Canvas.SetLeft(m_moving, e.GetPosition(this).X);
        Canvas.SetTop(m_moving, e.GetPosition(this).Y);
    }
}

private void CanvasMouseUp(object sender, MouseButtonEventArgs e)
{
    ReleaseMouseCapture();
    m_moving = null;
}

MouseDown触发,因此CaptureMouse被调用更长的关闭应用程序或点击其中的任何东西!),但MouseMove永远不会再被调用 - 所以MouseMove事件在哪里发送现在???

The MouseDown fires and so the CaptureMouse gets called (and works because I can no longer close the app or click anything else in it!) but the MouseMove never gets called anymore - so where do the MouseMove events get sent now???

如果我选择另一个应用程序,然后立即返回,直到鼠标移动鼠标,Foo移动鼠标。

If I alt-tab to another application and then go back now suddendly the MouseMove is called and the Foo moves with the mouse.

推荐答案

尝试:

Mouse.Capture(this, CaptureMode.SubTree);

m_moving.CaptureMouse();
...
if (m_moving != null)
{
    m_moving.ReleaseMouseCapture();
    m_moving = null;
}

鼠标事件由Foo引发,而不是由Canvas引发当你使用Canvas捕获鼠标时,可以防止它们被提升。

The mouse events were being raised by the Foo, not by the Canvas, so when you capture the mouse with the Canvas you prevent them from being raised.

这篇关于WPF CaptureMouse()之后不发送MouseMove事件;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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