解决从模板中删除IMapSurround对象(图例、指北针、比例尺等)时,报内存错误的情况


        //双击修改地图元素
        private void axPageLayoutControl1_OnDoubleClick(object sender, IPageLayoutControlEvents_OnDoubleClickEvent e)
        {
            try
            {
                if (pPageLayoutControl.CurrentTool !=null && pPageLayoutControl.CurrentTool != CommonPrintClass.pCommand)
                {
                    return;
                }
                IPoint pPoint = pPageLayoutControl.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(e.x, e.y);
                IGraphicsContainer pGraphicsContainer = pPageLayoutControl.PageLayout as IGraphicsContainer;
                IGraphicsContainerSelect pGraphicsContainerSelect = pPageLayoutControl.PageLayout as IGraphicsContainerSelect;

                IEnumElement pEnumElement = pGraphicsContainer.LocateElements(pPoint, 1);

                pEnumElement.Reset();
                IElement pElement = pEnumElement.Next();
                while (pElement != null)
                {
                    if (pElement is IMapFrame)
                    {
                        pElement = pEnumElement.Next();
                        continue;
                    }

                    IElement pNewElement = ModifyMapElement(pElement);

                    if (pNewElement != null)
                    {
                        if (pElement is IMapSurroundFrame)     //解决从模板中删除IMapSurround对象(图例、指北针、比例尺等)时,报内存错误的情况。
                        {
                            IMapSurroundFrame pMapSf = pElement as IMapSurroundFrame;
                            IMapSurround pMapSurround = pMapSf.MapSurround;
                            if (pMapSurround is IMarkerNorthArrow || pMapSurround is ILegend || pMapSurround is IScaleBar || pMapSurround is IScaleText)
                            {
                                IMap pMap = pPageLayoutControl.ActiveView.FocusMap;
                                pMapSurround.Map = pMap;
                                pMap.DeleteMapSurround(pMapSurround);
                                pPageLayoutControl.Refresh(esriViewDrawPhase.esriViewGraphics, null, null);

                            }
                            if (pElement!=null)
                            {
                                pGraphicsContainer.DeleteElement(pElement);
                            }
                        }
                        else
                        {
                            pGraphicsContainer.DeleteElement(pElement);
                        }
                        pGraphicsContainer.AddElement(pNewElement, 0);
                        pGraphicsContainerSelect.SelectElement(pNewElement);
                        pPageLayoutControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
                        break;
                    }
                    else
                    {
                        return;
                        //break;
                    }
                }
            }
            catch
            {
            }
        }

posted on 2011-06-27 21:09  xc#  阅读(853)  评论(0编辑  收藏  举报