我正在处理一个使用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;
}
}
答案 0 :(得分: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);
}