0

I'm writing a WPF applicating, in C#, using ArcObjects.

I have an ESRI.ArcGIS.Controls.AxMapControl on my form, and I'm trying to draw some graphics elements on top of it.

The map I'm developing with is a customer-provided mdf of the state of Georgia.

I'm trying an example I found here: How to interact with map elements.

public void AddTextElement(IMap map, double x, double y)
{
    IGraphicsContainer graphicsContainer = map as IGraphicsContainer;
    IElement element = new TextElementClass();
    ITextElement textElement = element as ITextElement;

    //Create a point as the shape of the element.
    IPoint point = new PointClass();
    point.X = x;
    point.Y = y;
    element.Geometry = point;
    textElement.Text = "Hello World";
    graphicsContainer.AddElement(element, 0);

    //Flag the new text to invalidate.
    IActiveView activeView = map as IActiveView;
    activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}

It took while to figure out how to project the lat/long of Atlanta to the coordinate system of the map, but I'm pretty sure that I've got it right. The x/y values I'm passing into AddTextElement() are clearly within the Atlanta area, according to the Location data I see when I use the Identify tool on the map.

But I'm not seeing the text. Everything seems to be working correctly, but I'm not seeing the text.

I can see a number of possibilities:

  • The layer I'm adding the TextElement to isn't visible, or doesn't exist.
  • I need to apply a spatial reference system to the point I'm setting as the TextElement's geometry
  • The text is drawing fine, but there's something wrong with the font - it's invisibly small, or in a transparent color, etc.

Haven't a clue, which.

I was hoping there was something obvious I was missing.

===

As I've continued to play with this, since my original posting, I've discovered that the problem is the scaling - the text is showing up where it should, only unreadably small.

This is what Rich Wawrzonek had suggested.

If I set a TextSymbol class, with a specified Size, the size does apply, and I see me text larger or smaller. Unfortunately, the text still resizes as the map zooms in and out, and my trying to set ScaleText = false doesn't fix it.

My latest attempt:

public void AddTextElement(IMap map, double x, double y, string text)
{
    var textElement = new TextElementClass
    {
        Geometry = new PointClass() { X = x, Y = y },
        Text = text,
        ScaleText = false,
        Symbol = new TextSymbolClass {Size = 25000}
    };

    (map as IGraphicsContainer)?.AddElement(textElement, 0);

    (map as IActiveView)?.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}

I recognize that the above is organized very differently than the way is usually done with ESRI sample code. I find the way ESRI does it to be every difficult to read, but switch from one to another is pretty mechanical.

This is the same function, organized in a more traditional manner. The behavior should be identical, and I'm seeing exactly the same behavior - the text is drawn to a specified size, but scales as the map zooms.

public void AddTextElement(IMap map, double x, double y, string text)
{
    IPoint point = new PointClass();
    point.X = x;
    point.Y = y;

    ITextSymbol textSymbol = new TextSymbolClass();
    textSymbol.Size = 25000;

    var textElement = new TextElementClass();
    textElement.Geometry = point;
    textElement.Text = text;
    textElement.ScaleText = false;
    textElement.Symbol = textSymbol;

    var iGraphicsContainer = map as IGraphicsContainer;
    Debug.Assert(iGraphicsContainer != null, "iGraphicsContainer != null");
    iGraphicsContainer.AddElement(textElement, 0);

    var iActiveView = (map as IActiveView);
    Debug.Assert(iActiveView != null, "iActiveView != null");
    iActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}

Any ideas as to why ScaleText is being ignored?

Jeff Dege
  • 11,190
  • 22
  • 96
  • 165

1 Answers1

1

You are only setting the geometry and the text of the text element. You also need to set the Symbol and ScaleText properties. The ScaleText property boolean will determine whether or not it scales with the map. The Symbol property needs to be created and set via the ITextSymbol interface.

See here for an example by Esri.

Rich Wawrzonek
  • 189
  • 4
  • 15