如果有附近的坐标,我怎么能改变我的光标形状?

时间:2016-07-18 20:23:08

标签: c# wpf logic

我有一堆坐标,我将它们绘制为位图图像上的坐标系。将鼠标悬停在我的图片上时,我想使用以下例程突出显示Distance内某个MousePosition内的坐标:

public void HighLightNearbyDots(Point _mousePosition)
{
    // ..

    foreach (var point in myDisplayedCoords)
    {
        Distance = (int)(MousePosition - point); // this gets the distance between two points and converts it to an int

        if (Distance < 10)
            point.Color = Colors.Red;
        else
            point.Color = InitialCoordColor;
    }
    DrawImage();
}

现在,如果我找到了附近的坐标,我想将我的CursorShape 更改为HAND。我不能在if (Distance < NearbyCursorDistance)内做到这一点;为什么?因为我需要在else上更改回箭头(需要一秒钟),用户不会看到它,或者它将继续作为HAND执行其余的操作。所以我实现了这个:

private bool IsThereANearbyDot(CoordPoint _mousePosition)
{
    foreach(var point in MyDisplayedCoords)
    {
        if (10 > (int) (_mousePosition - point))
            return true;
    }
    return false;
}

并以这种方式使用它:

public void HighLightNearbyDots(Point _mousePosition)
{
    //..

    if (IsThereANearbyDot(MousePosition))
        CursorShape = Cursors.Hand;
    else
        CursorShape = Cursors.Arrow;

    foreach (var point in myDisplayedCoords)
    {
        Distance = (int)(MousePosition - point); 

        if (Distance < 10)
            point.Color = Colors.Red;
        else
            point.Color = InitialCoordColor;
    }
    DrawImage();
}

它有效,但如果MyDisplayedCoords很大,我现在需要迭代两次,这需要花费很多时间,用户会注意到屏幕滞后。我怎么能解决这个问题?

1 个答案:

答案 0 :(得分:2)

您需要做的就是将光标设置为HighLightNearbyDots()方法开头的默认值(箭头),然后设置为distance < 10子句(而不是{{1}) })。 E.g:

else

光标闪烁不应该有任何问题,但如果你这样做,你可以设置一个标志,只需在方法中设置一次光标,如下所示:

public void HighLightNearbyDots(Point _mousePosition)
{
    // ..

    CursorShape = Cursors.Arrow;

    foreach (var point in myDisplayedCoords)
    {
        Distance = (int)(MousePosition - point); // this gets the distance between two points and converts it to an int

        if (Distance < 10)
        {
            CursorShape = Cursors.Hand;
            point.Color = Colors.Red;
        }
        else
            point.Color = InitialCoordColor;
    }
    DrawImage();
}

这将确保每次执行public void HighLightNearbyDots(Point _mousePosition) { // .. bool handCursor = false; foreach (var point in myDisplayedCoords) { Distance = (int)(MousePosition - point); // this gets the distance between two points and converts it to an int if (Distance < 10) { handCursor = true; point.Color = Colors.Red; } else point.Color = InitialCoordColor; } CursorShape = handCursor ? Cursors.Hand : Cursors.Arrow; DrawImage(); } 方法(可能是您定期进行的操作,例如鼠标移动事件)时光标都会被解析,并且除非您执行此操作,否则它将成为默认光标突出显示任何点,在这种情况下它将是手。

如果这不能解决您的问题,请提供一个好的Minimal, Complete, and Verifiable example,清楚地显示您正在做的事情,以及对代码的作用以及您希望它做什么的精确解释,以及正如具体而言你无法搞清楚。