I have a PagyLayoutControl in a print preview dialog. It shows the same extent and layers as the master MapControl. Both controls are not syncronised, because changes made in the PageLayoutControl shall not be reflected in the MapControl. So I load the same map file into the PageLayoutControl and copy the layers from the MapControl. That works fine.
The MapControl adds some Texts to the selected features in the AfterDraw event like this:
private void MapControl_OnAfterDraw(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnAfterDrawEvent e)
{
if (e.viewDrawPhase == (int)esriViewDrawPhase.esriViewForeground)
{
for (int iL = 0; iL < _mapControl.Map.LayerCount; iL++)
{
IFeatureSelection features = _mapControl.Map.Layer[iL] as IFeatureSelection;
if ((features != null) && (features.SelectionSet != null))
{
ICursor cursor;
features.SelectionSet.Search(null, true, out cursor);
IFeature feature;
while ((feature = ((IFeatureCursor)cursor).NextFeature()) != null)
{
int iBt1 = feature.Fields.FindField("TEXT1");
if (iBt1 < 0)
iBt1 = feature.Fields.FindField("NAME");
if ((feature.Shape != null) && (iBt1 >= 0))
{
object ofskz = feature.get_Value(iBt1);
if (ofskz != null)
{
String someText = ofskz.ToString();
_mapControl.DrawShape(feature.Shape);
IArea farea = feature.Shape as IArea;
ESRI.ArcGIS.Geometry.Point start = new ESRI.ArcGIS.Geometry.Point();
start.X = farea.LabelPoint.X - 100;
start.Y = farea.LabelPoint.Y;
ESRI.ArcGIS.Geometry.Point end = new ESRI.ArcGIS.Geometry.Point();
end.X = farea.LabelPoint.X + 100;
end.Y = farea.LabelPoint.Y;
ESRI.ArcGIS.Geometry.Polyline baseline = new ESRI.ArcGIS.Geometry.Polyline();
baseline.AddPoint(start);
baseline.AddPoint(end);
ITextSymbol textSymbol = new TextSymbolClass();
textSymbol.VerticalAlignment = esriTextVerticalAlignment.esriTVACenter;
object oTextSym = textSymbol;
_mapControl.DrawText((ESRI.ArcGIS.Geometry.IGeometry)baseline, someText, ref oTextSym);
}
}
}
}
}
}
}
I tried to reproduce this for the PageLayoutControl but failed, because there are no methods like MapControls DrawText
and DrawShape
. I didn't found something like DrawShape
with the PageLayoutControl. OK, I can live without the shape been drawn.
But the text is what bugs me. I found and tried ActiveView.ScreenDisplay.DrawText
which indeed draws text on the map, but it seems that is uses a different positioning than the MapControl.DrawText
. I simply tried to use the Point
start
as the IGeometry
for DrawText
:
private void Pagelayout_OnAfterDraw(object sender, ESRI.ArcGIS.Controls.IPageLayoutControlEvents_OnAfterDrawEvent e)
{
if (e.viewDrawPhase == (int)esriViewDrawPhase.esriViewForeground)
{
for (int iL = 0; iL < _pageLayoutControl.ActiveView.FocusMap.LayerCount; iL++)
{
IFeatureSelection features = _pageLayoutControl.ActiveView.FocusMap.Layer[iL] as IFeatureSelection;
if ((features != null) && (features.SelectionSet != null))
{
ICursor cursor;
features.SelectionSet.Search(null, true, out cursor);
IFeature feature;
while ((feature = ((IFeatureCursor)cursor).NextFeature()) != null)
{
int iBt1 = feature.Fields.FindField("TEXT1");
if (iBt1 < 0)
iBt1 = feature.Fields.FindField("NAME");
if ((feature.Shape != null) && (iBt1 >= 0))
{
object ofskz = feature.get_Value(iBt1);
if (ofskz != null)
{
String someText = ofskz.ToString();
//_mapControl.DrawShape(feature.Shape);
IArea farea = feature.Shape as IArea;
ESRI.ArcGIS.Geometry.Point start = new ESRI.ArcGIS.Geometry.Point();
start.X = farea.LabelPoint.X - 100;
start.Y = farea.LabelPoint.Y;
ITextSymbol textSymbol = new TextSymbolClass();
textSymbol.VerticalAlignment = esriTextVerticalAlignment.esriTVACenter;
object oTextSym = textSymbol;
int mapx;
int mapy;
_pageLayoutControl.ActiveView.ScreenDisplay.DisplayTransformation.FromMapPoint(start, out mapx, out mapy);
IPoint point = new ESRI.ArcGIS.Geometry.Point();
point.X = mapx;
point.Y = mapy;
_pageLayoutControl.ActiveView.ScreenDisplay.DrawText((IGeometry)point, someText);
}
}
}
}
}
}
}
}
The Text is drawn somewhere on the map but not where it is drawn in the MapControl. start
has a value like 3103951.3241, 2503651.3274, which seems to me to be world coordinates. I tested to draw at the mouse position (something like 6.4,7.2 - screen coordinates) which draws at the correct position. So I tried the conversion with FromMapPoint
which changed the numbers but with no success in drawing at the right position.
How do I find the right input coordinate for DrawText? Or is there an alternative way to draw text in the PageLayoutControl?