1

我正在研究一种使用 SharpMap MapBox 将对象显示为世界地图上的点的表单。目前,如果我用光标输入 MapBox (mapBox1) 并停在该点上,它会显示我想要的工具提示。但是,一旦我在 MapBox 内停止鼠标(不一定在点上)并在 MapBox 内移动鼠标,移动到该点将不会(重新)显示工具提示。但是,如果我离开 MapBox(例如,将光标移出窗口或移到菜单条之一上,或移到覆盖在地图上的按钮上),我可以让工具提示出现,但只有一次我必须像以前一样移动光标。

是什么导致了这种行为,有没有简单的方法来解决它?

我已经尝试使用 ToolTip.Hide(), ToolTip.Active = false (然后在我希望它显示时再次将其设置为 true)并在各个点刷新 MapBox。

相关代码:

ToolTip 是全局的,构造函数定义如下:

toolTip.InitialDelay = 1000;
toolTip.ReshowDelay = 750;
toolTip.ShowAlways = true;

然后我有两个鼠标事件处理程序,都绑定到 MapBox。“obj”是包含纬度和经度点的自定义类的全局对象。

private void mapBox1_MouseHover(object sender, EventArgs e)
    {
        PointF pos = mapBox1.PointToClient(Cursor.Position);
        int screenToleranceX = 20, screenToleranceY = 20;
        PointF posLow = new PointF(pos.X - screenToleranceX, pos.Y - screenToleranceY);
        PointF posHigh = new PointF(pos.X + screenToleranceX, pos.Y + screenToleranceY);
        GeoAPI.Geometries.Coordinate objLoc = new GeoAPI.Geometries.Coordinate(obj.longitude, obj.latitude);
        PointF objPoint = mapBox1.Map.WorldToImage(objLoc);
        if (posLow.X <= objPoint.X && objPoint.X <= posHigh.X && posLow.Y <= objPoint.Y && objPoint.Y <= posHigh.Y)
        {
            toolTip.Active = true;
            toolTip.Show(obj.Name, mapBox1, mapBox1.PointToClient(Cursor.Position));
        }
    }

    private void mapBox1_MouseMove(GeoAPI.Geometries.Coordinate worldPos, MouseEventArgs imagePos)
    {
        PointF pos = mapBox1.PointToClient(Cursor.Position);
        int screenToleranceX = 20, screenToleranceY = 20;
        PointF posLow = new PointF(pos.X - screenToleranceX, pos.Y - screenToleranceY);
        PointF posHigh = new PointF(pos.X + screenToleranceX, pos.Y + screenToleranceY);
        GeoAPI.Geometries.Coordinate objLoc = new GeoAPI.Geometries.Coordinate(obj.longitude, obj.latitude);
        PointF objPoint = mapBox1.Map.WorldToImage(objLoc);
        if (toolTip.Active && (posLow.X > objPoint.X || objPoint.X > posHigh.X || posLow.Y > objPoint.Y || objPoint.Y > posHigh.Y))
        {
            toolTip.Active = false;
        }
    }

** 编辑 **

根据接受的答案,我有以下代码作为解决方案,希望根据需要进一步完善它。但是,这暂时有效(使用外部声明的 bool,toolTipDisp,默认为 false):

private void mapBox1_MouseMove(GeoAPI.Geometries.Coordinate worldPos, MouseEventArgs imagePos)
    {
        PointF pos = mapBox1.PointToClient(Cursor.Position);
        int screenToleranceX = 20, screenToleranceY = 20;
        PointF posLow = new PointF(pos.X - screenToleranceX, pos.Y - screenToleranceY);
        PointF posHigh = new PointF(pos.X + screenToleranceX, pos.Y + screenToleranceY);
        GeoAPI.Geometries.Coordinate objLoc = new GeoAPI.Geometries.Coordinate(obj.longitude, obj.latitude);
        PointF objPoint = mapBox1.Map.WorldToImage(objLoc);
        if (posLow.X <= objPoint.X && objPoint.X <= posHigh.X && posLow.Y <= objPoint.Y && objPoint.Y <= posHigh.Y)
        {
            if (!toolTipDisp)
            {
                toolTip.Show(obj.Name, mapBox1, mapBox1.PointToClient(Cursor.Position));
                toolTipDisp = true;
            }
        }
        else
        {
            toolTip.Hide(mapBox1);
            toolTipDisp = false;
        }
    }
4

1 回答 1

1

试试这个(伪代码):

private string _previous;

private void mapBox1_MouseMove(GeoAPI.Geometries.Coordinate worldPos, MouseEventArgs imagePos)
{
    var text = ...; // generate tooltip text based on the new position
    if(text != _previous)
    {
        _previous = text;
        tooltip.Show(text, mapBox1, mapBox1.PointToClient(imagePos.Location)); 
    }
}

private void mapBox1_MouseLeave(object sender, EventArgs e)
{
    toolTip.Hide(mapBox1);
}
于 2014-09-23T12:47:31.943 回答