ToolTip.cs

This example demonstrates how to show tootip with attributes of a shape in mousemove event. The tooltip is drawn as a label on the spatially referenced drawing layer. The smooth redraw of the tooltip is possible only for version higher than 4.8 where the redraw of the spatially referenced drawing layers can be done independently of the data layers. Here is a screenshot with the results of the code.

ToolTip.png
´╗┐using System;
using System.Windows.Forms;
using AxMapWinGIS;
using MapWinGIS;
using System.IO;

public partial class MapExamples
{
    // the handle of the drawing layer
    private int m_drawingHandle = -1;

    // <summary>
    // Opens a shapefile, registers event handler
    // </summary>
    public void ToolTip(AxMap axMap1, string dataPath)
    {
        string filename = dataPath + "landuse.shp";

        if (!File.Exists(filename))
        {
            MessageBox.Show("Couldn't file the file: " + filename);
            return;
        }

        Shapefile sf = new Shapefile();
        sf.Open(filename, null);
        if (!sf.StartEditingShapes(true, null))
        {
            MessageBox.Show("Failed to start edit mode: " + sf.Table.get_ErrorMsg(sf.LastErrorCode));
        }
        else
        {
            sf.UseQTree = true;
            sf.Labels.Generate("[Name]", tkLabelPositioning.lpCentroid, false);

            axMap1.AddLayer(sf, true);
            axMap1.SendMouseMove = true;
            axMap1.ShowRedrawTime = true;
            axMap1.MapUnits = tkUnitsOfMeasure.umMeters;
            axMap1.CurrentScale = 50000;
            axMap1.CursorMode = tkCursorMode.cmNone;
            MapEvents.MouseMoveEvent += new AxMapWinGIS._DMapEvents_MouseMoveEventHandler(axMap1_MouseMoveEvent);  // change MapEvents to axMap1

            int m_drawingHandle = axMap1.NewDrawing(tkDrawReferenceList.dlScreenReferencedList);
            Labels labels = axMap1.get_DrawingLabels(m_drawingHandle);
            labels.FrameVisible = true;
            labels.FrameType = tkLabelFrameType.lfRectangle;
         }
    }

    // <summary>
    // Handles mouse move event. Determines which shape is under cursor. Calls drawing routine.
    // </summary>
    void axMap1_MouseMoveEvent(object sender, _DMapEvents_MouseMoveEvent e)
    {
        Labels labels = axMap1.get_DrawingLabels(0);
        labels.Clear();

        // it's assumed here that the layer we want to edit is the first 1 (with 0 index)
        int layerHandle = axMap1.get_LayerHandle(0);  
        Shapefile sf = axMap1.get_Shapefile(layerHandle);
        if (sf != null)
        {
            double projX = 0.0;
            double projY = 0.0;
            axMap1.PixelToProj(e.x, e.y, ref projX, ref projY);

            object result = null;
            Extents ext = new Extents();
            ext.SetBounds(projX, projY, 0.0, projX, projY, 0.0);
            if (sf.SelectShapes(ext, 0.0, SelectMode.INTERSECTION, ref result))
            {
                int[] shapes = result as int[];
                if (shapes.Length == 1)
                {
                    string s = "";
                    for (int i = 0; i < sf.NumFields; i++)
                    {
                        s += sf.get_Field(i).Name + ": " + sf.get_CellValue(i, shapes[0]) + "\n";
                    }
                    labels.AddLabel(s, e.x + 80, e.y, 0.0, -1);

                    Shape shape = sf.get_Shape(shapes[0]);
                }
            }
        }

        axMap1.Refresh();
    }
}
 All Classes Files Functions Enumerations Properties