MouseEventArgs.GetPosition(...)在WPF中不返回相对位置

时间:2014-09-12 03:46:14

标签: c# wpf winforms wpf-controls

我在应用程序中有两个事件处理程序,它们提供“拖动”类型操作(不是实际的拖动事件)。为此,我有事件处理程序来处理 MouseDown MouseMove 事件。

    private void OnMouseDown(object sender,MouseButtonEventArgs e)
    {
        //The following CORRECTLY gives the relative position
        //Note the MouseButtonEventArgs type
        Point position = e.GetPosition(this.myCanvas);
        Point relativePosition = e.GetPosition(this.myUsercontrol);

        //Do something with position and relative position
    }

    private void OnMouseMove(object sender,MouseEventArgs e)
    {
        //The following INCORRECTLY gives the relative position.
        //Note the MouseEventArgs type

        Point position = e.GetPosition(this.myCanvas);
        Point relativePosition = e.GetPosition(this.myUsercontrol);

        //Do something with position and relative position
    }

我遇到的问题是我需要在 MouseMove 事件期间利用鼠标的相对位置。但是,当我在MouseEventArgs上调用GetPosition(...)时,它返回(0,0),但在MouseButtonEventArgs上调用时工作正常。为什么选择

1 个答案:

答案 0 :(得分:0)

Mouse事件的问题在于它们不仅仅发生在您正在查看的上下文中,同一事件随处发生。如果其他元素设置了MouseEventArgs.Handled = true,那么奇怪的事情就会开始发生。我遇到了同样的问题,e.GetPosition()返回常量{-25; -170},但是只有当窗口处于正常模式时,最大化时一切正常。罪魁祸首是窗口级事件处理程序设置e.Handled = true; 因此,请检查您的应用程序还在哪里使用鼠标事件