After a ShapeFile that was read to IFeatureLayer converted to IGraphicElements, I'm trying to draw that on map (using Sublayer of ICompositeGraphicsLayer). Of course, I succeed to display it on map, however, when I move the map to other position using IScreenDisplay.Pan*(), them automatically redraws.
I want to prevent them redraw. How can I do that?
To solve the above problem, I tried to use ILayer.Cached and IActiveView.PartialRefresh, but I failed.
The sample code is
AxMapControl m_axMapControl;
public AxMapControl getMapControl()
{
return m_axMapControl;
}
public IActiveView getActiveView()
{
return getMapControl().ActiveView;
}
public IMap getFocusMap()
{
return getActiveView().FocusMap;
}
internal static IGraphicElements SelectedFeaturesToGraphics(/*IMap targetMap,*/ IFeatureLayer featureLayer)
{
var elements = new GraphicElements() as IGraphicElements;
IGeoFeatureLayer geoFeatureLayer = (IGeoFeatureLayer)featureLayer;
IFeatureRenderer featureRenderer = geoFeatureLayer.Renderer;
IQueryFilter queryFilter = new QueryFilter();
var cursor = featureLayer.FeatureClass.Search(queryFilter, false);
ISymbol symbol = null;
switch (featureLayer.FeatureClass.ShapeType)
{
case esriGeometryType.esriGeometryPoint:
case esriGeometryType.esriGeometryMultipoint:
{
var s = new SimpleMarkerSymbol();
s.Style = esriSimpleMarkerStyle.esriSMSCircle;
s.Size = 2;
s.Color = GeoUtil.GetIColor(Colors.Black);
symbol = s as ISymbol;
}
break;
case esriGeometryType.esriGeometryLine:
case esriGeometryType.esriGeometryPolyline:
{
var s = new SimpleLineSymbol();
s.Color = GeoUtil.GetIColor(Colors.Black);
s.Style = esriSimpleLineStyle.esriSLSSolid;
s.Width = 1;
symbol = s as ISymbol;
}
break;
case esriGeometryType.esriGeometryPolygon:
{
var l = new SimpleLineSymbol();
l.Color = GeoUtil.GetIColor(Colors.Black);
l.Width = 1;
l.Style = esriSimpleLineStyle.esriSLSSolid;
var s = new SimpleFillSymbol();
s.Color = l.Color;
s.Style = esriSimpleFillStyle.esriSFSSolid;
s.Outline = l;
symbol = s as ISymbol;
}
break;
}
IFeatureCursor featureCursor = (IFeatureCursor)cursor;
IFeature feature = featureCursor.NextFeature();
while (feature != null)
{
IGeometry geometry = feature.Shape;
//ISymbol symbol = featureRenderer.get_SymbolByFeature(feature);
//string elementName = string.Format("{0}_{1}", featureLayer.Name, feature.OID);
IElement element = null;
switch (geometry.GeometryType)
{
case esriGeometryType.esriGeometryPoint:
element = new MarkerElementClass();
(element as IElement).Geometry = geometry;
//(element as IMarkerElement).Symbol = symbol as IMarkerSymbol;
//graphicsCon.AddElement(element, 0);
break;
case esriGeometryType.esriGeometryPolyline:
element = new LineElementClass();
element.Geometry = geometry;
//(element as ILineElement).Symbol = symbol as ILineSymbol;
//graphicsCon.AddElement(element, 0);
break;
case esriGeometryType.esriGeometryPolygon:
element = new PolygonElementClass();
element.Geometry = geometry;
//(element as IFillShapeElement).Symbol = symbol as IFillSymbol;
//graphicsCon.AddElement(element, 0);
break;
}
element.Locked = true;
elements.Add(element as IGraphicElement);
feature = featureCursor.NextFeature();
}
System.Runtime.InteropServices.Marshal.ReleaseComObject(cursor);
return elements;
}
public void AddGeometry(string key, IGraphicsElement elements)
{
ICompositeGraphicsLayer compositeGraphicsLayer = (ICompositeGraphicsLayer)FindLayer(SuperGraphicsLayerID);
if (compositeGraphicsLayer == null)
{
MessageBox.Show("compositeGraphicsLayer is null");
return;
}
var layer = compositeGraphicsLayer.AddLayer(l.Key, null) as ILayer;
int cnt = graphicElements.Count;
IElementCollection collection = new ElementCollectionClass();
for (int i = 0; i < cnt; i++)
{
collection.Add(graphicElements.get_Element(i) as IElement);
}
layer.Visible = true;
layer.Cached = true;
(layer as IGraphicsLayer).Activate(getActiveView().ScreenDisplay);
(layer as IGraphicsContainer).AddElements(collection, 0);
//getActiveView().Deactivate();
//getActiveView().Activate(getActiveView().ScreenDisplay.hWnd);
getActiveView().ScreenDisplay.Invalidate(null, true, getActiveView().get_ScreenCacheID(esriViewDrawPhase.esriViewGraphics, null));
}
private void onMouseUp()
{
if (bGrab == true)
{
bGrab = false;
IEnvelope extent = m_focusScreenDisplay.PanStop();
double distance = Math.Sqrt(Math.Pow(e.x - ptMouseDownOnDisplay.X, 2) + Math.Pow(e.y - ptMouseDownOnDisplay.Y, 2));
if (distance > 1)
{
//Check if user dragged a rectangle or just clicked.
//If a rectangle was dragged, m_ipFeedback in non-NULL
if (extent != null)
{
m_focusScreenDisplay.DisplayTransformation.VisibleBounds = extent;
m_focusScreenDisplay.Invalidate(null, true, (short)esriScreenCache.esriNoScreenCache);
}
MainFrame.g_Main.AddScene(GetCenterPoint(), getMapControl().ActiveView.Extent);
return;
}
}
}
private void SampleCode()
{
var elements = SelectedFeaturesToGraphics(readShapeFile("...."));
AddGeometry("sample", elements); // Automatically redraw when move.
getMapContrl().AddLayer(readShapeFile("....")); // Didn't redraw again except first time when move.
}