c# - 鼠标移动比重绘wpf更快

标签 c# wpf shapes mousemove onmousemove

我正在定义继承自 Shape 类并实现“Geometry”属性的形状。

这是一个例子:

public class Landmark : Shape
{
    public override bool IsInBounds(Point currentLocation)
    {

        return (((currentLocation.X >= Location.X - 3) && (currentLocation.X <= Location.X + 3)) && ((currentLocation.Y >= Location.Y - 3) && (currentLocation.Y <= Location.Y + 3)));
    }

    protected override Geometry DefiningGeometry
    {
        get
        {
            var landMark = new EllipseGeometry {Center = Location};

            Stroke = Brushes.Red;
            return landMark;
        }
    }

    protected override void OnIsMouseDirectlyOverChanged(DependencyPropertyChangedEventArgs e)
    {
        StrokeThickness = IsMouseDirectlyOver ? 12 : 6;
        Mouse.OverrideCursor = IsMouseDirectlyOver ? Mouse.OverrideCursor = Cursors.ScrollAll : Mouse.OverrideCursor = Cursors.Arrow;
    }
    protected override void OnMouseMove(MouseEventArgs e)
    {
        if (e.LeftButton == MouseButtonState.Pressed)
        {
            Location = e.GetPosition(this);
            InvalidateVisual();
        }
    }
}

当我单击Shape 并移动鼠标时,我希望在新位置重新绘制Shape - 并且它确实有效。

但是,如果我“太”移动鼠标,那么我将“离开”OnMouseMove 事件,并且 shape 会卡在最后一个位置,鼠标指针和Shape的位置“同步”。

这样的问题能解决吗?

最佳答案

是的,by capturing the Mouse .

要使其发挥作用,您必须确定何时捕获和何时释放。由于您希望使用鼠标左键进行此操作,因此可以在 OnMouseDown 中捕获并在 OnMouseUp 中释放鼠标。

关于c# - 鼠标移动比重绘wpf更快,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38332211/

相关文章:

wpf - 依赖属性回调后调用的自定义控件 OnApplyTemplate

ios - 如何精确检测何时触摸 SKShapeNode?

C#:具有复杂条件的 Debug.Assert()

c# - Json 获取值类型的标题而不是对象

c# - 多语言 WPF 应用程序

c# - 有什么方法可以使用 Wpf (C#) 更改 Web 浏览器的上下文菜单

android三角形drawablw xml

android - 如何通过轮廓多边形设置按钮?安卓

c# - 异步方法中的 Await 与 Task.Result

c# - C# 4.0 中泛型类型参数的变体是否更接近更高种类的类型?