WPF 在 CaptureMouse() 之后不发送 MouseMove 事件;

发布于 2024-09-12 14:24:35 字数 1562 浏览 11 评论 0原文

我正在尝试使用带有圆角矩形的 WPF 画布,我可以使用鼠标在其上拖动圆形。然而,一旦我尝试捕获画布上的鼠标,我就不再收到移动事件。

这是一个“mycanvas”用户控件,矩形是“foo”用户控件。这些的 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:

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 事件发送到哪里现在???

如果我用 alt-tab 切换到另一个应用程序,然后突然返回,MouseMove 就会被调用,并且 Foo 会随着鼠标移动。

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.

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

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>

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

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???

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谈下烟灰 2024-09-19 14:24:35

尝试以下任一操作:

Mouse.Capture(this, CaptureMode.SubTree);

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

鼠标事件是由 Foo 而不是 Canvas 引发的,因此当您使用 Canvas 捕获鼠标时,您会阻止它们被引发。

Try either:

Mouse.Capture(this, CaptureMode.SubTree);

or

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

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.

将军与妓 2024-09-19 14:24:35

您可以直接在Window上使用MouseMove事件:

    public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        this.MouseMove += MouseEventHandler;

    }

    private void MouseEventHandler(Object sender, MouseEventArgs e)
    {
        System.Windows.Point position = e.GetPosition(this);

        Canvas.SetLeft(ElipseElement, position.X-5);
        Canvas.SetTop(ElipseElement, position.Y-5);    


    }
}

You can directly use the MouseMove event on the Window:

    public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        this.MouseMove += MouseEventHandler;

    }

    private void MouseEventHandler(Object sender, MouseEventArgs e)
    {
        System.Windows.Point position = e.GetPosition(this);

        Canvas.SetLeft(ElipseElement, position.X-5);
        Canvas.SetTop(ElipseElement, position.Y-5);    


    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文