MouseEventArgs.GetPosition 在 WPF 中无法按预期工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20098435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:04:48  来源:igfitidea点击:

MouseEventArgs.GetPosition not working as expected in WPF

.netwpf

提问by GreenEyedAndy

I would try a little Shapemoving and wrote the following Application:

我会尝试一点 Shapemoving 并编写以下应用程序:

<Window x:Class="WpfApplication7.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
  <Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="27" />
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <ToolBar Grid.Row="0"/>
    <Canvas Grid.Row="1" MouseDown="OnMouseDown" MouseUp="OnMouseUp" MouseMove="OnMouseMove">
        <Ellipse Width="100" Height="100" Fill="Red" Canvas.Left="50" Canvas.Top="50" />
        <Ellipse Width="100" Height="100" Fill="Red" Canvas.Left="180" Canvas.Top="50" />
        <Rectangle Width="100" Height="100" Fill="Blue" Canvas.Left="220" Canvas.Top="160" />
    </Canvas>
  </Grid>
</Window>

and the Code behind:

以及背后的代码:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace WpfApplication7
{
  public partial class MainWindow
  {
    private UIElement SelectedObject { get; set; }
    private bool captureMouse;
    private Point diffPosition;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void OnMouseDown(object sender, MouseButtonEventArgs e)
    {
        SelectedObject = e.Source as UIElement;
        if (SelectedObject != null)
        {
            captureMouse = SelectedObject.CaptureMouse();
            diffPosition = e.GetPosition(SelectedObject);
        }
    }

    private void OnMouseUp(object sender, MouseButtonEventArgs e)
    {
        if (captureMouse)
        {
            SelectedObject.ReleaseMouseCapture();
        }
        SelectedObject = null;
    }

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

I thought getting the diffPositionof the Click-Point in OnMouseDownwith GetPositionrelative to SelectedObjectsave me a few lines of code, but the height of the Toolbar is added. So my Y-Coordinate is 27 pixel more than I expected. If I delete the Toolbar, all works well. What do I have to do to get the correct offset with the Toolbar in the Form?

我以为获得diffPosition的点击点在OnMouseDownGetPosition相对于SelectedObject我保存的几行代码,但添加了工具栏的高度。所以我的 Y 坐标比我预期的多 27 像素。如果我删除工具栏,一切正常。我该怎么做才能使用表单中的工具栏获得正确的偏移量?

I know it's just a little mistake, but I can't find it.

我知道这只是一个小错误,但我找不到它。

采纳答案by Bizhan

Name your drawing area:

命名您的绘图区域:

 <Canvas Grid.Row="1" x:Name="drawingArea" ...>
    ...
 </Canvas>

and compare positions with drawingArea's.

并与drawingArea 的位置进行比较。

e.GetPosition(this.drawingArea)

This way you don't need to worry about layout changes and etc.

这样您就无需担心布局更改等。